Untitled

 avatar
unknown
java
a month ago
2.1 kB
2
Indexable
	public static String generateHWID(){

		try{
			// Combine system properties to create a unique ID
			String systemInfo=getSystemInfo();
			String macAddress=getMACAddress();
		 	String rawHWID=systemInfo+macAddress;
			// Generate a SHA-256 hash from the combined string to make it unique
			return generateHash(rawHWID);

		}catch(Exception e){

			e.printStackTrace();
			return "";

		}

	}

	// Get a unique system info string (based on system properties)
	private static String getSystemInfo(){

		String userName=System.getProperty("user.name");
		String osArch=System.getProperty("os.arch");
		String osName=System.getProperty("os.name");
		String osVersion=System.getProperty("os.version");
		// Combining system properties to form a unique string
		return userName+osArch+osName+osVersion;

	}

	// Get the MAC address of the first network interface (e.g., Ethernet or WiFi)
	private static String getMACAddress(){

		try{
			// Get the network interfaces
			Enumeration<NetworkInterface> networkInterfaces=NetworkInterface.getNetworkInterfaces();

			while (networkInterfaces.hasMoreElements()){

				NetworkInterface networkInterface=networkInterfaces.nextElement();
				byte[] mac=networkInterface.getHardwareAddress();

				// If we find a MAC address, return it as a string
				if(mac!=null){

					StringBuilder macAddress=new StringBuilder();
					for (byte b:mac)macAddress.append(String.format("%02X:", b));
					// Remove last colon (":")
					return macAddress.substring(0,macAddress.length()-1);

				}

			}

		}catch (Exception e){

			e.printStackTrace();

		}

		return "00:00:00:00:00:00"; // Default value if MAC address cannot be obtained

	}

	// Generate a SHA-256 hash of the given string (used for HWID generation)
	private static String generateHash(String input) throws NoSuchAlgorithmException{

		MessageDigest digest=MessageDigest.getInstance("SHA-256");
		byte[] hash=digest.digest(input.getBytes());
		StringBuilder hexString=new StringBuilder();
		for(byte b:hash)hexString.append(String.format("%02x",b));

		return hexString.toString();  // Return the full hash string

	}
Leave a Comment