What is js-sha256?
The js-sha256 npm package is a simple SHA-256 & SHA-224 hash function for JavaScript supports UTF-8 encoding. It can be used in various environments like web browsers and Node.js. It provides functionalities for hashing messages with SHA-256 and SHA-224 algorithms.
What are js-sha256's main functionalities?
SHA-256 Hash
Generates a SHA-256 hash of the input string. This is useful for creating unique identifiers for data, secure password storage, and ensuring data integrity.
sha256('message')
SHA-224 Hash
Generates a SHA-224 hash of the input string. Similar to SHA-256, but produces a shorter hash value. It's used for applications where hash length is a concern but still requires a high level of security.
sha224('message')
HMAC-SHA-256
Generates a HMAC (Hash-based Message Authentication Code) using SHA-256. This is used for verifying both the data integrity and the authenticity of a message.
sha256.hmac('key', 'message')
HMAC-SHA-224
Generates a HMAC using SHA-224. Similar to HMAC-SHA-256, it's used for data integrity and authenticity but with a shorter hash value.
sha224.hmac('key', 'message')
Other packages similar to js-sha256
crypto-js
Crypto-js is a package that provides cryptographic functionalities including SHA-256. It offers a broader range of cryptographic algorithms compared to js-sha256, making it suitable for applications requiring more than just SHA-256 or SHA-224.
sha.js
Sha.js is a streaming SHA hashes module for Node.js that supports SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512. It's more versatile than js-sha256 in terms of the variety of SHA algorithms supported.
js-sha256
A simple SHA-256 / SHA-224 hash function for JavaScript supports UTF-8 encoding.
Demo
SHA256 Online
SHA224 Online
SHA256 File Online
SHA225 File Online
Download
Compress
Uncompress
Installation
You can also install js-sha256 by using Bower.
bower install js-sha256
For node.js, you can use this command to install:
npm install js-sha256
Usage
You could use like this:
sha256('Message to hash');
sha224('Message to hash');
var hash = sha256.create();
hash.update('Message to hash');
hash.hex();
var hash2 = sha256.update('Message to hash');
hash2.update('Message2 to hash');
hash2.array();
sha256.hmac('key', 'Message to hash');
sha224.hmac('key', 'Message to hash');
var hash = sha256.hmac.create('key');
hash.update('Message to hash');
hash.hex();
var hash2 = sha256.hmac.update('key', 'Message to hash');
hash2.update('Message2 to hash');
hash2.array();
Node.js
If you use node.js, you should require the module first:
var sha256 = require('js-sha256');
or
var sha256 = require('js-sha256').sha256;
var sha224 = require('js-sha256').sha224;
or
const { sha256, sha224 } = require('js-sha256');
TypeScript
If you use TypeScript, you can import like this:
import { sha256, sha224 } from 'js-sha256';
RequireJS
It supports AMD:
require(['your/path/sha256.js'], function(sha256) {
});
Example
sha256('');
sha256('The quick brown fox jumps over the lazy dog');
sha256('The quick brown fox jumps over the lazy dog.');
sha224('');
sha224('The quick brown fox jumps over the lazy dog');
sha224('The quick brown fox jumps over the lazy dog.');
sha256('中文');
sha224('中文');
sha256([]);
sha256(new Uint8Array([211, 212]));
sha256('');
sha256.hex('');
sha256.array('');
sha256.digest('');
sha256.arrayBuffer('');
Benchmark
jsPerf Benchmark
License
The project is released under the MIT license.
Contact
The project's website is located at https://github.com/emn178/js-sha256
Author: Chen, Yi-Cyuan (emn178@gmail.com)