【问题标题】:Libsodium JS KDF function produces different outputLibsodium JS KDF 函数产生不同的输出
【发布时间】:2019-08-21 10:13:37
【问题描述】:

我尝试在 Android、iOS 和 JS 上使用 crypto_kdf_derive_from_key 函数。在 Android 和 iOS 上,它产生相同的输出,但在 JS 上没有。上下文、主密钥和大小是相同的。任何想法为什么?

所有平台都在下面使用相同的核心函数:crypto_kdf_derive_from_key

JS:

generateKey(basedOnKey: string): Uint8Array {
    const masterKey = this.convertHexToBytes(basedOnKey);
    const context = this.textEncoder.encode('AAAAAAAA');
    const newKey = sodium.crypto_kdf_derive_from_key(sodium.crypto_secretbox_KEYBYTES, 0, context, masterKey);

    return newKey;
}

iOS:

public func getNewSecretKey(basedOn key: String) -> Data? {
    let masterKey = key.hexDecodedData().bytes
    let context = "AAAAAAAA"
    let newKey = sodium.keyDerivation.derive(secretKey: masterKey, index: 0, length: 32, context: context)

    return newKey?.data
}

【问题讨论】:

    标签: javascript ios swift libsodium sodium


    【解决方案1】:

    不要转换上下文。假设是一个字符串。

    【讨论】:

      【解决方案2】:

      Frank Denis 建议不要转换上下文,即:

      generateKey(basedOnKey: string): Uint8Array {
          const masterKey = this.convertHexToBytes(basedOnKey);
          const context = 'AAAAAAAA';
          const newKey = sodium.crypto_kdf_derive_from_key(sodium.crypto_secretbox_KEYBYTES, 0, context, masterKey);
      
          return newKey;
      }
      

      现在一切正常!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-20
        • 1970-01-01
        • 2020-02-10
        • 2014-05-06
        • 2014-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多