| <!DOCTYPE html> |
| <html> |
| <head> |
| <script src="../../resources/js-test-pre.js"></script> |
| <script src="resources/common.js"></script> |
| </head> |
| <body> |
| <p id="description"></p> |
| <div id="console"></div> |
| |
| <script> |
| description("Test importing a JWK key for HMAC."); |
| |
| jsTestIsAsync = true; |
| |
| var extractable = true; |
| |
| var hmacKey = { |
| "kty": "oct", |
| "alg": "HS256", |
| "use": "sig", |
| "ext": false, |
| "k": "ahjkn-_387fgnsibf23qsvahjkn-_387fgnsibf23qs" |
| }; |
| |
| var hmacKeyAsArrayBuffer = asciiToUint8Array(JSON.stringify(hmacKey)); |
| |
| debug("Importing a key...\n"); |
| crypto.subtle.importKey("jwk", hmacKeyAsArrayBuffer, null, extractable, ["sign", "verify"]).then(function(result) { |
| key = result; |
| |
| shouldBe("key.type", "'secret'"); |
| shouldBe("key.extractable", "false"); |
| shouldBe("key.algorithm.name", "'HMAC'"); |
| shouldBe("key.algorithm.length", "32"); |
| shouldBe("key.usages", '["sign", "verify"]'); |
| |
| debug("\nUsing the key to sign message 'foo'..."); |
| return crypto.subtle.sign(key.algorithm, key, asciiToUint8Array('foo')); |
| }).then(function(result) { |
| signature = result; |
| shouldBe("bytesToHexString(new Uint8Array(signature))", "'e03736fe098892b2a2da77812431f7c014d32e2fd69f3bcff883ac923a8fa2da'"); |
| |
| debug("\nVerifying the signature..."); |
| return crypto.subtle.verify(key.algorithm, key, result, asciiToUint8Array('foo')); |
| }).then(function(result) { |
| verificationResult = result; |
| shouldBe("verificationResult", "true"); |
| finishJSTest(); |
| }); |
| </script> |
| |
| <script src="../../resources/js-test-post.js"></script> |
| </body> |
| </html> |