SHA256(3)
π« SHA256(3) β the girlies' guide to SHA-256 (and her lil sister SHA-224) π«
okay so bestie, welcome to the SHA256(3) manual page, literally the library functions manual for the Message Digest family, and we're about to make hashing make sense. no cap. π
β¨ NAME β¨
so basically this whole page is about a whole squad of functions that calculate the FIPS 180-2 ``SHA-256'' (or SHA-224) message digest. and yes, the squad is LONG, let me list them all because every single one is invited to the party:
SHA224_Init,SHA224_Update,SHA224_Final,SHA224_End,SHA224_Fd,SHA224_FdChunk,SHA224_File,SHA224_FileChunk,SHA224_DataSHA256_Init,SHA256_Update,SHA256_Final,SHA256_End,SHA256_Fd,SHA256_FdChunk,SHA256_File,SHA256_FileChunk,SHA256_Data
that's them. that's the whole roster. π
π LIBRARY π
this lives in the Message Digest (MD4, MD5, etc.) Support Library, aka libmd, and you link it with -lmd. iconique. π«Ά
πΈ SYNOPSIS πΈ
first up, the SHA-224 section β she has her own header:
#include <sys/types.h>
#include <sha224.h>
void
SHA224_Init(SHA224_CTX *context);
void
SHA224_Update(SHA224_CTX *context, const unsigned char *data,
size_t len);
void
SHA224_Final(unsigned char digest[32], SHA224_CTX *context);
char *
SHA224_End(SHA224_CTX *context, char *buf);
char *
SHA224_Fd(int fd, char *buf);
char *
SHA224_FdChunk(int fd, char *buf, off_t offset, off_t length);
char *
SHA224_File(const char *filename, char *buf);
char *
SHA224_FileChunk(const char *filename, char *buf, off_t offset,
off_t length);
char *
SHA224_Data(const unsigned char *data, unsigned int len, char *buf);
and now the SHA-256 side, different header, same energy:
#include <sha256.h>
void
SHA256_Init(SHA256_CTX *context);
void
SHA256_Update(SHA256_CTX *context, const unsigned char *data,
size_t len);
void
SHA256_Final(unsigned char digest[32], SHA256_CTX *context);
char *
SHA256_End(SHA256_CTX *context, char *buf);
char *
SHA256_Fd(int fd, char *buf);
char *
SHA256_FdChunk(int fd, char *buf, off_t offset, off_t length);
char *
SHA256_File(const char *filename, char *buf);
char *
SHA256_FileChunk(const char *filename, char *buf, off_t offset,
off_t length);
char *
SHA256_Data(const unsigned char *data, unsigned int len, char *buf);
like, look at all those signatures. she's thorough. π
π DESCRIPTION π
so the SHA256_ functions calculate a 256-bit cryptographic checksum (that's a digest, bestie) for any number of input bytes. any. number. β¨
a cryptographic checksum is a one-way hash function β meaning it's computationally impractical to find the input that corresponds to a particular output. you can't reverse it, periodt. the net result is a "fingerprint" of the input-data, which does not disclose the actual input. iykyk. π₯
the core three π§
SHA256_Init(), SHA256_Update(), and SHA256_Final() are the core functions β the main characters, honestly. the whole flow is:
- allocate an
SHA256_CTX - initialize it with
SHA256_Init() - run over the data with
SHA256_Update() - finally extract the result using
SHA256_Final()
and plot twist: SHA256_Final() also erases the SHA256_CTX. she cleans up after herself. we love a tidy queen. π
the convenience girlies π
SHA256_End()is a wrapper forSHA256_Final()that converts the return value into a 65-character (including the terminating'\0') ASCII string which represents the 256 bits in hexadecimal.SHA256_File()calculates the digest of a file, and usesSHA256_End()to return the result. if the file cannot be opened, a null pointer is returned. πSHA256_FileChunk()is similar toSHA256_File(), but it only calculates the digest over a byte-range of the file specified β starting at offset and spanning length bytes. if the length parameter is specified as0, or more than the length of the remaining part of the file,SHA256_FileChunk()calculates the digest from offset to the end of file. no wasted effort.SHA256_Data()calculates the digest of a chunk of data in memory, and usesSHA256_End()to return the result.SHA256_Fd()andSHA256_FdChunk()are identical to theirSHA256_File()andSHA256_FileChunk()counterparts, with one exception: the first argument is an fd instead of a filename. same vibes, different entry point.
the buf situation β pay attention bestie π«Ά
when using SHA256_End(), SHA256_File(), or SHA256_Data(), the buf argument can be a null pointer. in that case, the returned string is allocated with malloc(3) and subsequently must be explicitly deallocated using free(3) after use. don't forget. i'm serious. π
if the buf argument is non-null, it must point to at least 65 characters of buffer space. sixty-five. not 64. not "like, around 65". 65. β
SHA-224 tea β
SHA224 is identical to SHA256, except it has slightly different initialization vectors, and is truncated to a shorter digest. she's the shorter sister and she still slays. π
π ERRORS π
the SHA256_End() function called with a null buf argument may fail and return NULL if:
- [ENOMEM] β Insufficient storage space is available.
and SHA256_File() and SHA256_FileChunk() may return NULL when underlying open(2), fstat(2), lseek(2), or SHA256_End(3) fail. so if something upstream is being messy, that's where your NULL came from. π«
π SEE ALSO π
go read these next, bestie, they're all in the family:
π¦ HISTORY π¦
these functions appeared in FreeBSD 6.0. vintage. iconic. a moment in time. β
π AUTHORS π
the core hash routines were implemented by Colin Percival based on the published FIPS 180-2 standard. shoutout Colin. π«Ά
π BUGS π
real talk: no method is known to exist which finds two files having the same hash value, nor to find a file with a specific hash value.
but β there is on the other hand no guarantee that such a method does not exist. so like... we're fine, but we're not that fine. π
and that's the whole page, bestie!! now go hash something and feel iconic doing it π
this little masterpiece comes to you from Debian, dated March 8, 2022, and it's the SHA256(3) manual page β mwah, love u, see u in the next man π
π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms