<script src="https://cdnjs.cloudflare.com/ajax/libs/sjcl/1.0.6/sjcl.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/cryptico/0.0.1343522940/cryptico.min.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.sha256.create().update(data).digest()
crypto.subtle.digest("SHA-256", dataBuffer ).then(function (hash) {console.log(hex(hash));});
sjcl.hash.sha256.hash(data);
CryptoJS.SHA256(data);
SHA256(data);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
forge | |
native | |
sjcl | |
crypto | |
cryptico |
Test name | Executions per second |
---|---|
forge | 36113.7 Ops/sec |
native | 31516.2 Ops/sec |
sjcl | 13161.2 Ops/sec |
crypto | 12195.5 Ops/sec |
cryptico | 5819.7 Ops/sec |
I'll do my best to explain the benchmarking process and options compared in this JSON data.
Overview
The provided JSON represents a JavaScript microbenchmarking test, which measures the performance of different libraries for calculating the SHA-256 hash of a given input data. The test consists of four individual benchmark cases: forge
, native
, sjcl
, and crypto
.
Script Preparation Code
The script preparation code is responsible for generating random data for the benchmark. It creates a 1024-element array of unsigned integers (Uint32Array
) using the Web Crypto API's getRandomValues
method, converts it to a binary string (dataBuffer
), and then converts this binary string to a character array (data
) using String.fromCharCode.apply(null, dataBuffer)
.
Benchmark Cases
Each benchmark case uses a different library or implementation to calculate the SHA-256 hash of the prepared data. Here's an overview of each case:
forge.md.sha256.create()
, updates it with the prepared data, and then calls the digest()
method to get the hash.digest
function to calculate the SHA-256 hash directly on the browser's native platform. It takes a binary string (dataBuffer
) as input and passes it to the digest
function with the algorithm parameter set to "SHA-256"
.hash()
method on the SHA-256 instance created by SJCL.SHA256()
function on the prepared data to get the hash.Pros and Cons of Each Approach
Here's a brief summary of the pros and cons of each approach:
Other Considerations
When choosing a hashing library or implementation, consider factors such as:
Alternatives
If you're looking for alternative libraries or implementations for SHA-256 hashing in JavaScript, some popular options include:
In conclusion, this benchmarking test compares the performance of different JavaScript libraries and implementations for calculating the SHA-256 hash. Each approach has its pros and cons, and the choice ultimately depends on your specific requirements and needs.