Advertisement
Guest User

Serial

a guest
Mar 24th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package quickgen;
  2.  
  3.  
  4.  
  5. import java.io.PrintStream;
  6.  
  7. public class QuickGen
  8. {
  9.   private static int shortHash(CharSequence s, int start, int end)
  10.   {
  11.     int check = 0;
  12.  
  13.     for (int ii = start; ii < end; ii++) {
  14.       check = check * 149 + s.charAt(ii);
  15.     }
  16.     return check & 0xFFFF;
  17.   }
  18.  
  19.   public static String generate(String prefix) {
  20.     long ts = System.currentTimeMillis();
  21.     StringBuilder sb = new StringBuilder();
  22.  
  23.     for (int ii = 0; ii < prefix.length(); ii++)
  24.       sb.append(String.format("%X", new Object[] { Short.valueOf((short)prefix.charAt(ii)) }));
  25.     sb.append('-');
  26.     sb.append(String.format("%X", new Object[] { Long.valueOf(ts) }));
  27.  
  28.     int hash = shortHash(sb, 0, sb.length());
  29.  
  30.     sb.append('-');
  31.  
  32.     String lastKeyPart = String.format("%X", new Object[] { Integer.valueOf(hash) });
  33.  
  34.     while (lastKeyPart.length() < 4) {
  35.       lastKeyPart = new StringBuilder().append("0").append(lastKeyPart).toString();
  36.     }
  37.  
  38.     if (lastKeyPart.length() > 4)
  39.       lastKeyPart.substring(0, 4);
  40.     sb.append(lastKeyPart);
  41.  
  42.     return sb.toString();
  43.   }
  44.  
  45.   public static boolean isValid(String serial) {
  46.     int pos = serial.lastIndexOf('-');
  47.  
  48.     if (pos < 1) {
  49.       return false;
  50.     }
  51.     int checkHash;
  52.     try
  53.     {
  54.       checkHash = Integer.parseInt(serial.substring(pos + 1), 16);
  55.     } catch (Throwable x) {
  56.       return false;
  57.     }
  58.  
  59.     return checkHash == shortHash(serial, 0, pos);
  60.   }
  61.  
  62.   public static void main(String[] args) {
  63.     String s = generate("GF");
  64.     System.out.println(s);
  65.    
  66.     s = generate("AB");
  67.     System.out.println(s);
  68.     s = generate("TG");
  69.     System.out.println(s);
  70.    
  71.       s = generate("YH");
  72.     System.out.println(s);
  73.       s = generate("AG");
  74.     System.out.println(s);
  75.       s = generate("UI");
  76.     System.out.println(s);
  77.       s = generate("AO");
  78.     System.out.println(s);
  79.       s = generate("OP");
  80.     System.out.println(s);
  81.    
  82.       s = generate("DP");
  83.     System.out.println(s);
  84.       s = generate("XY");
  85.     System.out.println(s);
  86.       s = generate("OZ");
  87.     System.out.println(s);
  88.    
  89.    
  90.      
  91.    
  92.   }
  93.  
  94.   public static void PrintMessage(String message)
  95. {
  96.     System.out.println(message);
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement