What is jssha?
jsSHA is a JavaScript library that provides various cryptographic hash functions and HMAC (Hash-based Message Authentication Code) functionalities. It supports a wide range of hash algorithms including SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA3-224, SHA3-256, SHA3-384, SHA3-512, and SHAKE128/256.
What are jssha's main functionalities?
Hashing
This feature allows you to create a hash of a given input text using various algorithms. In this example, SHA-256 is used to hash the input text 'This is my input text' and the result is output in hexadecimal format.
const jsSHA = require('jssha');
const shaObj = new jsSHA('SHA-256', 'TEXT');
shaObj.update('This is my input text');
const hash = shaObj.getHash('HEX');
console.log(hash);
HMAC
This feature allows you to create an HMAC using a specified key and input text. In this example, SHA-256 is used to create an HMAC for the input text 'This is my input text' with the key 'my-secret-key', and the result is output in hexadecimal format.
const jsSHA = require('jssha');
const shaObj = new jsSHA('SHA-256', 'TEXT');
shaObj.setHMACKey('my-secret-key', 'TEXT');
shaObj.update('This is my input text');
const hmac = shaObj.getHMAC('HEX');
console.log(hmac);
SHAKE
This feature allows you to use the SHAKE (Secure Hash Algorithm Keccak) function, which is a variable-length hash function. In this example, SHAKE128 is used with an output length of 256 bits to hash the input text 'This is my input text', and the result is output in hexadecimal format.
const jsSHA = require('jssha');
const shaObj = new jsSHA('SHAKE128', 'TEXT', {shakeLen: 256});
shaObj.update('This is my input text');
const shake = shaObj.getHash('HEX');
console.log(shake);
Other packages similar to jssha
crypto-js
crypto-js is a widely-used library that provides a variety of cryptographic algorithms including MD5, SHA-1, SHA-256, and more. It is similar to jsSHA in terms of hashing capabilities but also includes additional features like encryption and decryption using AES, DES, and other algorithms.
hash.js
hash.js is a library that provides a simple and efficient way to compute hash digests using various algorithms like SHA-1, SHA-256, SHA-512, and more. It is similar to jsSHA but focuses solely on hashing without HMAC or other cryptographic functionalities.
node-forge
node-forge is a comprehensive library that provides a wide range of cryptographic functionalities including hashing, HMAC, encryption, and digital signatures. It is more feature-rich compared to jsSHA, offering additional capabilities like PKI (Public Key Infrastructure) and TLS (Transport Layer Security) support.
jsSHA
A pure JavaScript streaming implementation of the complete Secure Hash Standard
family (SHA-1, SHA-224, SHA3-224, SHA-256, SHA3-256, SHA-384, SHA3-384, SHA-512,
SHA3-512, SHAKE128, and SHAKE256) as well as HMAC.
Usage
Installation
Browser
Include the desired JavaScript file (sha.js, sha1.js, sha256.js, sha512.js, or
sha3.js) in your header (sha.js used below):
<script type="text/javascript" src="/path/to/sha.js"></script>
Node.js
jsSHA is available through NPM and be installed by simply doing
npm install jssha
To use the module, first require it using:
jsSHA = require("jssha");
Hashing
Instantiate a new jsSHA object with the desired hash type, input type, and
options as parameters. The hash type can be one of SHA-1, SHA-224, SHA3-224,
SHA-256, SHA3-256, SHA-384, SHA3-384, SHA-512, SHA3-512, SHAKE128, or SHAKE256.
The input type can be one of HEX, TEXT, B64, BYTES, ARRAYBUFFER, or UINT8ARRAY.
You can then stream in input using the update
object function, calling it
multiple times if needed. Finally, simply call getHash
with the output type
as a parameter (B64, HEX, BYTES, ARRAYBUFFER, or UINT8ARRAY). Example to
calculate the SHA-512 of "This is a test":
var shaObj = new jsSHA("SHA-512", "TEXT");
shaObj.update("This is a ");
shaObj.update("test");
var hash = shaObj.getHash("HEX");
The constructor takes a hashmap as a optional third argument with possible
properties of numRounds
and encoding
. numRounds
controls the number of
hashing iterations/rounds performed and defaults to a value of 1 if not
specified. encoding
specifies the encoding used to encode TEXT-type inputs.
Valid options are "UTF8", "UTF16BE", and "UTF16LE", it defaults to "UTF8".
getHash
also takes a hashmap as an optional second argument. By default the
hashmap is {"outputUpper" : false, "b64Pad" : "="}
. These options are
intelligently interpreted based upon the chosen output format. Important:
SHAKE128 and SHAKE256 require shakeLen
to be included in the hashmap where
shakeLen
is the desired output length of the SHAKE algorithm in a multiple
of 8 bits.
HMAC
Instantiate a new jsSHA object the same way as for hashing. Then set the HMAC
key to be used by calling setHMACKey
with the key and its input type (this
MUST be done before calling update). You can stream in the input using the
update
object function just like hashing. Finally, get the HMAC by calling
the getHMAC
function with the output type as its argument. Example to
calculate the SHA-512 HMAC of the string "This is a test" with the key "abc":
var shaObj = new jsSHA("SHA-512", "TEXT");
shaObj.setHMACKey("abc", "TEXT");
shaObj.update("This is a ");
shaObj.update("test");
var hmac = shaObj.getHMAC("HEX");
setHMACKey
takes the same input types as the constructor and getHMAC
takes the
same inputs as getHash
as described above.
Note: You cannot calculate both the hash and HMAC using the same object.
Files
src/sha_dev.js
A commented implementation of the entire SHA family of hashes. Not to be used
in production.
src/sha.js
A Google Closure Compiler optimized version of the entire library.
src/sha1.js
A Google Closure Compiler optimized version the library with non SHA-1
functionality removed.
src/sha256.js
A Google Closure Compiler optimized version the library with non SHA-224/SHA-256
functionality removed.
src/sha3.js
A Google Closure Compiler optimized version the library with non SHA-3
functionality removed.
src/sha512.js
A Google Closure Compiler optimized version the library with non SHA-384/SHA-512
functionality removed.
Compiling
This library makes use of the Google Closure Compiler
to both boost performance and reduce filesizes. To compile sha_dev.js into a customized output file,
use a command like the following:
java -jar compiler.jar --define="SUPPORTED_ALGS=<FLAG>" \
--externs /path/to/build/externs.js --warning_level VERBOSE \
--compilation_level ADVANCED_OPTIMIZATIONS \
--js /path/to/sha_dev.js --js_output_file /path/to/sha.js
where FLAG is a bitwise OR of the following values:
- 8 for SHA3
- 4 for SHA-384/SHA-512
- 2 for SHA-224/256
- 1 for SHA-1
Contact Info
The project's website is located at https://caligatio.github.com/jsSHA/