Advertisement
Guest User

Untitled

a guest
Feb 16th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. public static class GoogleAuthenticator {
  2.         public static String generateSecret() {
  3.             SecureRandom random = new SecureRandom();
  4.             byte bytes[] = new byte[32];
  5.             random.nextBytes(bytes);
  6.  
  7.             Base32 codec = new Base32();
  8.             byte[] secretKey = Arrays.copyOf(bytes, GoogleAuthentictorSecretSize);
  9.             byte[] bEncodedKey = codec.encode(secretKey);
  10.             return new String(bEncodedKey);
  11.         }
  12.  
  13.         public static String getQRBarcodeURL(String user, String host, String secret) {
  14.             return "otpauth://totp/"+user+"@"+host+"?secret="+secret;
  15.         }
  16.  
  17.         private static int verify_code(byte[] key,long t) throws NoSuchAlgorithmException, InvalidKeyException {
  18.             byte[] data = new byte[8];
  19.             long value = t;
  20.             for (int i = 8; i-- > 0; value >>>= 8) {
  21.                 data[i] = (byte) value;
  22.             }
  23.            
  24.             SecretKeySpec signKey = new SecretKeySpec(key, "HmacSHA1");
  25.             Mac mac = Mac.getInstance("HmacSHA1");
  26.             mac.init(signKey);
  27.             byte[] hash = mac.doFinal(data);
  28.  
  29.  
  30.             int offset = hash[20 - 1] & 0xF;
  31.  
  32.             // We're using a long because Java hasn't got unsigned int.
  33.             long truncatedHash = 0;
  34.             for (int i = 0; i < 4; ++i) {
  35.                 truncatedHash <<= 8;
  36.                 // We are dealing with signed bytes:
  37.                 // we just keep the first byte.
  38.                 truncatedHash |= (hash[offset + i] & 0xFF);
  39.             }
  40.            
  41.             truncatedHash &= 0x7FFFFFFF;
  42.             truncatedHash %= 1000000;
  43.  
  44.             return (int) truncatedHash;
  45.         }
  46.  
  47.         public static boolean check_code(String secret, long code, long t) throws NoSuchAlgorithmException, InvalidKeyException {
  48.             Base32 codec = new Base32();
  49.             byte[] decodedKey = codec.decode(secret);
  50.  
  51.  
  52.             // Window is used to check codes generated in the near past.
  53.             // You can use this value to tune how far you're willing to go.
  54.             int window = 0;
  55.             for (int i = -window; i <= window; ++i) {
  56.                 long hash = verify_code(decodedKey, t + i);
  57.                 if (hash == code) {
  58.                     return true;
  59.                 }
  60.             }
  61.             // The validation code is invalid.
  62.             return false;
  63.         }
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement