简体   繁体   English

使用 Android 创建凭据哈希

[英]Create credentials hash using Android

someone told me he creates a hash like this:有人告诉我他创建了一个这样的哈希:

const enc = await NativeModules.Aes.pbkdf2(plaintext_pasword, serial, 100000, 256);
hashed_password= Buffer.from(enc, 'hex').toString('base64').substr(0, 32);

In Android, I don't know how to translate this to Java.在 Android 中,我不知道如何将其转换为 Java。 I tried我试过了

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec          spec    = new PBEKeySpec(password.toCharArray(), serialNumber.getBytes(), 100000, 256);
SecretKey        tmp     = factory.generateSecret(spec);
SecretKeySpec    key     = new SecretKeySpec(tmp.getEncoded(), "AES");

hashed_password = new String(Base64.encode(key.getEncoded(), Base64.NO_WRAP)).substring(0, 32);

but it might be this is not correct ;)但这可能是不正确的;)

And also this is far slower than the original solution (the original is said to take less than 1 sec on a Huawei P20, mine takes nearly a minute on my P30).而且这比原始解决方案慢得多(据说原始解决方案在华为 P20 上不到 1 秒,我的在 P30 上需要将近一分钟)。

Could anyone please help me to translate this code?谁能帮我翻译一下这段代码?

If the first code is for react native and the library "react-native-aes" then it uses SHA512 as hash and not SHA-1.如果第一个代码用于 react native 和库“react-native-aes”,那么它使用 SHA512 作为哈希而不是 SHA-1。

See it's implementation:看它的实现:

private static String pbkdf2(String pwd, String salt, Integer cost, Integer length)
throws NoSuchAlgorithmException, InvalidKeySpecException, UnsupportedEncodingException
{
    PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(new SHA512Digest());
    gen.init(pwd.getBytes("UTF_8"), salt.getBytes("UTF_8"), cost);
    byte[] key = ((KeyParameter) gen.generateDerivedParameters(length)).getKey();
    return bytesToHex(key);
}

https://github.com/tectiv3/react-native-aes/blob/master/android/src/main/java/com/tectiv3/aes/RCTAes.java#L178-L185 https://github.com/tectiv3/react-native-aes/blob/master/android/src/main/java/com/tectiv3/aes/RCTAes.java#L178-L185

Note that PBKDF2withHmacSHA512 requires at least Android API level 26 (Android 8) .请注意, PBKDF2withHmacSHA512至少需要Android API 级别 26 (Android 8) So my recommendation would be to use Spongycastle Java library in the same way the react native library creates the PBKDF2 hash.所以我的建议是使用 Spongycastle Java 库,就像 react native 库创建 PBKDF2 哈希一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM