SHA2(3)

πŸ“¦
version 1.3.0 Β· 2 versions
, in arch rolling, epel 10, epel 9, fedora 43, fedora 44, nixpkgs unstable

SHA2(3) β€” bestie, it's the hash page πŸ’…βœ¨

okay so you opened sha2(3) from the Library Functions Manual and honestly? iconic behavior. this is the NIST Secure Hash Standard (version 2) and we're gonna go through every single function name because we do not skip. periodt. πŸŽ€


🌸 NAME 🌸

all of these babes, in order, and their whole entire job is to calculate the NIST Secure Hash Standard (version 2):

SHA224Init, SHA224Update, SHA224Pad, SHA224Final, SHA224Transform,
SHA224End, SHA224File, SHA224FileChunk, SHA224Data, SHA256Init,
SHA256Update, SHA256Pad, SHA256Final, SHA256Transform, SHA256End,
SHA256File, SHA256FileChunk, SHA256Data, SHA384Init, SHA384Update,
SHA384Pad, SHA384Final, SHA384Transform, SHA384End, SHA384File,
SHA384FileChunk, SHA384Data, SHA512Init, SHA512Update, SHA512Pad,
SHA512Final, SHA512Transform, SHA512End, SHA512File, SHA512FileChunk,
SHA512Data, SHA512_224Init, SHA512_224Update, SHA512_224Pad,
SHA512_224Final, SHA512_224Transform, SHA512_224End, SHA512_224File,
SHA512_224FileChunk, SHA512_224Data, SHA512_256Init, SHA512_256Update,
SHA512_256Pad, SHA512_256Final, SHA512_256Transform, SHA512_256End,
SHA512_256File, SHA512_256FileChunk, SHA512_256Data

like... that's a lot of names and every one of them ate. πŸ’–


πŸ“š LIBRARY πŸ“š

it lives in the Message Digest (MD4, MD5, etc.) Support Library β€” aka libmd, and you link it with -lmd. slay. ✨


πŸ¦‹ SYNOPSIS πŸ¦‹

here's the whole thing, character for character, because we are not playing games with includes:

#include <sys/types.h>
#include <sha2.h>

void
SHA224Init(SHA2_CTX *context);

void
SHA224Update(SHA2_CTX *context, const uint8_t *data, size_t len);

void
SHA224Pad(SHA2_CTX *context);

void
SHA224Final(uint8_t digest[SHA224_DIGEST_LENGTH], SHA2_CTX *context);

void
SHA224Transform(uint32_t state[8],
    const uint8_t buffer[SHA224_BLOCK_LENGTH]);

char *
SHA224End(SHA2_CTX *context, char *buf);

char *
SHA224File(const char *filename, char *buf);

char *
SHA224FileChunk(const char *filename, char *buf, off_t offset,
    off_t length);

char *
SHA224Data(const uint8_t *data, size_t len, char *buf);

void
SHA256Init(SHA2_CTX *context);

void
SHA256Update(SHA2_CTX *context, const uint8_t *data, size_t len);

void
SHA256Pad(SHA2_CTX *context);

void
SHA256Final(uint8_t digest[SHA256_DIGEST_LENGTH], SHA2_CTX *context);

void
SHA256Transform(uint32_t state[8],
    const uint8_t buffer[SHA256_BLOCK_LENGTH]);

char *
SHA256End(SHA2_CTX *context, char *buf);

char *
SHA256File(const char *filename, char *buf);

char *
SHA256FileChunk(const char *filename, char *buf, off_t offset,
    off_t length);

char *
SHA256Data(const uint8_t *data, size_t len, char *buf);

void
SHA384Init(SHA2_CTX *context);

void
SHA384Update(SHA2_CTX *context, const uint8_t *data, size_t len);

void
SHA384Pad(SHA2_CTX *context);

void
SHA384Final(uint8_t digest[SHA384_DIGEST_LENGTH], SHA2_CTX *context);

void
SHA384Transform(uint64_t state[8],
    const uint8_t buffer[SHA384_BLOCK_LENGTH]);

char *
SHA384End(SHA2_CTX *context, char *buf);

char *
SHA384File(const char *filename, char *buf);

char *
SHA384FileChunk(const char *filename, char *buf, off_t offset,
    off_t length);

char *
SHA384Data(const uint8_t *data, size_t len, char *buf);

void
SHA512Init(SHA2_CTX *context);

void
SHA512Update(SHA2_CTX *context, const uint8_t *data, size_t len);

void
SHA512Pad(SHA2_CTX *context);

void
SHA512Final(uint8_t digest[SHA512_DIGEST_LENGTH], SHA2_CTX *context);

void
SHA512Transform(uint64_t state[8],
    const uint8_t buffer[SHA512_BLOCK_LENGTH]);

char *
SHA512End(SHA2_CTX *context, char *buf);

char *
SHA512File(const char *filename, char *buf);

char *
SHA512FileChunk(const char *filename, char *buf, off_t offset,
    off_t length);

char *
SHA512Data(const uint8_t *data, size_t len, char *buf);

void
SHA512_224Init(SHA2_CTX *context);

void
SHA512_224Update(SHA2_CTX *context, const uint8_t *data, size_t len);

void
SHA512_224Pad(SHA2_CTX *context);

void
SHA512_224Final(uint8_t digest[SHA512_224_DIGEST_LENGTH],
    SHA2_CTX *context);

void
SHA512_224Transform(uint64_t state[8],
    const uint8_t buffer[SHA512_224_BLOCK_LENGTH]);

char *
SHA512_224End(SHA2_CTX *context, char *buf);

char *
SHA512_224File(const char *filename, char *buf);

char *
SHA512_224FileChunk(const char *filename, char *buf, off_t offset,
    off_t length);

char *
SHA512_224Data(const uint8_t *data, size_t len, char *buf);

void
SHA512_256Init(SHA2_CTX *context);

void
SHA512_256Update(SHA2_CTX *context, const uint8_t *data, size_t len);

void
SHA512_256Pad(SHA2_CTX *context);

void
SHA512_256Final(uint8_t digest[SHA512_256_DIGEST_LENGTH],
    SHA2_CTX *context);

void
SHA512_256Transform(uint64_t state[8],
    const uint8_t buffer[SHA512_256_BLOCK_LENGTH]);

char *
SHA512_256End(SHA2_CTX *context, char *buf);

char *
SHA512_256File(const char *filename, char *buf);

char *
SHA512_256FileChunk(const char *filename, char *buf, off_t offset,
    off_t length);

char *
SHA512_256Data(const uint8_t *data, size_t len, char *buf);

✨ DESCRIPTION ✨

okay so the SHA2 functions implement the NIST Secure Hash Standard, FIPS PUB 180-2. their whole vibe is generating a condensed representation of a message β€” that's called a message digest β€” and it's suitable for use as a digital signature. πŸ’–

there are four families of functions, and the names literally match up to the number of bits in the resulting message digest. no cap.

  • SHA-224 and SHA-256 are limited to processing a message of less than 2^64 bits as input.
  • SHA-384 and SHA-512 can process a message of at most 2^128 - 1 bits as input.

the SHA2 functions are considered to be more secure than the SHA1 functions, and they share a similar interface with them. the 224, 256, 384, and 512-bit versions of SHA2 all share the same interface. and bestie, SHA512/224 and SHA512/256 β€” the truncated versions of SHA512 β€” are also supported. πŸŽ€

for brevity, only the 256-bit variants are described below. iykyk, the rest are the exact same shape.

🌈 the functions, one by one 🌈

  • SHA256Init() β€” initializes a SHA2_CTX context so it's ready to be used with SHA256Update() and SHA256Final(). it's giving setup era.
  • SHA256Update() β€” adds data of length len to the SHA2_CTX specified by context.
  • SHA256Final() β€” you call this once all your data has been added via SHA256Update(), and it stores a message digest in the digest parameter. the grand finale fr fr. πŸ’…
  • SHA256Pad() β€” can be used to apply padding to the message digest just like SHA256Final() does, but here's the gag: the current context can still be used with SHA256Update() afterwards. so it's not a goodbye, it's a "see you in a sec". 🫢
  • SHA256Transform() β€” used by SHA256Update() to hash 512-bit blocks and it forms the core of the algorithm. but most programs should use the interface provided by SHA256Init(), SHA256Update(), and SHA256Final() instead of calling SHA256Transform() directly. like, it's the engine, not the steering wheel. πŸ”₯
  • SHA256End() β€” a front end for SHA256Final() which converts the digest into an ASCII representation of the digest in hexadecimal. hex girlie behavior. ✨
  • SHA256File() β€” calculates the digest for a file and returns the result via SHA256End(). if SHA256File() is unable to open the file, a NULL pointer is returned. πŸ’€
  • SHA256FileChunk() β€” behaves like SHA256File() but calculates the digest only for that portion of the file starting at offset and continuing for length bytes or until end of file is reached, whichever comes first. a zero length can be specified to read until end of file. a negative length or offset will be ignored.
  • SHA256Data() β€” calculates the digest of an arbitrary string and returns the result via SHA256End().

🧚 about that buf parameter 🧚

for each of the SHA256End(), SHA256File(), SHA256FileChunk(), and SHA256Data() functions, the buf parameter should either be a string large enough to hold the resulting digest β€” like:

  • SHA224_DIGEST_STRING_LENGTH
  • SHA256_DIGEST_STRING_LENGTH
  • SHA384_DIGEST_STRING_LENGTH
  • SHA512_DIGEST_STRING_LENGTH
  • SHA512_224_DIGEST_STRING_LENGTH
  • SHA512_256_DIGEST_STRING_LENGTH

(depending on the function being used, obviously πŸ’–)

or a NULL pointer. and in that latter case? space will be dynamically allocated via malloc(3), and you should free it using free(3) when it's no longer needed. don't be a memory leaker bestie, that's not the vibe. πŸ“


πŸ‘‘ EXAMPLES πŸ‘‘

this code fragment will calculate the SHA-256 digest for the string "abc", which is β€œ0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad”. iconic hash, honestly.

SHA2_CTX ctx;
uint8_t results[SHA256_DIGEST_LENGTH];
char *buf;
int n;

buf = "abc";
n = strlen(buf);
SHA256Init(&ctx);
SHA256Update(&ctx, (uint8_t *)buf, n);
SHA256Final(results, &ctx);

/* Print the digest as one long hex value */
printf("0x");
for (n = 0; n < SHA256_DIGEST_LENGTH; n++)
        printf("%02x", results[n]);
putchar('\n');

alternately, the helper functions could be used in the following way β€” look how short it is, she's efficient:

uint8_t output[SHA256_DIGEST_STRING_LENGTH];
char *buf = "abc";

printf("0x%s\n", SHA256Data(buf, strlen(buf), output));

πŸ”₯ SEE ALSO πŸ”₯

go read these next, bestie:

also: Secure Hash Standard, FIPS PUB 180-2. πŸ“–


🌸 HISTORY 🌸

the SHA2 functions appeared in OpenBSD 3.4. vintage. a classic. πŸ’œ


πŸŽ€ AUTHORS πŸŽ€

this implementation of the SHA functions was written by Aaron D. Gifford. πŸ‘‘

and the SHA256End(), SHA256File(), SHA256FileChunk(), and SHA256Data() helper functions are derived from code written by Poul-Henning Kamp. legends only. 🫢


okay that's the whole page!! this one came to you from Debian, dated December 4, 2019, and it's filed under SHA2(3). mwah, go hash something responsibly πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from