SHA1(3)
π SHA1 β ok bestie, we're doing NIST Secure Hash Algorithm β¨
So like, this is the manual for SHA1(3), from the Library Functions Manual, and it's literally the whole tea on calculating the NIST Secure Hash Algorithm. Grab a snack, we're going through every single thing π
πΈ NAME πΈ
SHA1Init, SHA1Update, SHA1Pad, SHA1Final, SHA1Transform, SHA1End, SHA1File, SHA1FileChunk, SHA1Data β calculate the NIST Secure Hash Algorithm
π LIBRARY π
Message Digest (MD4, MD5, etc.) Support Library (libmd, -lmd)
π«Ά SYNOPSIS π«Ά
#include <sys/types.h>
#include <sha1.h>
void
SHA1Init(SHA1_CTX *context);
void
SHA1Update(SHA1_CTX *context, const uint8_t *data, size_t len);
void
SHA1Pad(SHA1_CTX *context);
void
SHA1Final(uint8_t digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context);
void
SHA1Transform(uint32_t state[5],
const uint8_t buffer[SHA1_BLOCK_LENGTH]);
char *
SHA1End(SHA1_CTX *context, char *buf);
char *
SHA1File(const char *filename, char *buf);
char *
SHA1FileChunk(const char *filename, char *buf, off_t offset,
off_t length);
char *
SHA1Data(const uint8_t *data, size_t len, char *buf);
β¨ DESCRIPTION β¨
Okay so the SHA1 functions implement the NIST Secure Hash Algorithm (SHA-1), which is FIPS PUB 180-1. SHA-1 is used to generate a condensed representation of a message called a message digest. The algorithm takes a message less than 2^64 bits as input and produces a 160-bit digest suitable for use as a digital signature. It's giving "tiny little fingerprint of your whole entire message" π
π¨ Lowkey the most important part, bestie: while the SHA1 functions are considered to be more secure than themd4(3)andmd5(3)functions with which they share a similar interface, they are considered insecure as of 2005, and as of 2020 chosen-prefix attacks have become practical, thus these must not be used in cryptographic contexts. Not a vibe. Do not use this for crypto. Periodt. π
Let me introduce you to the whole friend group, one function at a time:
SHA1Init()β initializes aSHA1_CTXcontext for use withSHA1Update(), andSHA1Final(). Your little setup moment β¨SHA1Update()β adds data of lengthlento theSHA1_CTXspecified bycontext. Feed it bytes, it eats them πSHA1Final()β called when all data has been added viaSHA1Update(), and it stores a message digest in thedigestparameter. The grand finale, the mic drop π€SHA1Pad()β can be used to apply padding to the message digest as inSHA1Final(), but the current context can still be used withSHA1Update(). Like, you pad it but you're not done-done yet πSHA1Transform()β used bySHA1Update()to hash512-bitblocks and forms the core of the algorithm. Most programs should use the interface provided bySHA1Init(),SHA1Update()andSHA1Final()instead of callingSHA1Transform()directly. So like... it's the backstage crew, not the main character, okay? πSHA1End()β a front end forSHA1Final()which converts the digest into an ASCII representation of the160 bitdigest in hexadecimal. Hex girl summer π₯SHA1File()β calculates the digest for a file and returns the result viaSHA1End(). IfSHA1File()is unable to open the file aNULLpointer is returned. πSHA1FileChunk()β behaves likeSHA1File()but calculates the digest only for that portion of the file starting atoffsetand continuing forlengthbytes or until end of file is reached, whichever comes first. A zero length can be specified to read until end of file. A negativelengthoroffsetwill be ignored. Slice it, dice it πSHA1Data()β calculates the digest of an arbitrary string and returns the result viaSHA1End(). One-shot, no thoughts, just hash π¦
Now the buf situation, because it's lowkey a whole thing: for each of the SHA1End(), SHA1File(), and SHA1Data() functions the buf parameter should either be a string of at least 41 characters in size or a NULL pointer. In the latter case, space will be dynamically allocated via malloc(3) and should be freed using free(3) when it is no longer needed. Don't be the person who forgets the free(3), I'm begging π«Ά
π EXAMPLES π
Bestie, the follow code fragment will calculate the digest for the string "abc" which is 0xa9993e364706816aba3e25717850c26c9cd0d89d. Watch and learn π
SHA1_CTX sha;
uint8_t results[SHA1_DIGEST_LENGTH];
char *buf;
int n;
buf = "abc";
n = strlen(buf);
SHA1Init(&sha);
SHA1Update(&sha, (uint8_t *)buf, n);
SHA1Final(results, &sha);
/* Print the digest as one long hex value */
printf("0x");
for (n = 0; n < SHA1_DIGEST_LENGTH; n++)
printf("%02x", results[n]);
putchar('\n');
Alternately, the helper functions could be used in the following way. Look how much shorter this is, we love efficiency β¨
uint8_t output[SHA1_DIGEST_STRING_LENGTH];
char *buf = "abc";
printf("0x%s\n", SHA1Data(buf, strlen(buf), output));
π SEE ALSO π
Go read your other faves, bestie:
And the actual source material, the academic texts:
- J. Burrows, The Secure Hash Standard,
FIPS PUB 180-1. - D. Eastlake and P. Jones, US Secure Hash Algorithm 1,
RFC 3174.
π§ HISTORY π§
The SHA-1 functions appeared in OpenBSD 2.0. An icon, a legend, and honestly a little bit of a cautionary tale π
π AUTHORS π
This implementation of SHA-1 was written by Steve Reid. Absolute legend behavior π
The SHA1End(), SHA1File(), SHA1FileChunk(), and SHA1Data() helper functions are derived from code written by Poul-Henning Kamp. Thank you for your service π«Ά
π CAVEATS π
Okay, warnings time, and I need you to actually listen:
- This implementation of SHA-1 has not been validated by NIST and as such is not in official compliance with the standard. So like, it's SHA-1 flavored, not officially blessed π
- If a message digest is to be copied to a multi-byte type (ie: an array of five
32-bitintegers) it will be necessary to perform byte swapping on little endian machines such as thei386,alpha, andvax. Byte order jumpscare, no cap π
π¨ And again for the people in the back: insecure as of 2005, chosen-prefix attacks practical as of 2020, do not use in cryptographic contexts. This is the rule. The vibe check has been failed. Use sha2(3) instead, bestie π
That's the whole page!! This was Debian's copy, dated February 13, 2008, page SHA1(3). Mwah, go hash responsibly π β¨π
π¦ where this page comes from
- alpine 3.22: libmd-doc 1.1.0-r0 Β· BSD-3-Clause AND BSD-2-Clause AND ISC AND Beerware AND Public Domain
- alpine 3.23: libmd-doc 1.1.0-r0 Β· BSD-3-Clause AND BSD-2-Clause AND ISC AND Beerware AND Public Domain
- alpine 3.24: libmd-doc 1.2.0-r0 Β· BSD-3-Clause AND BSD-2-Clause AND ISC AND Beerware AND Public Domain
- arch rolling: libmd 1.3.0-1 Β· BSD-3-Clause AND BSD-2-Clause AND ISC AND Beerware AND CC-PDM-1.0
- debian bookworm: libmd-dev 1.0.4-2
- debian trixie: libmd-dev 1.1.0-2+b1
- epel 10: libmd-devel 1.3.0-1.el10_3 Β· BSD-2-Clause AND BSD-3-Clause AND ISC AND Beerware AND LicenseRef-Fedora-Public-Domain
- epel 9: libmd-devel 1.3.0-1.el9 Β· BSD-2-Clause AND BSD-3-Clause AND ISC AND Beerware AND LicenseRef-Fedora-Public-Domain
- fedora 43: libmd-devel 1.1.0-8.fc43 Β· BSD-2-Clause AND BSD-3-Clause AND ISC AND Beerware AND LicenseRef-Fedora-Public-Domain
- fedora 43: libmd-devel 1.1.0-8.fc43 Β· BSD-2-Clause AND BSD-3-Clause AND ISC AND Beerware AND LicenseRef-Fedora-Public-Domain
- fedora 43: libmd-devel 1.2.0-1.fc43
- fedora 43: libmd-devel 1.2.0-1.fc43
- fedora 43: libmd-devel 1.3.0-1.fc43 Β· BSD-2-Clause AND BSD-3-Clause AND ISC AND Beerware AND LicenseRef-Fedora-Public-Domain
- fedora 43: libmd-devel 1.3.0-1.fc43 Β· BSD-2-Clause AND BSD-3-Clause AND ISC AND Beerware AND LicenseRef-Fedora-Public-Domain
- fedora 44: libmd-devel 1.1.0-9.fc44 Β· BSD-2-Clause AND BSD-3-Clause AND ISC AND Beerware AND LicenseRef-Fedora-Public-Domain
- fedora 44: libmd-devel 1.1.0-9.fc44 Β· BSD-2-Clause AND BSD-3-Clause AND ISC AND Beerware AND LicenseRef-Fedora-Public-Domain
- fedora 44: libmd-devel 1.3.0-1.fc44 Β· BSD-2-Clause AND BSD-3-Clause AND ISC AND Beerware AND LicenseRef-Fedora-Public-Domain
- fedora 44: libmd-devel 1.3.0-1.fc44 Β· BSD-2-Clause AND BSD-3-Clause AND ISC AND Beerware AND LicenseRef-Fedora-Public-Domain
- nixos 26.05: libmd 1.1.0
- nixpkgs unstable: libmd 1.3.0
- opensuse-leap 16.0: libmd-devel 1.1.0-160000.3.2 Β· BSD-2-Clause OR BSD-3-Clause OR ISC OR SUSE-Public-Domain
- opensuse-leap 16.0: libmd-devel 1.1.0-160000.3.2 Β· BSD-2-Clause OR BSD-3-Clause OR ISC OR SUSE-Public-Domain
- opensuse-leap 16.0: libmd-devel 1.1.0-160000.3.2 Β· BSD-2-Clause OR BSD-3-Clause OR ISC OR SUSE-Public-Domain
- opensuse-leap 16.0: libmd-devel 1.1.0-160000.3.2 Β· BSD-2-Clause OR BSD-3-Clause OR ISC OR SUSE-Public-Domain
- ubuntu jammy: libmd-dev 1.0.4-1build1
- ubuntu noble: libmd-dev 1.1.0-2build1
- ubuntu noble: libmd-dev 1.1.0-2build1.1
- ubuntu questing: libmd-dev 1.1.0-2build3
- ubuntu resolute: libmd-dev 1.1.0-2build4
- ubuntu stonking: libmd-dev 1.2.0-2
- Copyright (c) 1997, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms