hmac-sign-verify.html 1.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<!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>
ap@apple.com's avatar
ap@apple.com committed
12
description("Test HMAC sign and verify functions.");
13 14 15

jsTestIsAsync = true;

ap@apple.com's avatar
ap@apple.com committed
16
var hmacKey = asciiToUint8Array('a');
17 18 19 20 21 22 23
var extractable = true;

debug("Importing a raw HMAC key from string literal...");
crypto.subtle.importKey("raw", hmacKey, {name: 'hmac', hash: {name: 'sha-1'}}, extractable, ["sign", "verify"]).then(function(result) {
    key = result;
    shouldBe("key.type", "'secret'");
    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 28 29
    shouldBe("key.usages", "['sign', 'verify']");

    debug("Using the key to sign 'foo'...");
30
    return crypto.subtle.sign(key.algorithm, key, asciiToUint8Array('foo'));
31
}).then(function(result) {
ap@apple.com's avatar
ap@apple.com committed
32 33
    signature = result;
    shouldBe("bytesToHexString(new Uint8Array(signature))", "'bebbc02e46b0f81183f40c25dce23e5045d65519'");
34 35

    debug("Verifying the signature...");
36
    return crypto.subtle.verify(key.algorithm, key, result, asciiToUint8Array('foo'));
37 38 39 40 41 42 43 44 45 46
}).then(function(result) {
    verificationResult = result;
    shouldBe("verificationResult", "true");
    finishJSTest();
});
</script>

<script src="../../resources/js-test-post.js"></script>
</body>
</html>