sha224
v1.0.0
Published
A Javascript implementation of SHA-224 for Node.js.
Downloads
341
Maintainers
Readme
A Node.js module for SHA-224
This Javascript module implements the SHA-224 cryptographic hash function designed by the United States National Security Agency (NSA). This module is a wrapper of the sha2
module.
Installation ⤓
npm install sha224
Usage
Importing
Import the module with
const SHA224 = require("sha224");
Available input types
It basically takes a Buffer
. Everything other than a Buffer
as its input turns into a Buffer
with Buffer.from()
internally. Reading these would help you understand it:
Buffer.from(string[, encoding])
Buffer.from(arrayBuffer[, byteOffset[, length]])
Buffer.from(array)
Buffer.from(object[, offsetOrEncoding[, length]])
// SHA-224
const SHA224 = require("sha224");
// Input: "Green chá" in UTF-8.
console.log(SHA224("Green chá"));
console.log(SHA224("Green chá", "utf8"));
console.log(SHA224("477265656e206368c3A1", "hex"));
console.log(SHA224("R3JlZW4gY2jDoQ==", "base64"));
console.log(SHA224([
0x47, 0x72, 0x65, 0x65, 0x6E, 0x20, 0x63, 0x68, 0xC3, 0xA1
]));
// All of them give `<Buffer 09 11 cc 3f 17 06 19 1a de 7b
// cb ad fa 95 14 28 f6 09 ca a2 f1 76 e5 f7 e4 fe d6 e7>`.
// Input: 0xC0FFEE.
console.log(SHA224(Buffer.from([0xC0, 0xFF, 0xEE])));
console.log(SHA224((new Uint8Array([0xC0, 0xFF, 0xEE])).buffer));
console.log(SHA224(
(new Uint8Array([0xC0, 0x01, 0xC0, 0xFF, 0xEE])).buffer,
2
));
console.log(SHA224("C0ffee", "hex"));
console.log(SHA224("wP/u", "base64"));
console.log(SHA224([0xC0, 0xFF, 0xEE]));
// All of them give `<Buffer 26 fb 46 cb 82 2b a8 2f 43 33
// 9c b2 47 ec d1 11 77 07 83 f5 72 a9 b9 a5 cf 34 cd 46>`.
Working with the outputs
It returns a Buffer
, so you may do what you may do with a Buffer
.
// SHA-224
const SHA224 = require("sha224");
const nyanbuffer = SHA224(`
░░░░░░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░░░░░
░░░░░░█░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░█░░░░░
░░░░░░█░█░▀░░░░░▀░░▀░░░░█░█░░░░░
░░░░░░█░█░░░░░░░░▄▀▀▄░▀░█░█▄▀▀▄░
█▀▀█▄░█░█░░▀░░░░░█░░░▀▄▄█▄▀░░░█░
▀▄▄░▀██░█▄░▀░░░▄▄▀░░░░░░░░░░░░▀▄
░░▀█▄▄█░█░░░░▄░░█░░░▄█░░░▄░▄█░░█
░░░░░▀█░▀▄▀░░░░░█░██░▄░░▄░░▄░███
░░░░░▄█▄░░▀▀▀▀▀▀▀▀▄░░▀▀▀▀▀▀▀░▄▀░
░░░░█░░▄█▀█▀▀█▀▀▀▀▀▀█▀▀█▀█▀▀█░░░
░░░░▀▀▀▀░░▀▀▀░░░░░░░░▀▀▀░░▀▀░░░░
`);
console.log(nyanbuffer);
// <Buffer e9 2b e7 21 c9 22 10 0b c8 81 65 2a 63
// 11 c4 f9 29 03 76 68 05 e1 7f 1b d2 af 31 34>
console.log(nyanbuffer.toString("hex"));
// "e92be721c922100bc881652a6311c4f92903766805e17f1bd2af3134"
console.log(nyanbuffer.toString("base64"));
// "6SvnIckiEAvIgWUqYxHE+SkDdmgF4X8b0q8xNA=="
console.log(Array.from(nyanbuffer));
// [233, 43, 231, 33, 201, 34, 16, 11, 200, 129, 101, 42, 99, 17,
// 196, 249, 41, 3, 118, 104, 5, 225, 127, 27, 210, 175, 49, 52]
console.log(nyanbuffer.equals(nyanbuffer));
// true
Warning ⚠️
Hashing passwords
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)
Hashing huge data
This module is not appropriate for hashing huge binary data, such as that of a 1 GB file.
Specification reference 📖
Request for Comments #6234(RFC 6234) ‘US Secure Hash Algorithms (SHA and SHA-based HMAC and HKDF)’
- §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.
Federal Information Processing Standards Publication 180-4(FIPS PUB 180-4) ‘Secure Hash Standard (SHS)’
- §5.3.6: SHA-512/t.
published by National Institute of Standards and Technology (NIST) in August 2015.
License 📜
This Javascript module has been licensed under the MIT license.