mirror of
https://github.com/denoland/deno.git
synced 2025-08-04 19:08:15 +00:00
feat(ext/crypto): implement unwrapKey (#12539)
This commit is contained in:
parent
c59f90d01f
commit
d31378726e
4 changed files with 229 additions and 6 deletions
|
@ -691,6 +691,60 @@ Deno.test(async function testAesKeyGen() {
|
|||
assertEquals(algorithm.length, 256);
|
||||
});
|
||||
|
||||
Deno.test(async function testUnwrapKey() {
|
||||
const subtle = crypto.subtle;
|
||||
|
||||
const AES_KEY: AesKeyAlgorithm & AesCbcParams = {
|
||||
name: "AES-CBC",
|
||||
length: 128,
|
||||
iv: new Uint8Array(16),
|
||||
};
|
||||
|
||||
const RSA_KEY: RsaHashedKeyGenParams & RsaOaepParams = {
|
||||
name: "RSA-OAEP",
|
||||
modulusLength: 2048,
|
||||
publicExponent: new Uint8Array([1, 0, 1]),
|
||||
hash: "SHA-1",
|
||||
};
|
||||
|
||||
const aesKey = await subtle.generateKey(AES_KEY, true, [
|
||||
"encrypt",
|
||||
"decrypt",
|
||||
]);
|
||||
|
||||
const rsaKeyPair = await subtle.generateKey(
|
||||
{
|
||||
name: "RSA-OAEP",
|
||||
hash: "SHA-1",
|
||||
publicExponent: new Uint8Array([1, 0, 1]),
|
||||
modulusLength: 2048,
|
||||
},
|
||||
false,
|
||||
["wrapKey", "encrypt", "unwrapKey", "decrypt"],
|
||||
);
|
||||
|
||||
const enc = await subtle.wrapKey(
|
||||
"raw",
|
||||
aesKey,
|
||||
rsaKeyPair.publicKey,
|
||||
RSA_KEY,
|
||||
);
|
||||
const unwrappedKey = await subtle.unwrapKey(
|
||||
"raw",
|
||||
enc,
|
||||
rsaKeyPair.privateKey,
|
||||
RSA_KEY,
|
||||
AES_KEY,
|
||||
false,
|
||||
["encrypt", "decrypt"],
|
||||
);
|
||||
|
||||
assert(unwrappedKey instanceof CryptoKey);
|
||||
assertEquals(unwrappedKey.type, "secret");
|
||||
assertEquals(unwrappedKey.extractable, false);
|
||||
assertEquals(unwrappedKey.usages, ["encrypt", "decrypt"]);
|
||||
});
|
||||
|
||||
Deno.test(async function testDecryptWithInvalidIntializationVector() {
|
||||
const data = new Uint8Array([42, 42, 42, 42]);
|
||||
const key = await crypto.subtle.generateKey(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue