422d7670a248ec36b039845b84d0752e92e94477
[WebKit-https.git] / LayoutTests / crypto / subtle / hmac-sign-verify.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../resources/js-test-pre.js"></script>
5 <script src="resources/common.js"></script>
6 </head>
7 <body>
8 <p id="description"></p>
9 <div id="console"></div>
10
11 <script>
12 description("Test HMAC sign and verify functions.");
13
14 jsTestIsAsync = true;
15
16 var hmacKey = asciiToUint8Array('a');
17 var extractable = true;
18
19 debug("Importing a raw HMAC key from string literal...");
20 crypto.subtle.importKey("raw", hmacKey, {name: 'hmac', hash: {name: 'sha-1'}}, extractable, ["sign", "verify"]).then(function(result) {
21     key = result;
22     shouldBe("key.type", "'secret'");
23     shouldBe("key.extractable", "true");
24     shouldBe("key.algorithm.name", "'hmac'");
25     shouldBe("key.algorithm.length", "1"); // See <https://www.w3.org/Bugs/Public/show_bug.cgi?id=23098>.
26     shouldBe("key.algorithm.hash.name", "'sha-1'");
27     shouldBe("key.usages", "['sign', 'verify']");
28
29     debug("Using the key to sign 'foo'...");
30     return crypto.subtle.sign(key.algorithm, key, [asciiToUint8Array('foo')]);
31 }).then(function(result) {
32     signature = result;
33     shouldBe("bytesToHexString(new Uint8Array(signature))", "'bebbc02e46b0f81183f40c25dce23e5045d65519'");
34
35     debug("Verifying the signature...");
36     return crypto.subtle.verify(key.algorithm, key, result, [asciiToUint8Array('foo')]);
37 }).then(function(result) {
38     verificationResult = result;
39     shouldBe("verificationResult", "true");
40     finishJSTest();
41 });
42 </script>
43
44 <script src="../../resources/js-test-post.js"></script>
45 </body>
46 </html>