What is js-sha3?
The js-sha3 npm package is a JavaScript implementation of the SHA3 (Secure Hash Algorithm 3) cryptographic hash function. It provides a simple interface to compute hashes using the SHA3 family of hash functions, including SHA3-224, SHA3-256, SHA3-384, and SHA3-512, as well as the SHAKE128 and SHAKE256 extendable-output functions.
What are js-sha3's main functionalities?
SHA3-256 Hash
This feature allows you to compute a SHA3-256 hash of a given input string. The code sample demonstrates how to import the sha3_256 function from the js-sha3 package and use it to hash the string 'message'.
"use strict"; const { sha3_256 } = require('js-sha3'); const hash = sha3_256('message'); console.log(hash);
SHA3-512 Hash
This feature allows you to compute a SHA3-512 hash of a given input string. The code sample demonstrates how to import the sha3_512 function from the js-sha3 package and use it to hash the string 'message'.
"use strict"; const { sha3_512 } = require('js-sha3'); const hash = sha3_512('message'); console.log(hash);
SHAKE128 Hash
This feature allows you to compute a SHAKE128 hash of a given input string with a specified output length. The code sample demonstrates how to import the shake128 function from the js-sha3 package and use it to hash the string 'message' with an output length of 256 bits.
"use strict"; const { shake128 } = require('js-sha3'); const hash = shake128('message', 256); console.log(hash);
SHAKE256 Hash
This feature allows you to compute a SHAKE256 hash of a given input string with a specified output length. The code sample demonstrates how to import the shake256 function from the js-sha3 package and use it to hash the string 'message' with an output length of 512 bits.
"use strict"; const { shake256 } = require('js-sha3'); const hash = shake256('message', 512); console.log(hash);
Other packages similar to js-sha3
sha3
The 'sha3' package is another implementation of the SHA3 hash function in JavaScript. It provides similar functionality to js-sha3 but may have different API interfaces and performance characteristics.
crypto-js
Crypto-js is a collection of cryptography algorithms implemented in JavaScript. It includes SHA3 among many other cryptographic functions. It is more comprehensive than js-sha3 but might be larger in size and potentially slower due to its broader scope.
keccak
The 'keccak' package is a JavaScript implementation of the Keccak hash functions, which are the basis for the SHA3 standard. It offers similar hashing capabilities and is focused specifically on the Keccak family of hashes.
js-sha3
A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.
Notice
- v0.8.0+ will throw an error if try to update hash after finalize.
- Sha3 methods has been renamed to keccak since v0.2.0. It means that sha3 methods of v0.1.x are equal to keccak methods of v0.2.x and later.
buffer
method is deprecated. This maybe confuse with Buffer in node.js. Please use arrayBuffer
instead.
Demo
SHA3-512 Online
SHA3-384 Online
SHA3-256 Online
SHA3-224 Online
Keccak-512 Online
Keccak-384 Online
Keccak-256 Online
Keccak-224 Online
Shake128 Online
Shake256 Online
Download
Compress
Uncompress
Installation
You can also install js-sha3 by using Bower.
bower install js-sha3
For node.js, you can use this command to install:
npm install js-sha3
Usage
You could use like this:
sha3_512('Message to hash');
sha3_384('Message to hash');
sha3_256('Message to hash');
sha3_224('Message to hash');
keccak512('Message to hash');
keccak384('Message to hash');
keccak256('Message to hash');
keccak224('Message to hash');
shake128('Message to hash', 256);
shake256('Message to hash', 512);
cshake128('Message to hash', 256, 'function name', 'customization');
cshake256('Message to hash', 512, 'function name', 'customization');
kmac128('key', 'Message to hash', 256, 'customization');
kmac256('key', 'Message to hash', 512, 'customization');
var arrayBuffer = keccak224.arrayBuffer('Message to hash');
var bytes = keccak224.digest('Message to hash');
var bytes = keccak224.array('Message to hash');
sha3_512.update('Message ').update('to ').update('hash').hex();
shake128.update('Message ', 256).update('to ').update('hash').hex();
var hash = sha3_512.create();
hash.update('...');
hash.update('...');
hash.hex();
var hash = shake128.create(256);
hash.update('...');
hash.update('...');
hash.hex();
var hash = cshake128.create(256, 'function name', 'customization');
var hash = kmac128.create('key', 256, 'customization');
Node.js
If you use node.js, you should require the module first:
const {
sha3_512,
sha3_384,
sha3_256,
sha3_224,
keccak512,
keccak384,
keccak256,
keccak224,
shake128,
shake256,
cshake128,
cshake256,
kmac128,
kmac25
} = require('js-sha3');
TypeScript
If you use TypeScript, you can import like this:
import {
sha3_512,
sha3_384,
sha3_256,
sha3_224,
keccak512,
keccak384,
keccak256,
keccak224,
shake128,
shake256,
cshake128,
cshake256,
kmac128,
kmac256
} from 'js-sha3';
Example
Code
sha3_512('');
sha3_512('The quick brown fox jumps over the lazy dog');
sha3_512('The quick brown fox jumps over the lazy dog.');
sha3_384('');
sha3_384('The quick brown fox jumps over the lazy dog');
sha3_384('The quick brown fox jumps over the lazy dog.');
sha3_256('');
sha3_256('The quick brown fox jumps over the lazy dog');
sha3_256('The quick brown fox jumps over the lazy dog.');
sha3_224('');
sha3_224('The quick brown fox jumps over the lazy dog');
sha3_224('The quick brown fox jumps over the lazy dog.');
keccak512('');
keccak512('The quick brown fox jumps over the lazy dog');
keccak512('The quick brown fox jumps over the lazy dog.');
keccak384('');
keccak384('The quick brown fox jumps over the lazy dog');
keccak384('The quick brown fox jumps over the lazy dog.');
keccak256('');
keccak256('The quick brown fox jumps over the lazy dog');
keccak256('The quick brown fox jumps over the lazy dog.');
keccak224('');
keccak224('The quick brown fox jumps over the lazy dog');
keccak224('The quick brown fox jumps over the lazy dog.');
shake128('', 256);
shake256('', 512);
It also supports UTF-8 encoding:
Code
sha3_512('中文');
sha3_384('中文');
sha3_256('中文');
sha3_224('中文');
keccak512('中文');
keccak384('中文');
keccak256('中文');
keccak224('中文');
It also supports byte Array
, Uint8Array
, ArrayBuffer
input:
Code
sha3_512([]);
sha3_512(new Uint8Array([]));
Benchmark
UTF8
ASCII
License
The project is released under the MIT license.
Contact
The project's website is located at https://github.com/emn178/js-sha3
Author: Chen, Yi-Cyuan (emn178@gmail.com)