Advertisement
jonalu

HWID.java

Feb 22nd, 2012
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. package net.jonalu.hwid;
  2.  
  3. import java.io.UnsupportedEncodingException;
  4. import java.net.NetworkInterface;
  5. import java.net.SocketException;
  6. import java.security.MessageDigest;
  7. import java.security.NoSuchAlgorithmException;
  8. import java.util.Enumeration;
  9.  
  10. public class HWID {
  11.     private static String hwid = null;
  12.     private static String convertToHex(byte[] data)
  13.     {
  14.         StringBuffer buf = new StringBuffer();
  15.  
  16.         for (int i = 0; i < data.length; i++)
  17.         {
  18.             int halfbyte = (data[i] >>> 4) & 0x0F;
  19.             int two_halfs = 0;
  20.             do
  21.             {
  22.                 if ((0 <= halfbyte) && (halfbyte <= 9))
  23.                     buf.append((char) ('0' + halfbyte));
  24.                 else
  25.                     buf.append((char) ('a' + (halfbyte - 10)));
  26.                 halfbyte = data[i] & 0x0F;
  27.             }
  28.             while(two_halfs++ < 1);
  29.         }
  30.         return buf.toString();
  31.     }
  32.  
  33.     private static String SHA512(String text)
  34.     throws NoSuchAlgorithmException, UnsupportedEncodingException
  35.     {
  36.         MessageDigest md;
  37.         md = MessageDigest.getInstance("SHA-512");
  38.         byte[] sha1hash = new byte[40];
  39.         md.update(text.getBytes("UTF-8"), 0, text.length());
  40.         sha1hash = md.digest();
  41.         return convertToHex(sha1hash);
  42.     }
  43.     public String getHWID() {
  44.         try {
  45.             if(hwid != null)
  46.                 return hwid;
  47.             hwid = "";
  48.             hwid += System.getenv("PROCESSOR_IDENTIFIER");
  49.             hwid += System.getenv("COMPUTERNAME");
  50.             Enumeration<NetworkInterface> netenum = NetworkInterface.getNetworkInterfaces();
  51.             if(netenum.hasMoreElements())
  52.                 hwid += netenum.nextElement().getHardwareAddress();
  53.             hwid = SHA512(hwid);
  54.             return hwid;
  55.         } catch(NoSuchAlgorithmException nsae) {} catch (SocketException e) {} catch (UnsupportedEncodingException e) {}
  56.         return null;
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement