LibWeb: Add happy path test for SubtleCrypto importKey and digest

This commit is contained in:
Andrew Kaster 2024-01-23 11:44:23 -07:00 committed by Andrew Kaster
parent be8489fb04
commit bf32a2027b
Notes: sideshowbarker 2024-07-16 23:34:49 +09:00
2 changed files with 36 additions and 0 deletions

View file

@ -0,0 +1,5 @@
imported key: [object CryptoKey]
imported key.type: secret
imported key.extractable: false
imported key.algorithm: {"name":"PBKDF2"}
SHA-256 digest: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3

View file

@ -0,0 +1,31 @@
<script src="../include.js"></script>
<script>
function bufferToHex(buffer) {
return [...new Uint8Array(buffer)].map(b => b.toString(16).padStart(2, "0")).join("");
}
test(async () => {
let key_material = "password";
let enc = new TextEncoder();
let key = await window.crypto.subtle.importKey(
"raw",
enc.encode(key_material),
{ name: "PBKDF2" },
false,
["deriveBits", "deriveKey"]
);
println(`imported key: ${key}`);
println(`imported key.type: ${key.type}`);
println(`imported key.extractable: ${key.extractable}`);
println(`imported key.algorithm: ${JSON.stringify(key.algorithm)}`);
// FIXME: Implement usages println(`imported key.usages: ${key.usages}`);
let message = "Hello, world!";
let encoded_message = enc.encode(message);
let digest = await window.crypto.subtle.digest("SHA-256", encoded_message);
println(`SHA-256 digest: ${bufferToHex(digest)}`);
});
</script>