1.0.0 • Published 6 years ago

sha384 v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

A Node.js module for SHA-384

This Javascript module implements the SHA-384 cryptographic hash function designed by the United States National Security Agency (NSA). This module is a wrapper of the sha2 module.

Installation ⤓

npm install sha384

Usage

Importing

Import the module with

const SHA384 = require("sha384");

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:

// SHA-384
const SHA384 = require("sha384");

// 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 `<Buffer 63 f2 63 3f f2 bc 1e 24 77 76
// 12 9b 76 97 66 0a 70 13 34 7f 8b ad e2 e5 c1 2c 5a e8 e0
// 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 `<Buffer 01 1f 36 0d b6 36 cf a4 c7 a6
// 17 68 ad 91 7f e3 d9 5a 6b d8 8a 79 68 ce 43 7b 00 b6 3a
// 32 b0 da 91 13 29 48 8b 85 71 22 4e 42 45 25 0b 62 ba 86>`.

Working with the outputs

It returns a Buffer, so you may do what you may do with a Buffer.

// SHA-384
const SHA384 = require("sha384");

const nyanbuffer = SHA384(`
░░░░░░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░░░░░
░░░░░░█░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░█░░░░░
░░░░░░█░█░▀░░░░░▀░░▀░░░░█░█░░░░░
░░░░░░█░█░░░░░░░░▄▀▀▄░▀░█░█▄▀▀▄░
█▀▀█▄░█░█░░▀░░░░░█░░░▀▄▄█▄▀░░░█░
▀▄▄░▀██░█▄░▀░░░▄▄▀░░░░░░░░░░░░▀▄
░░▀█▄▄█░█░░░░▄░░█░░░▄█░░░▄░▄█░░█
░░░░░▀█░▀▄▀░░░░░█░██░▄░░▄░░▄░███
░░░░░▄█▄░░▀▀▀▀▀▀▀▀▄░░▀▀▀▀▀▀▀░▄▀░
░░░░█░░▄█▀█▀▀█▀▀▀▀▀▀█▀▀█▀█▀▀█░░░
░░░░▀▀▀▀░░▀▀▀░░░░░░░░▀▀▀░░▀▀░░░░
`);
console.log(nyanbuffer);
// <Buffer 6e 94 95 6a a8 93 06 e2 76 b1 c3 10 20 4c
// e5 f9 1e 8f 2b b9 b4 1f ac 41 a5 3d 4f 00 b9 5f 83
// 93 88 1c de 5b 7e 7e 72 0c ab 5f 52 ff cc a9 6a 40>

console.log(nyanbuffer.toString("hex"));
// "6e94956aa89306e276b1c310204ce5f91e8f2bb9b41fac41
// a53d4f00b95f8393881cde5b7e7e720cab5f52ffcca96a40"

console.log(nyanbuffer.toString("base64"));
// "bpSVaqiTBuJ2scMQIEzl+R6PK7m0H6xBpT1PALlfg5OIHN5bfn5yDKtfUv/MqWpA"

console.log(Array.from(nyanbuffer));
// [110, 148, 149, 106, 168, 147, 6, 226, 118, 177, 195, 16,
// 32, 76, 229, 249, 30, 143, 43, 185, 180, 31, 172, 65,
// 165, 61, 79, 0, 185, 95, 131, 147, 136, 28, 222, 91,
// 126, 126, 114, 12, 171, 95, 82, 255, 204, 169, 106, 64]

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.

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.

1.0.0

6 years ago