<script src="https://cdnjs.cloudflare.com/ajax/libs/sjcl/1.0.6/sjcl.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/rusha/0.8.7/rusha.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/forge/0.9.1/forge.min.js"></script>
var data = new Uint32Array(1024);
window.crypto.getRandomValues(data);
var dataBuffer = new Uint8Array(data);
data = String.fromCharCode.apply(null, dataBuffer);
// src: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
function hex(buffer) {
var hexCodes = [];
var view = new DataView(buffer);
for (var i = 0; i < view.byteLength; i += 4) {
// Using getUint32 reduces the number of iterations needed (we process 4 bytes each time)
var value = view.getUint32(i)
// toString(16) will give the hex representation of the number without padding
var stringValue = value.toString(16)
// We use concatenation and slice for padding
var padding = '00000000'
var paddedValue = (padding + stringValue).slice(-padding.length)
hexCodes.push(paddedValue);
}
// Join all the hex strings into one
return hexCodes.join("");
}
forge.md.sha1.create().update(data).digest()
crypto.subtle.digest("SHA-1", dataBuffer ).then(function (hash) {console.log(hex(hash));});
sjcl.hash.sha1.hash(data);
CryptoJS.SHA1(data);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
forge | |
native | |
sjcl | |
cryptojs |
Test name | Executions per second |
---|---|
forge | 77285.7 Ops/sec |
native | 305089.9 Ops/sec |
sjcl | 0.0 Ops/sec |
cryptojs | 23879.0 Ops/sec |
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, comparing the performance of different libraries and implementations for various cryptographic algorithms.
Benchmark Definition JSON Explanation
The provided JSON defines a benchmark named "sha1-js". The script preparation code generates a random 1024-byte array using the window.crypto.getRandomValues()
function and converts it to a hexadecimal string. The hex
function is used to process the data and convert it into a SHA-1 hash.
Options Compared
The benchmark compares four different libraries/libraries-like implementations:
Pros and Cons of Each Approach
Library Descriptions
Special JS Features/Syntax
The benchmark uses some specialized JavaScript features and syntax:
window.crypto.getRandomValues()
: A function that generates cryptographically secure random numbers.Other Alternatives
If you're looking for alternative libraries or implementations for SHA-1 calculations, some options include:
Keep in mind that these alternatives may not be as performant or feature-rich as the libraries and implementations compared in the benchmark.