Skip to content

Instantly share code, notes, and snippets.

@roustem
Last active April 12, 2019 19:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roustem/64357eefe5200a3d5095799d0144b0e7 to your computer and use it in GitHub Desktop.
Save roustem/64357eefe5200a3d5095799d0144b0e7 to your computer and use it in GitHub Desktop.
Compare SJCL and WebCrypto SHA-256
// 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);
});
@roustem
Copy link
Author

roustem commented Dec 16, 2016

Result in Safari TP Release 19 (Safari 10.1, WebKit 12603.1.14.2)

[Debug] SJCL SHA-256 (13 bytes, 100 iterations): 6.052ms
[Debug] SJCL SHA-256 (13 bytes, 1000 iterations): 10.205ms
[Debug] SJCL SHA-256 (10240 bytes, 100 iterations): 296.837ms
[Debug] SJCL SHA-256 (10240 bytes, 1000 iterations): 1430.852ms

[Debug] WebCrypto SHA-256 (13 bytes, 100 iterations): 8.720ms
[Debug] WebCrypto SHA-256 (13 bytes, 1000 iterations): 64.798ms
[Debug] WebCrypto SHA-256 (10240 bytes, 100 iterations): 19.238ms
[Debug] WebCrypto SHA-256 (10240 bytes, 1000 iterations): 136.688ms

@roustem
Copy link
Author

roustem commented Dec 16, 2016

Google Chrome Version 56.0.2924.28 beta (64-bit)

SJCL SHA-256 (13 bytes, 100 iterations): 6.054ms
SJCL SHA-256 (13 bytes, 1000 iterations): 15.752ms
SJCL SHA-256 (10240 bytes, 100 iterations): 44.766ms
SJCL SHA-256 (10240 bytes, 1000 iterations): 308.431ms

WebCrypto SHA-256 (13 bytes, 100 iterations): 11.289ms
WebCrypto SHA-256 (13 bytes, 1000 iterations): 58.236ms
WebCrypto SHA-256 (10240 bytes, 100 iterations): 21.599ms
WebCrypto SHA-256 (10240 bytes, 1000 iterations): 163.212ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment