diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2021-07-06 17:46:04 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-06 14:16:04 +0200 |
commit | 570309d795d2e2a00fc2523ca80818b275b567e6 (patch) | |
tree | d9596da5c8762c8742b872d4ebc95d97414e32d4 /cli/tests | |
parent | 2ed222fceb2a2f3f2dfaeff87953e3b599e18641 (diff) |
feat(crypto): implement generateKey() and sign() (#9614)
Co-authored-by: Luca Casonato <hello@lcas.dev>
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Diffstat (limited to 'cli/tests')
-rw-r--r-- | cli/tests/unit/webcrypto_test.ts | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/cli/tests/unit/webcrypto_test.ts b/cli/tests/unit/webcrypto_test.ts new file mode 100644 index 000000000..4361d3a10 --- /dev/null +++ b/cli/tests/unit/webcrypto_test.ts @@ -0,0 +1,58 @@ +import { assert, assertEquals, unitTest } from "./test_util.ts"; + +unitTest(async function testGenerateRSAKey() { + const subtle = window.crypto.subtle; + assert(subtle); + + const keyPair = await subtle.generateKey( + { + name: "RSA-PSS", + modulusLength: 2048, + publicExponent: new Uint8Array([1, 0, 1]), + hash: "SHA-256", + }, + true, + ["sign", "verify"], + ); + + assert(keyPair.privateKey); + assert(keyPair.publicKey); + assertEquals(keyPair.privateKey.extractable, true); + assert(keyPair.privateKey.usages.includes("sign")); +}); + +unitTest(async function testGenerateHMACKey() { + const key = await window.crypto.subtle.generateKey( + { + name: "HMAC", + hash: "SHA-512", + }, + true, + ["sign", "verify"], + ); + + assert(key); + assertEquals(key.extractable, true); + assert(key.usages.includes("sign")); +}); + +unitTest(async function testSignECDSA() { + const key = await window.crypto.subtle.generateKey( + { + name: "ECDSA", + namedCurve: "P-384", + }, + true, + ["sign", "verify"], + ); + + const encoder = new TextEncoder(); + const encoded = encoder.encode("Hello, World!"); + const signature = await window.crypto.subtle.sign( + { name: "ECDSA", hash: "SHA-384" }, + key.privateKey, + encoded, + ); + + assert(signature); +}); |