nish/crypto/sha512

SHA-512 and SHA-384, written from FIPS 180-4.

import {
  SHA384_SIZE,
  SHA512_BLOCK,
  SHA512_SIZE,
  Sha384,
  Sha512,
  sha384,
  sha512,
} from "nish/crypto/sha512";
import { sha512, sha384, Sha512 } from "nish/crypto/sha512";

const whole: u8[] = sha512(message); // 64 bytes; sha384 answers 48

const h = new Sha512();
h.update(first, 0, toI32(first.length));
h.update(second, 0, toI32(second.length));
const all: u8[] = h.digest();
One engine, two hashes

Both hashes are one compression function over eight 64-bit words: SHA-384 starts from its own initial value (§5.3.4) and keeps the first 48 bytes of the result (§6.5), SHA-512 starts from §5.3.5 and keeps all 64. So one engine does the work, and Sha512 and Sha384 are the two ways to start it.

Time says only the length

A hasher streams: update any number of windows, copy to take an independent hasher at the same point (TLS 1.3 hashes its transcript and reads intermediate hashes of it), then digest once. The work depends on how many bytes arrive and never on their values: there is no branch and no table index on message data, so a secret message (an HMAC key, a transcript) is hashed in time that says nothing about it but its length.

The constants are the fractional parts of the cube roots of the first eighty primes (§4.2.3) and of the square roots of the first sixteen (§5.3.4, §5.3.5), as FIPS 180-4 defines them. A literal past 2^53 cannot be written exactly, so each is spelled as its two 32-bit halves.

Functions

sha384

The SHA-384 digest of all of data, as a fresh 48-byte array. An array longer than 2^31 - 1 bytes panics, as in sha512.

const sha384: (data: u8[]) => u8[]
datau8[]required

The whole message; it is read, not changed.

returnsu8[]

A fresh 48-byte digest.

sha512

The SHA-512 digest of all of data, as a fresh 64-byte array. An array longer than 2^31 - 1 bytes panics, for the reason sha256 gives: a longer length does not survive toI32 under --number-mode f64, and the digest would be of a prefix.

const sha512: (data: u8[]) => u8[]
datau8[]required

The whole message; it is read, not changed.

returnsu8[]

A fresh 64-byte digest.

Classes

Sha384

A streaming SHA-384 hasher, with Sha512's methods. digest answers a fresh 48-byte array.

class Sha384
Member
new Sha384() A hasher at the start of an empty message (FIPS 180-4 §5.3.4's initial hash value).
engine: Sha512Engine The engine the hash runs on. nish/crypto/hmac and nish/crypto/ed25519 reach into it to wipe a hasher, and the tests to check the wipe; a caller has no use for it.
copy(): Sha384 An independent hasher at the same point: updating either leaves the other alone.
digest(): u8[] Pads the message (§5.1.2) and answers its digest, the first 48 bytes of the final hash value (§6.5). Ends the computation: a second digest panics.
update(data: u8[], off: i32, len: i32): void Absorbs data[off .. off + len). A window outside data, or an update after digest, panics.

Sha512

A streaming SHA-512 hasher. update takes a window (data, off, len); digest answers a fresh 64-byte array, after which the hasher is spent and another update or digest panics.

class Sha512
Member
new Sha512() A hasher at the start of an empty message (FIPS 180-4 §5.3.5's initial hash value).
engine: Sha512Engine The engine the hash runs on. nish/crypto/hmac and nish/crypto/ed25519 reach into it to wipe a hasher, and the tests to check the wipe; a caller has no use for it.
copy(): Sha512 An independent hasher at the same point: updating either leaves the other alone.
digest(): u8[] Pads the message (§5.1.2) and answers its digest. Ends the computation: a second digest panics.
update(data: u8[], off: i32, len: i32): void Absorbs data[off .. off + len). A window outside data, or an update after digest, panics.

Constants

48

SHA384_SIZE: i32

The length of a SHA-384 digest, in bytes.

128

SHA512_BLOCK: i32

The block both hashes compress, in bytes.

64

SHA512_SIZE: i32

The length of a SHA-512 digest, in bytes.