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); | |
}); |
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
Result in Safari TP Release 19 (Safari 10.1, WebKit 12603.1.14.2)