如何使用 Web Crypto API SubtleCrypto.deriveKey() 为 PBKDF2 设置外长度
How to set the out-length with the Web Crypto API SubtleCrypto.deriveKey() for PBKDF2
根据doc一个用PBKDF2派生密码的简单例子是
return window.crypto.subtle.importKey(
'raw',
encoder.encode(password),
{name: 'PBKDF2'},
false,
['deriveBits', 'deriveKey']
).then(function(key) {
return window.crypto.subtle.deriveKey(
{ "name": 'PBKDF2',
"salt": encoder.encode(salt),
"iterations": iterations,
"hash": 'SHA-256'
},
key,
{ "name": 'AES-CTR', "length": 128 }, //api requires this to be set
true, //extractable
[ "encrypt", "decrypt" ] //allowed functions
)
}).then(function (webKey) {
return crypto.subtle.exportKey("raw", webKey);
})
正如你所见,API 让你选择:
- 密钥派生函数(及其底层哈希)
- 盐
- 迭代
- 原始密钥material(即密码)
但是据我所知,没有选择外长的选项。 似乎密码套件参数{ "name": 'AES-CTR', "length": 128 }
影响输出长度,但只能选择16和32字节。
例如 10,000 轮,盐:'salt',密码:'key material' 128 将产生以下 16 个字节:
26629f0e2b7b14ed4b84daa8071c648c
而 { "name": 'AES-CTR', "length": 256 }
你会得到
26629f0e2b7b14ed4b84daa8071c648c648d2cce067f93e2c5bde0c620030521
如何将输出长度设置为 16 或 32 字节?我必须自己截断它吗?
deriveKey 函数与 AES 算法选项 returns 你 AES 密钥。可能的 AES 密钥长度参数如下(在 位 中):
- 128
- 192
- 256
因此,您在使用AES 密码时只能从它们中进行选择。在我看来,修改从 deriveKey 函数生成的密钥是一个 糟糕的 想法。首先,你会破坏一个算法标准,而且以后你使用截断键也会有问题。
但是如果你只想使用 PBKDF2 并从密码中导出 bits,你可以使用 deriveBits 函数。这是一个例子:
window.crypto.subtle.deriveBits(
{
name: "PBKDF2",
salt: window.crypto.getRandomValues(new Uint8Array(16)),
iterations: 50000,
hash: {name: "SHA-256"}, // can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
},
key, //your key from generateKey or importKey
512 //the number of bits you want to derive, values: 8, 16, 32, 64, 128, 512, 1024, 2048
)
.then(function(bits){
//returns the derived bits as an ArrayBuffer
console.log(new Uint8Array(bits));
})
.catch(function(err){
console.error(err);
});
此处有更多示例 - https://github.com/diafygi/webcrypto-examples#pbkdf2---derivekey .
此外,我已经测试了导出位的可能值,它们是 2 的幂(从 8 到 2048)。
希望对您有所帮助。请记住,如果您只想使用 AES 密码,最好使用默认值和 deriveKey 函数。
根据doc一个用PBKDF2派生密码的简单例子是
return window.crypto.subtle.importKey(
'raw',
encoder.encode(password),
{name: 'PBKDF2'},
false,
['deriveBits', 'deriveKey']
).then(function(key) {
return window.crypto.subtle.deriveKey(
{ "name": 'PBKDF2',
"salt": encoder.encode(salt),
"iterations": iterations,
"hash": 'SHA-256'
},
key,
{ "name": 'AES-CTR', "length": 128 }, //api requires this to be set
true, //extractable
[ "encrypt", "decrypt" ] //allowed functions
)
}).then(function (webKey) {
return crypto.subtle.exportKey("raw", webKey);
})
正如你所见,API 让你选择:
- 密钥派生函数(及其底层哈希)
- 盐
- 迭代
- 原始密钥material(即密码)
但是据我所知,没有选择外长的选项。 似乎密码套件参数{ "name": 'AES-CTR', "length": 128 }
影响输出长度,但只能选择16和32字节。
例如 10,000 轮,盐:'salt',密码:'key material' 128 将产生以下 16 个字节:
26629f0e2b7b14ed4b84daa8071c648c
而 { "name": 'AES-CTR', "length": 256 }
你会得到
26629f0e2b7b14ed4b84daa8071c648c648d2cce067f93e2c5bde0c620030521
如何将输出长度设置为 16 或 32 字节?我必须自己截断它吗?
deriveKey 函数与 AES 算法选项 returns 你 AES 密钥。可能的 AES 密钥长度参数如下(在 位 中):
- 128
- 192
- 256
因此,您在使用AES 密码时只能从它们中进行选择。在我看来,修改从 deriveKey 函数生成的密钥是一个 糟糕的 想法。首先,你会破坏一个算法标准,而且以后你使用截断键也会有问题。
但是如果你只想使用 PBKDF2 并从密码中导出 bits,你可以使用 deriveBits 函数。这是一个例子:
window.crypto.subtle.deriveBits(
{
name: "PBKDF2",
salt: window.crypto.getRandomValues(new Uint8Array(16)),
iterations: 50000,
hash: {name: "SHA-256"}, // can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
},
key, //your key from generateKey or importKey
512 //the number of bits you want to derive, values: 8, 16, 32, 64, 128, 512, 1024, 2048
)
.then(function(bits){
//returns the derived bits as an ArrayBuffer
console.log(new Uint8Array(bits));
})
.catch(function(err){
console.error(err);
});
此处有更多示例 - https://github.com/diafygi/webcrypto-examples#pbkdf2---derivekey .
此外,我已经测试了导出位的可能值,它们是 2 的幂(从 8 到 2048)。
希望对您有所帮助。请记住,如果您只想使用 AES 密码,最好使用默认值和 deriveKey 函数。