我尝试在 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
}