mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-27 02:05:07 +02:00
LibWeb: Implement PBKDF2 deriveBits for SubtleCrypto
This commit is contained in:
Notes:
sideshowbarker
2024-07-17 11:29:41 +09:00
Author: https://github.com/stelar7 Commit: https://github.com/SerenityOS/serenity/commit/19bb62d60e Pull-request: https://github.com/SerenityOS/serenity/pull/23834 Reviewed-by: https://github.com/ADKaster ✅
54
Tests/LibWeb/Text/input/Crypto/SubtleCrypto-deriveBits.html
Normal file
54
Tests/LibWeb/Text/input/Crypto/SubtleCrypto-deriveBits.html
Normal file
@@ -0,0 +1,54 @@
|
||||
<script src="../include.js"></script>
|
||||
<script>
|
||||
asyncTest(async done => {
|
||||
const encoder = new TextEncoder();
|
||||
const message = "Hello friends";
|
||||
const encodedMessage = encoder.encode(message);
|
||||
|
||||
const format = "raw";
|
||||
const keyData = encodedMessage;
|
||||
const importAlgorithm = {
|
||||
name: "PBKDF2",
|
||||
};
|
||||
const extractable = false;
|
||||
const keyUsages = ["deriveBits", "deriveKey"];
|
||||
const keyMaterial = await window.crypto.subtle.importKey(
|
||||
format,
|
||||
keyData,
|
||||
importAlgorithm,
|
||||
extractable,
|
||||
keyUsages
|
||||
);
|
||||
|
||||
const salt = encodedMessage;
|
||||
const iterations = 100000;
|
||||
const hash = "SHA-256";
|
||||
const derivationAlgorithm = {
|
||||
name: "PBKDF2",
|
||||
salt,
|
||||
iterations,
|
||||
hash,
|
||||
};
|
||||
const length = 256;
|
||||
const derivedBits = await window.crypto.subtle.deriveBits(
|
||||
derivationAlgorithm,
|
||||
keyMaterial,
|
||||
length
|
||||
);
|
||||
|
||||
function arrayBufferToBase64(buffer) {
|
||||
return btoa(String.fromCharCode.apply(null, new Uint8Array(buffer)));
|
||||
}
|
||||
|
||||
const expectedResult = "WHYex7U9vTCisuffYpbNHR+Gbto/edldB+P+WB6RNIg=";
|
||||
const actualResult = arrayBufferToBase64(derivedBits);
|
||||
|
||||
if (expectedResult == actualResult) {
|
||||
println("Derived bits OK");
|
||||
} else {
|
||||
println("Derived bits FAIL");
|
||||
}
|
||||
|
||||
done();
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user