Go to main content

man pages section 3: Extended Library Functions, Volume 1

Exit Print View

Updated: Wednesday, July 27, 2022
 
 

sha1(3EXT)

Name

sha1, SHA1Init, SHA1Update, SHA1Final - SHA-1 digest functions

Synopsis

cc [ flag ... ] 
file ... –lmd [ library ... ]
#include <sha1.h>

void SHA1Init(SHA1_CTX *
context);
void SHA1Update(SHA1_CTX *context, const void *input,
     size_t inlen);
void SHA1Final(void *output, SHA1_CTX *
context);

Description

The SHA1 functions implement the SHA-1 message-digest algorithm. The algorithm takes as input a message of arbitrary length and produces a 160-bit “fingerprint” or “message digest” as output. The SHA-1 message-digest algorithm is intended for digital signature applications in which large files are “compressed” in a secure manner before being encrypted with a private (secret) key under a public-key cryptosystem such as RSA.

SHA1Init(), SHA1Update(), SHA1Final()

The SHA1Init(), SHA1Update(), and SHA1Final() functions allow a SHA-1 digest to be computed over multiple message blocks. Between blocks, the state of the SHA-1 computation is held in an SHA1_CTX context structure allocated by the caller. A complete digest computation consists of calls to SHA1 functions in the following order: one call to SHA1Init(), one or more calls to SHA1Update(), and one call to SHA1Final().

The SHA1Init() function initializes the SHA1_CTX context structure pointed to by context.

The SHA1Update() function computes a partial SHA-1 digest on the inlen-byte message block pointed to by input, and updates the SHA1_CTX context structure pointed to by context accordingly.

The SHA1Final() function generates the final SHA-1 digest, using the SHA1_CTX context structure pointed to by context. The 160-bit SHA-1 digest is written to output. After a call to SHA1Final(), the state of the context structure is undefined. It must be reinitialized with SHA1Init() before it can be used again.

Security

The SHA-1 algorithm is believed to have some weaknesses. Migration to one of the SHA-2 or SHA-3 algorithms is highly recommended when compatibility with data formats and network protocols permit doing so. See sha2(3EXT) and sha3(3EXT) for functions which use those algorithms.

Return Values

These functions do not return a value.

Examples

Example 1 Authenticate a message found in multiple buffers

The following is a sample function that authenticates a message found in multiple buffers. The calling function provides an authentication buffer to contain the result of the SHA1 digest.

#include <sys/types.h>
#include <sys/uio.h>
#include <sha1.h>

int
AuthenticateMsg(unsigned char *auth_buffer, struct iovec
                *messageIov, unsigned int num_buffers)
{
    SHA1_CTX sha1_context;
    unsigned int i;

    SHA1Init(&sha1_context);

    for (i = 0; i < num_buffers; i++)
    {
         SHA1Update(&sha1_context, messageIov->iov_base,
                   messageIov->iov_len);
         messageIov += sizeof(struct iovec);
    }

    SHA1Final(auth_buffer, &sha1_context);

    return 0;
}

Attributes

See attributes(7) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Interface Stability
Committed
MT-Level
MT-Safe

See Also

sha2(3EXT), sha3(3EXT), libmd(3LIB)

Eastlake, D., RFC 3174, US Secure Hash Algorithm 1 (SHA1), September 2001. https://tools.ietf.org/html/rfc3174

History

These functions were added to Solaris in Solaris 10 8/07 (Update 4).