【问题标题】:Create credentials hash using Android使用 Android 创建凭据哈希
【发布时间】:2021-09-17 21:39:21
【问题描述】:

有人告诉我他创建了这样的哈希:

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

在 Android 中,我不知道如何将其转换为 Java。我试过了

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);

但这可能是不正确的;)

而且这也比原始解决方案慢得多(据说原始解决方案在华为 P20 上花费不到 1 秒,而在我的 P30 上花费将近一分钟)。

谁能帮我翻译这段代码?

【问题讨论】:

    标签: android encryption hash pbkdf2


    【解决方案1】:

    如果第一个代码用于 react native 和 library "react-native-aes",那么它使用 SHA512 作为哈希而不是 SHA-1。

    查看它的实现:

    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

    请注意,PBKDF2withHmacSHA512 至少需要 Android API level 26 (Android 8)。所以我的建议是使用 Spongycastle Java 库,就像 react native 库创建 PBKDF2 哈希一样。

    【讨论】:

    • 非常感谢 - 我会努力的。
    猜你喜欢
    • 2015-10-06
    • 2013-12-20
    • 2016-10-06
    • 1970-01-01
    • 2011-09-20
    • 2012-01-25
    • 2012-07-19
    • 2021-02-21
    • 1970-01-01
    相关资源
    最近更新 更多