sha2
nimony/lib/std/crypto/sha2.nim
SHA-2 (FIPS 180-4): SHA-256, SHA-384 and SHA-512.
SHA-256 is the transcript hash and the HKDF hash of the TLS 1.3 suites std/tls speaks; SHA-384 and SHA-512 are what certificate signatures are computed over. None of them branches on or indexes by the data, so the straightforward implementation is already constant time.
type Sha256Digest = array[0..31, uint8]type Sha384Digest = array[0..47, uint8]type Sha512Digest = array[0..63, uint8]type Sha256 = object h: array[0..7, uint32] buf: array[0..63, uint8] bufLen: 0..63 total: uint64
type Sha512 = object h: array[0..7, uint64] buf: array[0..127, uint8] bufLen: 0..127 total: uint64
func initSha256(): Sha256func update(s: var Sha256; data: openArray[uint8])func update(s: var Sha256; data: string)func finish(s: Sha256): array[0..31, uint8]The digest of everything fed so far.
sis not consumed — that is what lets a TLS transcript hash be read and then extended.func sha256(data: openArray[uint8]): array[0..31, uint8]func sha256(data: string): array[0..31, uint8]func initSha512(): Sha512func initSha384(): Sha512SHA-384 is SHA-512 from another starting point, truncated; feed it with
updateand read it withfinish384.func update(s: var Sha512; data: openArray[uint8])func update(s: var Sha512; data: string)func finish(s: Sha512): array[0..63, uint8]func finish384(s: Sha512): array[0..47, uint8]func sha512(data: openArray[uint8]): array[0..63, uint8]func sha384(data: openArray[uint8]): array[0..47, uint8]func sha512(data: string): array[0..63, uint8]func sha384(data: string): array[0..47, uint8]func toHex(d: openArray[uint8]): stringLower-case hex, the way digests are written in test vectors.