diff --git a/package-lock.json b/package-lock.json index 677c406..57860ef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@peculiar/webcrypto", - "version": "1.2.0", + "version": "1.2.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@peculiar/webcrypto", - "version": "1.2.0", + "version": "1.2.2", "license": "MIT", "dependencies": { "@peculiar/asn1-schema": "^2.0.38", diff --git a/package.json b/package.json index 66dffe0..690869c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@peculiar/webcrypto", - "version": "1.2.0", + "version": "1.2.2", "description": "A WebCrypto Polyfill for NodeJS", "repository": { "type": "git", diff --git a/src/crypto.ts b/src/crypto.ts index 2b558b3..513d9a3 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -10,7 +10,7 @@ export class Crypto extends core.Crypto { if (!ArrayBuffer.isView(array)) { throw new TypeError("Failed to execute 'getRandomValues' on 'Crypto': parameter 1 is not of type 'ArrayBufferView'"); } - const buffer = Buffer.from(array.buffer); + const buffer = Buffer.from(array.buffer, array.byteOffset, array.byteLength); crypto.randomFillSync(buffer); return array; } diff --git a/src/keys/key.ts b/src/keys/key.ts index c8fd240..f9e1c9d 100644 --- a/src/keys/key.ts +++ b/src/keys/key.ts @@ -17,6 +17,6 @@ export class CryptoKey extends core.CryptoKey { @JsonProp({ type: JsonPropTypes.String }) protected kty = "oct"; - @JsonProp({ type: JsonPropTypes.String }) + @JsonProp({ type: JsonPropTypes.String, optional: true }) protected alg = ""; } diff --git a/test/crypto.ts b/test/crypto.ts index 7c895b8..93b1280 100644 --- a/test/crypto.ts +++ b/test/crypto.ts @@ -23,6 +23,16 @@ context("Crypto", () => { assert.strictEqual(Buffer.from(array2).equals(array), true); }); + it("Uint8Array subarray", () => { + const array = new Uint8Array(10); + const subarray = array.subarray(0, 5); + const array2 = crypto.getRandomValues(subarray); + + assert.notStrictEqual(Buffer.from(array).toString("hex"), "00000000000000000000"); + assert.strictEqual(subarray, array2); + assert.ok(Buffer.from(array).toString("hex").endsWith("0000000000")); + }); + it("Uint16Array", () => { const array = new Uint16Array(5); const array2 = crypto.getRandomValues(array); @@ -67,7 +77,7 @@ context("Crypto", () => { it("Ed25519", async () => { const keys = await crypto.subtle.generateKey({ name: "eddsa", namedCurve: "ed25519" } as globalThis.EcKeyGenParams, false, ["sign", "verify"]); - + assert.strictEqual(keys.privateKey.algorithm.name, "EdDSA"); assert.strictEqual((keys.privateKey.algorithm as EcKeyAlgorithm).namedCurve, "Ed25519"); }); @@ -195,4 +205,13 @@ context("Crypto", () => { }); }); + it("Import Secret JWK without 'alg' and 'key_ops' fields", async () => { + const aesKey = await crypto.subtle.generateKey({ name: "AES-CBC", length: 256 }, true, ["encrypt", "decrypt"]); + const jwk = await crypto.subtle.exportKey("jwk", aesKey); + delete jwk.key_ops; + delete jwk.alg; + const hmacKey = await crypto.subtle.importKey("jwk", jwk, { name: "HMAC", hash: "SHA-256" } as Algorithm, false, ["sign", "verify"]); + assert.strictEqual(hmacKey.algorithm.name, "HMAC"); + }); + });