Last active
April 12, 2019 19:13
-
-
Save roustem/64357eefe5200a3d5095799d0144b0e7 to your computer and use it in GitHub Desktop.
Compare SJCL and WebCrypto SHA-256
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run on https://start.1password.com to get sjcl | |
function timeSJCL_SHA256(iterations, value) { | |
let timerName = "SJCL SHA-256 (" + value.length + " bytes, " + iterations + " iterations)"; | |
console.time(timerName); | |
for (let i = 0; i < iterations; i++) { | |
sjcl.hash.sha256.hash(value); | |
} | |
console.timeEnd(timerName); | |
} | |
function webcryptoDigest(iterations, timerName, value) { | |
if (iterations <= 0) { | |
console.timeEnd(timerName); | |
return | |
} | |
var buffer = new TextEncoder("utf-8").encode(value); | |
return crypto.subtle.digest("SHA-256", buffer).then(() => { | |
return webcryptoDigest(iterations - 1, timerName, value); | |
}); | |
} | |
function timeWebCryptoSHA256(iterations, value) { | |
let timerName = "WebCrypto SHA-256 (" + value.length + " bytes, " + iterations + " iterations)"; | |
console.time(timerName); | |
return webcryptoDigest(iterations, timerName, value).catch((error) => { | |
console.error(error); | |
}) | |
} | |
// 13-byte value | |
let smallValue = "Hello, World!"; | |
let bigValue = "1234567890"; | |
for (let i = 0; i < 10; i++) { | |
bigValue = bigValue + bigValue; | |
} | |
// running SJCL tests, they are synchrounous | |
timeSJCL_SHA256(100, smallValue); | |
timeSJCL_SHA256(1000, smallValue); | |
timeSJCL_SHA256(100, bigValue); | |
timeSJCL_SHA256(1000, bigValue); | |
// WebCrypto code is async, running sequentially | |
Promise.resolve(1).then(() => { | |
return timeWebCryptoSHA256(100, smallValue); | |
}).then(() => { | |
return timeWebCryptoSHA256(1000, smallValue); | |
}).then(() => { | |
return timeWebCryptoSHA256(100, bigValue); | |
}).then(() => { | |
return timeWebCryptoSHA256(1000, bigValue); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Google Chrome Version 56.0.2924.28 beta (64-bit)