---
name: "SHA1_Init"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/SHA1_Data"
---

# SHA1\_Init(3)

calculate the FIPS 160 and 160-1 \`\`SHA'' message digests

Part of [SHA1\_Data(3)](/SHA1_Data/3.md).

## NAME

**SHA\_Init**, **SHA\_Update**, **SHA\_Final**, **SHA\_End**, **SHA\_Fd**, **SHA\_FdChunk**, **SHA\_File**, **SHA\_FileChunk**, **SHA\_Data**, **SHA1\_Init**, **SHA1\_Update**, **SHA1\_Final**, **SHA1\_End**, **SHA1\_Fd**, **SHA1\_FdChunk**, **SHA1\_File**, **SHA1\_FileChunk**, **SHA1\_Data** – calculate the FIPS 160 and 160-1 \`\`SHA'' message digests

## LIBRARY

library “libmd”

## SYNOPSIS

**#include \<sys/types.h\>**

**#include \<sha.h\>**

*void*\

**SHA\_Init**(*SHA\_CTX \*context*);

*void*\

**SHA\_Update**(*SHA\_CTX \*context*, *const unsigned char \*data*, *size\_t len*);

*void*\

**SHA\_Final**(*unsigned char digest\[20\]*, *SHA\_CTX \*context*);

*char \**\

**SHA\_End**(*SHA\_CTX \*context*, *char \*buf*);

*char \**\

**SHA\_Fd**(*int fd*, *char \*buf*);

*char \**\

**SHA\_FdChunk**(*int fd*, *char \*buf*, *off\_t offset*, *off\_t length*);

*char \**\

**SHA\_File**(*const char \*filename*, *char \*buf*);

*char \**\

**SHA\_FileChunk**(*const char \*filename*, *char \*buf*, *off\_t offset*, *off\_t length*);

*char \**\

**SHA\_Data**(*const unsigned char \*data*, *unsigned int len*, *char \*buf*);

*void*\

**SHA1\_Init**(*SHA\_CTX \*context*);

*void*\

**SHA1\_Update**(*SHA\_CTX \*context*, *const unsigned char \*data*, *size\_t len*);

*void*\

**SHA1\_Final**(*unsigned char digest\[20\]*, *SHA\_CTX \*context*);

*char \**\

**SHA1\_End**(*SHA\_CTX \*context*, *char \*buf*);

*char \**\

**SHA1\_Fd**(*int fd*, *char \*buf*);

*char \**\

**SHA1\_FdChunk**(*int fd*, *char \*buf*, *off\_t offset*, *off\_t length*);

*char \**\

**SHA1\_File**(*const char \*filename*, *char \*buf*);

*char \**\

**SHA1\_FileChunk**(*const char \*filename*, *char \*buf*, *off\_t offset*, *off\_t length*);

*char \**\

**SHA1\_Data**(*const unsigned char \*data*, *unsigned int len*, *char \*buf*);

## DESCRIPTION

The SHA\_ and SHA1\_ functions calculate a 160-bit cryptographic checksum (digest) for any number of input bytes. A cryptographic checksum is a one-way hash function; that is, it is computationally impractical to find the input corresponding to a particular output. This net result is a “fingerprint” of the input-data, which does not disclose the actual input.

SHA (or SHA-0) is the original Secure Hash Algorithm specified in FIPS 160. It was quickly proven insecure, and has been superseded by SHA-1. SHA-0 is included for compatibility purposes only.

The **SHA1\_Init**(), **SHA1\_Update**(), and **SHA1\_Final**() functions are the core functions. Allocate an *SHA\_CTX*, initialize it with **SHA1\_Init**(), run over the data with **SHA1\_Update**(), and finally extract the result using **SHA1\_Final**(), which will also erase the *SHA\_CTX*.

**SHA1\_End**() is a wrapper for **SHA1\_Final**() which converts the return value to a 41-character (including the terminating '\\0') ASCII string which represents the 160 bits in hexadecimal.

**SHA1\_File**() calculates the digest of a file, and uses **SHA1\_End**() to return the result. If the file cannot be opened, a null pointer is returned. **SHA1\_FileChunk**() is similar to **SHA1\_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 as 0, or more than the length of the remaining part of the file, **SHA1\_FileChunk**() calculates the digest from *offset* to the end of file. **SHA1\_Data**() calculates the digest of a chunk of data in memory, and uses **SHA1\_End**() to return the result.

The **SHA1\_Fd**() and **SHA1\_FdChunk**() functions are identical to their **SHA1\_File**() and **SHA1\_FileChunk**() counterparts, with the exception that the first argument is an *fd* instead of a *filename*.

When using **SHA1\_End**(), **SHA1\_File**(), or **SHA1\_Data**(), the *buf* argument can be a null pointer, in which case the returned string is allocated with [malloc(3)](/malloc/3.md) and subsequently must be explicitly deallocated using [free(3)](/free/3.md) after use. If the *buf* argument is non-null it must point to at least 41 characters of buffer space.

## ERRORS

The **SHA1\_End**() function called with a null buf argument may fail and return NULL if:

- \[ENOMEM\]

  Insufficient storage space is available.

The **SHA1\_File**() and **SHA1\_FileChunk**() may return NULL when underlying [open(2)](/open/2.md), [fstat(2)](/fstat/2.md), [lseek(2)](/lseek/2.md), or [SHA1\_End(3)](/SHA1_End/3.md) fail.

## SEE ALSO

[md4(3)](/md4/3.md), [md5(3)](/md5/3.md), [ripemd(3)](/ripemd/3.md), [sha256(3)](/sha256/3.md), [sha512(3)](/sha512/3.md), [skein(3)](/skein/3.md)

## HISTORY

These functions appeared in FreeBSD 4.0.

## AUTHORS

The core hash routines were implemented by Eric Young based on the published FIPS standards.

## BUGS

The SHA1 algorithm has been proven to be vulnerable to practical collision attacks and should not be relied upon to produce unique outputs, *nor* *should* *it* *be* *used* *as* *part* *of* *a* *new* *cryptographic* *signature* *scheme.*
