What is sha?
The 'sha' npm package is used for generating SHA (Secure Hash Algorithm) hashes. It supports various SHA algorithms like SHA-1, SHA-256, SHA-384, and SHA-512. This package is useful for creating cryptographic hashes of data, which can be used for data integrity checks, password hashing, and more.
What are sha's main functionalities?
Generate SHA-1 Hash
This feature allows you to generate a SHA-1 hash of a given input string. The code sample demonstrates how to create a SHA-1 hash of the string 'Hello World' and output it in hexadecimal format.
const sha = require('sha');
const hash = sha('sha1').update('Hello World').digest('hex');
console.log(hash);
Generate SHA-256 Hash
This feature allows you to generate a SHA-256 hash of a given input string. The code sample demonstrates how to create a SHA-256 hash of the string 'Hello World' and output it in hexadecimal format.
const sha = require('sha');
const hash = sha('sha256').update('Hello World').digest('hex');
console.log(hash);
Generate SHA-384 Hash
This feature allows you to generate a SHA-384 hash of a given input string. The code sample demonstrates how to create a SHA-384 hash of the string 'Hello World' and output it in hexadecimal format.
const sha = require('sha');
const hash = sha('sha384').update('Hello World').digest('hex');
console.log(hash);
Generate SHA-512 Hash
This feature allows you to generate a SHA-512 hash of a given input string. The code sample demonstrates how to create a SHA-512 hash of the string 'Hello World' and output it in hexadecimal format.
const sha = require('sha');
const hash = sha('sha512').update('Hello World').digest('hex');
console.log(hash);
Other packages similar to sha
crypto
The 'crypto' module is a built-in Node.js module that provides cryptographic functionality, including a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions. It is more comprehensive and versatile compared to 'sha', supporting a wider range of cryptographic operations.
hash.js
The 'hash.js' package is a JavaScript library that provides a simple way to create hash digests of data. It supports various hash algorithms including SHA-1, SHA-256, SHA-384, and SHA-512. It is similar to 'sha' but offers a more modular approach and additional features like HMAC.
js-sha256
The 'js-sha256' package is a JavaScript implementation of the SHA-256 hash function. It is lightweight and fast, making it suitable for client-side applications. Unlike 'sha', it focuses solely on SHA-256, providing a highly optimized implementation for this specific algorithm.
sha
Check and get file hashes (using any algorithm)
Installation
$ npm install sha
API
check(fileName, expected, [options,] cb) / checkSync(filename, expected, [options])
Asynchronously check that fileName
has a "hash" of expected
. The callback will be called with either null
or an error (indicating that they did not match).
Options:
- algorithm: defaults to
sha1
and can be any of the algorithms supported by crypto.createHash
get(fileName, [options,] cb) / getSync(filename, [options])
Asynchronously get the "hash" of fileName
. The callback will be called with an optional error
object and the (lower cased) hex digest of the hash.
Options:
- algorithm: defaults to
sha1
and can be any of the algorithms supported by crypto.createHash
stream(expected, [options])
Check the hash of a stream without ever buffering it. This is a pass through stream so you can do things like:
fs.createReadStream('src')
.pipe(sha.stream('expected'))
.pipe(fs.createWriteStream('dest'))
dest
will be a complete copy of src
and an error will be emitted if the hash did not match 'expected'
.
Options:
- algorithm: defaults to
sha1
and can be any of the algorithms supported by crypto.createHash
License
You may use this software under the BSD or MIT. Take your pick. If you want me to release it under another license, open a pull request.