MCPcopy Index your code
hub / github.com/nodejs/node / checkHmacKey

Function checkHmacKey

test/parallel/test-webcrypto-cryptokey-clone-transfer.js:71–121  ·  view source on GitHub ↗
(original)

Source from the content-addressed store, hash-verified

69}
70
71async function checkHmacKey(original) {
72 const data = Buffer.from('some data to sign');
73
74 const cloned = structuredClone(original);
75 assertSameCryptoKey(original, cloned);
76
77 const viaPort = await roundTripViaMessageChannel(original);
78 assertSameCryptoKey(original, viaPort);
79
80 // Round-trip: clone a clone.
81 const clonedAgain = structuredClone(viaPort);
82 assertSameCryptoKey(original, clonedAgain);
83 const viaPortAgain = await roundTripViaMessageChannel(cloned);
84 assertSameCryptoKey(original, viaPortAgain);
85
86 // Signatures produced by every copy must match.
87 const sigs = await Promise.all(
88 [original, cloned, viaPort, clonedAgain, viaPortAgain].map(
89 (k) => subtle.sign('HMAC', k, data),
90 ),
91 );
92 for (let i = 1; i < sigs.length; i++) {
93 assert.deepStrictEqual(Buffer.from(sigs[0]), Buffer.from(sigs[i]));
94 }
95
96 // Each copy must verify a signature produced by any other copy.
97 for (const verifier of [original, cloned, viaPort, clonedAgain]) {
98 for (const sig of sigs) {
99 assert.strictEqual(
100 await subtle.verify('HMAC', verifier, sig, data), true);
101 }
102 }
103
104 // Exported JWK must match byte-for-byte when extractable.
105 if (original.extractable) {
106 const jwks = await Promise.all(
107 [original, cloned, viaPort, clonedAgain].map(
108 (k) => subtle.exportKey('jwk', k),
109 ),
110 );
111 for (let i = 1; i < jwks.length; i++) {
112 assert.deepStrictEqual(jwks[0], jwks[i]);
113 }
114 } else {
115 // Non-extractable keys must refuse export on every copy.
116 for (const k of [cloned, viaPort, clonedAgain]) {
117 await assert.rejects(subtle.exportKey('jwk', k),
118 { name: 'InvalidAccessError' });
119 }
120 }
121}
122
123async function checkAsymmetricKeyPair({ publicKey, privateKey }) {
124 const data = Buffer.from('payload');

Calls 7

structuredCloneFunction · 0.85
assertSameCryptoKeyFunction · 0.85
allMethod · 0.80
mapMethod · 0.65
fromMethod · 0.45
verifyMethod · 0.45

Tested by

no test coverage detected