admin管理员组文章数量:1428473
Hello everyone!
I have this code:
async function generateKey() {
const algoritm = { name: "AES-CBC", length: 256 };
const exportable = true;
const usage = ['encrypt'];
return await window.crypto.subtle.generateKey(algoritm, exportable, usage).then(key => { return key;
});
}
when I call console.log(generateKey ()); I get: >Promise
at first load and >Promise {<pending>}
when update browser window.
async function exportKey(key) {
const format = "jwk";
return await window.crypto.subtle.exportKey(format, key).then(key => { return key; });
}
when I call let key =generateKey(); console.log(exportKey (key));
I get: 'SubtleCrypto': parameter 2 is not of type 'CryptoKey'.
I have two questions about
What is the correct way to generate a key with the given parameters?
What is the correct way to export a generated key in JSON format?
I e read from: SubtleCrypto MDN Web Docs
Hello everyone!
I have this code:
async function generateKey() {
const algoritm = { name: "AES-CBC", length: 256 };
const exportable = true;
const usage = ['encrypt'];
return await window.crypto.subtle.generateKey(algoritm, exportable, usage).then(key => { return key;
});
}
when I call console.log(generateKey ()); I get: >Promise
at first load and >Promise {<pending>}
when update browser window.
async function exportKey(key) {
const format = "jwk";
return await window.crypto.subtle.exportKey(format, key).then(key => { return key; });
}
when I call let key =generateKey(); console.log(exportKey (key));
I get: 'SubtleCrypto': parameter 2 is not of type 'CryptoKey'.
I have two questions about
What is the correct way to generate a key with the given parameters?
What is the correct way to export a generated key in JSON format?
I e read from: SubtleCrypto MDN Web Docs
Share Improve this question edited May 28, 2020 at 17:58 LT-Sites asked May 28, 2020 at 17:37 LT-SitesLT-Sites 4156 silver badges20 bronze badges1 Answer
Reset to default 4Your API usage looks correct. You are getting the error 'SubtleCrypto': parameter 2 is not of type 'CryptoKey'
because parameter 2
is of type Promise. To fix this issue, resolve the promise from generateKey
before passing it to exportKey
const main = async () => {
const key = await generateKey()
const exported = await exportKey(key)
console.log(exported)
}
Of course, this can get even simpler if you use my library, rubico
const { pipe } = require('rubico')
const main = pipe([generateKey, exportKey, console.log])
The two examples are equivalent.
本文标签: javascriptHow to correctly implement Web Crypto API generateKey and exportKeyStack Overflow
版权声明:本文标题:javascript - How to correctly implement Web Crypto API generateKey and exportKey? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745503128a2661122.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论