最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

java实现谷歌二步验证(GoogleAuthenticator)

IT圈 admin 41浏览 0评论

2024年3月11日发(作者:种含海)

long t = tTimeMillis();

GoogleAuthenticator ga = new GoogleAuthenticator();

dowSize(15); // should give 5 * 30 seconds

boolean r = _code(savedSecret, code, t);

return r;

}

public static String genSecret(String name) {

String secret = teSecretKey();

//arcodeURL("testuser","testhost", secret);

arcodeURL(name,

"testhost", secret);

return secret;

}

public static String generateSecretKey() {

SecureRandom sr = null;

try {

sr = tance(RANDOM_NUMBER_ALGORITHM);

d(Base64(SEED));

byte[] buffer = teSeed(SECRET_SIZE);

Base32 codec = new Base32();

byte[] bEncodedKey = (buffer);

String encodedKey = new String(bEncodedKey);

return encodedKey;

}catch (NoSuchAlgorithmException e) {

// should configuration error

}

return null;

}

public static String getQRBarcodeURL(String user, String host, String secret) {

String format = "/chart?chs=200x200&chld=M%%7C0&cht=qr&chl=otpauth://totp/%s@%s%%3Fsecret%%3D%s";

return (format, user, host, secret);

}

public boolean check_code(String secret, long code, long timeMsec) {

Base32 codec = new Base32();

byte[] decodedKey = (secret);

// convert unix msec time into a 30 second "window"

// this is per the TOTP spec (see the RFC for details)

long t = (timeMsec / 1000L) / 30L;

// Window is used to check codes generated in the near past.

// You can use this value to tune how far you're willing to go.

for (int i = -window_size; i <= window_size; ++i) {

long hash;

try {

hash = verify_code(decodedKey, t + i);

}catch (Exception e) {

// Yes, this is bad form - but

// the exceptions thrown would be rare and a static configuration problem

tackTrace();

throw new RuntimeException(sage());

//return false;

}

if (hash == code) {

return true;

}

}

// The validation code is invalid.

return false;

}

private static int verify_code(byte[] key, long t) throws NoSuchAlgorithmException, InvalidKeyException {

byte[] data = new byte[8];

long value = t;

for (int i = 8; i-- > 0; value >>>= 8) {

data[i] = (byte) value;

}

SecretKeySpec signKey = new SecretKeySpec(key, "HmacSHA1");

Mac mac = tance("HmacSHA1");

(signKey);

byte[] hash = l(data);

int offset = hash[20 - 1] & 0xF;

// We're using a long because Java hasn't got unsigned int.

long truncatedHash = 0;

for (int i = 0; i < 4; ++i) {

truncatedHash <<= 8;

// We are dealing with signed bytes:

// we just keep the first byte.

truncatedHash |= (hash[offset + i] & 0xFF);

}

2024年3月11日发(作者:种含海)

long t = tTimeMillis();

GoogleAuthenticator ga = new GoogleAuthenticator();

dowSize(15); // should give 5 * 30 seconds

boolean r = _code(savedSecret, code, t);

return r;

}

public static String genSecret(String name) {

String secret = teSecretKey();

//arcodeURL("testuser","testhost", secret);

arcodeURL(name,

"testhost", secret);

return secret;

}

public static String generateSecretKey() {

SecureRandom sr = null;

try {

sr = tance(RANDOM_NUMBER_ALGORITHM);

d(Base64(SEED));

byte[] buffer = teSeed(SECRET_SIZE);

Base32 codec = new Base32();

byte[] bEncodedKey = (buffer);

String encodedKey = new String(bEncodedKey);

return encodedKey;

}catch (NoSuchAlgorithmException e) {

// should configuration error

}

return null;

}

public static String getQRBarcodeURL(String user, String host, String secret) {

String format = "/chart?chs=200x200&chld=M%%7C0&cht=qr&chl=otpauth://totp/%s@%s%%3Fsecret%%3D%s";

return (format, user, host, secret);

}

public boolean check_code(String secret, long code, long timeMsec) {

Base32 codec = new Base32();

byte[] decodedKey = (secret);

// convert unix msec time into a 30 second "window"

// this is per the TOTP spec (see the RFC for details)

long t = (timeMsec / 1000L) / 30L;

// Window is used to check codes generated in the near past.

// You can use this value to tune how far you're willing to go.

for (int i = -window_size; i <= window_size; ++i) {

long hash;

try {

hash = verify_code(decodedKey, t + i);

}catch (Exception e) {

// Yes, this is bad form - but

// the exceptions thrown would be rare and a static configuration problem

tackTrace();

throw new RuntimeException(sage());

//return false;

}

if (hash == code) {

return true;

}

}

// The validation code is invalid.

return false;

}

private static int verify_code(byte[] key, long t) throws NoSuchAlgorithmException, InvalidKeyException {

byte[] data = new byte[8];

long value = t;

for (int i = 8; i-- > 0; value >>>= 8) {

data[i] = (byte) value;

}

SecretKeySpec signKey = new SecretKeySpec(key, "HmacSHA1");

Mac mac = tance("HmacSHA1");

(signKey);

byte[] hash = l(data);

int offset = hash[20 - 1] & 0xF;

// We're using a long because Java hasn't got unsigned int.

long truncatedHash = 0;

for (int i = 0; i < 4; ++i) {

truncatedHash <<= 8;

// We are dealing with signed bytes:

// we just keep the first byte.

truncatedHash |= (hash[offset + i] & 0xFF);

}

发布评论

评论列表 (0)

  1. 暂无评论