A Javascript implementation of the SHA-2 cryptographic hash function family.
npm install sha2 This Javascript module implements the SHA-2 (Secure Hash Algorithm 2) cryptographic hash function family designed by _the United States National Security Agency (NSA)_ ― SHA-224, SHA-256, SHA-384, SHA-512, and SHA-512/t (including SHA-512/224 and SHA-512/256).
It makes use of the crypto standard built-in module if the module is available, otherwise, an alternative Javascript implementation for SHA-2 that I developed is used.
```
npm install sha2
Note that this module works with Buffers. As for web browsers that don't have Buffers, you may use _SHA2.js for web browsers_ instead.
Import the module with
`javascript`
const SHA2 = require("sha2");
or if you want to take its methods only,
`javascript`
const {SHA256, SHA384} = require("sha2");
Choose your preferred naming convention!
`javascript
const SHA2 = require("sha2");
const input = "Was touwaka ga knawa murfanare yor.";
// SHA-224
console.log(SHA2"SHA-224");
console.log(SHA2.SHA_224(input));
console.log(SHA2.SHA224(input));
console.log(SHA2"sha-224");
console.log(SHA2.sha_224(input));
console.log(SHA2.sha224(input));
// All of them give .
// SHA-512/80
console.log(SHA2"SHA-512/t");
console.log(SHA2.SHA_512_t(80, input));
console.log(SHA2.SHA512_t(80, input));
console.log(SHA2.SHA_512t(80, input));
console.log(SHA2.SHA512t(80, input));
console.log(SHA2"sha-512/t");
console.log(SHA2.sha_512_t(80, input));
console.log(SHA2.sha512_t(80, input));
console.log(SHA2.sha_512t(80, input));
console.log(SHA2.sha512t(80, input));
// All of them give .`
They basically take a Buffer. Everything other than a Buffer as their input turns into a Buffer with Buffer.from() internally. Reading these would help you understand it:
- [Buffer.from(string[, encoding])](https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_string_encoding)Buffer.from(arrayBuffer[, byteOffset[, length]])
- [](https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_arraybuffer_byteoffset_length)Buffer.from(array)
- Buffer.from(object[, offsetOrEncoding[, length]])
- [](https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_object_offsetorencoding_length)
`javascript
// SHA-384
const {SHA384} = require("sha2");
// Input: "Green chá" in UTF-8.
console.log(SHA384("Green chá"));
console.log(SHA384("Green chá", "utf8"));
console.log(SHA384("477265656e206368c3A1", "hex"));
console.log(SHA384("R3JlZW4gY2jDoQ==", "base64"));
console.log(SHA384([
0x47, 0x72, 0x65, 0x65, 0x6E, 0x20, 0x63, 0x68, 0xC3, 0xA1
]));
// All of them give
// 53 13 e5 9e 7b 74 84 68 c9 ba ab 37 8f 79 5b 03 42 85 c2>.
// Input: 0xC0FFEE.
console.log(SHA384(Buffer.from([0xC0, 0xFF, 0xEE])));
console.log(SHA384((new Uint8Array([0xC0, 0xFF, 0xEE])).buffer));
console.log(SHA384(
(new Uint8Array([0xC0, 0x01, 0xC0, 0xFF, 0xEE])).buffer,
2
));
console.log(SHA384("C0ffee", "hex"));
console.log(SHA384("wP/u", "base64"));
console.log(SHA384([0xC0, 0xFF, 0xEE]));
// All of them give
// 32 b0 da 91 13 29 48 8b 85 71 22 4e 42 45 25 0b 62 ba 86>.`
They return a Buffer, so you can do what you can do with a Buffer.
`javascript
// SHA-256
const {SHA256} = require("sha2");
const nyanbuffer = SHA256(
░░░░░░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░░░░░
░░░░░░█░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░█░░░░░
░░░░░░█░█░▀░░░░░▀░░▀░░░░█░█░░░░░
░░░░░░█░█░░░░░░░░▄▀▀▄░▀░█░█▄▀▀▄░
█▀▀█▄░█░█░░▀░░░░░█░░░▀▄▄█▄▀░░░█░
▀▄▄░▀██░█▄░▀░░░▄▄▀░░░░░░░░░░░░▀▄
░░▀█▄▄█░█░░░░▄░░█░░░▄█░░░▄░▄█░░█
░░░░░▀█░▀▄▀░░░░░█░██░▄░░▄░░▄░███
░░░░░▄█▄░░▀▀▀▀▀▀▀▀▄░░▀▀▀▀▀▀▀░▄▀░
░░░░█░░▄█▀█▀▀█▀▀▀▀▀▀█▀▀█▀█▀▀█░░░
░░░░▀▀▀▀░░▀▀▀░░░░░░░░▀▀▀░░▀▀░░░░);
//
console.log(nyanbuffer.toString("hex"));
// "917af159e6a87a2b3d3cd16cd5f1a789b7ec900775add052167305d397e985f2"
console.log(nyanbuffer.toString("base64"));
// "kXrxWeaoeis9PNFs1fGnibfskAd1rdBSFnMF05fphfI="
console.log(Array.from(nyanbuffer));
// [145, 122, 241, 89, 230, 168, 122, 43, 61, 60, 209,
// 108, 213, 241, 167, 137, 183, 236, 144, 7, 117, 173,
// 208, 82, 22, 115, 5, 211, 151, 233, 133, 242]
console.log(nyanbuffer.equals(nyanbuffer));
// true
`
- For SHA-224
- SHA2"SHA-224"SHA2.SHA_224(......)
- SHA2.SHA224(......)
- SHA2"sha-224"
- SHA2.sha_224(......)
- SHA2.sha224(......)
- SHA2"SHA-256"
- For SHA-256
- SHA2"SHA-384"
- And so on.
- For SHA-384
- SHA2"SHA-512"
- And so on.
- For SHA-512
- SHA2"SHA-512/t"
- And so on.
- For SHA-512/t (_t_ must satisfy 1 ≤ _t_ ≤ 511 and _t_ ≠ 384. And only _t_ that is a multiple of 8 is supported.)
- SHA2.SHA_512_t(t, ......)
- SHA2.SHA512_t(t, ......)
- SHA2.SHA_512t(t, ......)
- SHA2.SHA512t(t, ......)
- SHA2"sha-512/t"
- SHA2.sha_512_t(t, ......)
- SHA2.sha512_t(t, ......)
- SHA2.sha_512t(t, ......)
- SHA2.sha512t(t, ......)
- SHA2"SHA-512/224"
- For SHA-512/224
- SHA2"SHA-512/256"
- And so on.
- You may also use the method for the SHA-512/t.
- For SHA-512/256
-
- And so on.
- You may also use the method for the SHA-512/t.
Making a hash of a password with one of the algorithms of the SHA-2 family and keeping it, is not recommended.
For that purpose, use slow hash functions which are slow by design such as PBKDF2, bcrypt, and scrypt, instead.
- How to securely hash passwords? (_Information security Stack Exchange_)
- Salted Password Hashing - Doing it Right (_CrackStation_)
- How To Safely Store A Password (_Coda Hale_)
This module is not appropriate for hashing huge binary data, such as that of a 1 GB file.
- §1: Overview of Contents.
- §2: Notation for Bit Strings and Integers.
- §3: Operations on Words.
- §4: Message Padding and Parsing.
- §5: Functions and Constants Used.
- §6: Computing the Message Digest.
written by _Donald E. Eastlake 3rd_, and _Tony Hansen_ in May 2011.
- §5.3.6: SHA-512/t.
published by _National Institute of Standards and Technology (NIST)_ in August 2015.
This Javascript module has been licensed under the MIT license:
`javascript
// 80
////////////////////////////////////////////////////////////////////////////////
/*
SHA2.js
(A Javascript implementation of
the United States of America (USA)
Federal Information Processing Standard (FIPS)
Secure Hash Algorithm 2 (SHA-2))
developed
by K. (https://github.com/wlzla000)
on January 16-22 and 26, 2018,
licensed under
the MIT license
Copyright (c) 2018 K.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
////////////////////////////////////////////////////////////////////////////////
/*
SHA2-Node.js
(A Node.js module implementation of
the United States of America (USA)
Federal Information Processing Standard (FIPS)
Secure Hash Algorithm 2 (SHA-2))
developed
by K. (https://github.com/wlzla000)
on January 23-26, 2018,
licensed under
the MIT license
Copyright (c) 2018 K.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
`
, and its dependency crypto` also has the MIT license.
Thank you for providing a great piece of music for programming,
_Nick Kaelar_ and _Team Salvato_!