---
name: "libressl-EVP_DigestInit"
section: "3"
platform: "linux"
package: "libressl"
version: "4.4.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/libressl-EVP_DigestInit"
---

# libressl-EVP\_DigestInit(3)

EVP digest routines

## NAME

**EVP\_MD\_CTX\_new**, **EVP\_MD\_CTX\_reset**, **EVP\_MD\_CTX\_free**, **EVP\_MD\_CTX\_init**, **EVP\_MD\_CTX\_create**, **EVP\_MD\_CTX\_cleanup**, **EVP\_MD\_CTX\_destroy**, **EVP\_DigestInit\_ex**, **EVP\_DigestUpdate**, **EVP\_DigestFinal\_ex**, **EVP\_Digest**, **EVP\_MD\_CTX\_copy\_ex**, **EVP\_DigestInit**, **EVP\_DigestFinal**, **EVP\_MD\_CTX\_copy**, **EVP\_MD\_CTX\_md**, **EVP\_md\_null**, **EVP\_sha224**, **EVP\_sha256**, **EVP\_sha384**, **EVP\_sha512**, **EVP\_sha512\_224**, **EVP\_sha512\_256**, **EVP\_ripemd160**, **EVP\_get\_digestbyname**, **EVP\_get\_digestbynid**, **EVP\_get\_digestbyobj** – EVP digest routines

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

**#include \<openssl/evp.h\>**

*EVP\_MD\_CTX* *\**\

**EVP\_MD\_CTX\_new**(*void*);

*int*\

**EVP\_MD\_CTX\_reset**(*EVP\_MD\_CTX \*ctx*);

*void*\

**EVP\_MD\_CTX\_free**(*EVP\_MD\_CTX \*ctx*);

*int*\

**EVP\_MD\_CTX\_init**(*EVP\_MD\_CTX \*ctx*);

*EVP\_MD\_CTX* *\**\

**EVP\_MD\_CTX\_create**(*void*);

*int*\

**EVP\_MD\_CTX\_cleanup**(*EVP\_MD\_CTX \*ctx*);

*void*\

**EVP\_MD\_CTX\_destroy**(*EVP\_MD\_CTX \*ctx*);

*int*\

**EVP\_DigestInit\_ex**(*EVP\_MD\_CTX \*ctx*, *const EVP\_MD \*type*, *ENGINE \*engine*);

*int*\

**EVP\_DigestUpdate**(*EVP\_MD\_CTX \*ctx*, *const void \*d*, *size\_t cnt*);

*int*\

**EVP\_DigestFinal\_ex**(*EVP\_MD\_CTX \*ctx*, *unsigned char \*md*, *unsigned int \*s*);

*int*\

**EVP\_Digest**(*const void \*d*, *size\_t cnt*, *unsigned char \*md*, *unsigned int \*s*, *const EVP\_MD \*type*, *ENGINE \*engine*);

*int*\

**EVP\_MD\_CTX\_copy\_ex**(*EVP\_MD\_CTX \*out*, *const EVP\_MD\_CTX \*in*);

*int*\

**EVP\_DigestInit**(*EVP\_MD\_CTX \*ctx*, *const EVP\_MD \*type*);

*int*\

**EVP\_DigestFinal**(*EVP\_MD\_CTX \*ctx*, *unsigned char \*md*, *unsigned int \*s*);

*int*\

**EVP\_MD\_CTX\_copy**(*EVP\_MD\_CTX \*out*, *EVP\_MD\_CTX \*in*);

*const* *EVP\_MD* *\**\

**EVP\_MD\_CTX\_md**(*const EVP\_MD\_CTX \*ctx*);

*const* *EVP\_MD* *\**\

**EVP\_md\_null**(*void*);

*const* *EVP\_MD* *\**\

**EVP\_sha224**(*void*);

*const* *EVP\_MD* *\**\

**EVP\_sha256**(*void*);

*const* *EVP\_MD* *\**\

**EVP\_sha384**(*void*);

*const* *EVP\_MD* *\**\

**EVP\_sha512**(*void*);

*const* *EVP\_MD* *\**\

**EVP\_sha512\_224**(*void*);

*const* *EVP\_MD* *\**\

**EVP\_sha512\_256**(*void*);

*const* *EVP\_MD* *\**\

**EVP\_ripemd160**(*void*);

*const* *EVP\_MD* *\**\

**EVP\_get\_digestbyname**(*const char \*name*);

*const* *EVP\_MD* *\**\

**EVP\_get\_digestbynid**(*int type*);

*const* *EVP\_MD* *\**\

**EVP\_get\_digestbyobj**(*const ASN1\_OBJECT \*o*);

## DESCRIPTION

The EVP digest routines are a high-level interface to message digests and should be used instead of the cipher-specific functions.

**EVP\_MD\_CTX\_new**() allocates a new, empty digest context.

**EVP\_MD\_CTX\_reset**() cleans up *ctx* and resets it to the state it had after **EVP\_MD\_CTX\_new**(), such that it can be reused.

**EVP\_MD\_CTX\_free**() cleans up *ctx* and frees the space allocated to it.

**EVP\_MD\_CTX\_init**() is a deprecated function to clear a digest context on the stack before use. Do not use it on a digest context returned from **EVP\_MD\_CTX\_new**() or one that was already used.

**EVP\_MD\_CTX\_create**(), **EVP\_MD\_CTX\_cleanup**(), and **EVP\_MD\_CTX\_destroy**() are deprecated aliases for **EVP\_MD\_CTX\_new**(), **EVP\_MD\_CTX\_reset**(), and **EVP\_MD\_CTX\_free**(), respectively.

**EVP\_DigestInit\_ex**() sets up the digest context *ctx* to use a digest *type*. The *type* will typically be supplied by a function such as **EVP\_sha512**(). The *ENGINE*, *\*engine* argument is always ignored and passing NULL is recommended.

**EVP\_DigestUpdate**() hashes *cnt* bytes of data at *d* into the digest context *ctx*. This function can be called several times on the same *ctx* to hash additional data.

**EVP\_DigestFinal\_ex**() retrieves the digest value from *ctx* and places it in *md*. If the *s* parameter is not NULL, then the number of bytes of data written (i.e. the length of the digest) will be written to the integer at *s*; at most EVP\_MAX\_MD\_SIZE bytes will be written. After calling **EVP\_DigestFinal\_ex**(), no additional calls to **EVP\_DigestUpdate**() can be made, but **EVP\_DigestInit\_ex**() can be called to initialize a new digest operation.

**EVP\_Digest**() is a simple wrapper function to hash *cnt* bytes of data at *d* using the digest *type* in a one-shot operation and place the digest value into *md*, and, unless *s* is NULL, the length of the digest in bytes into \**s*. This wrapper uses a temporary digest context and passes its arguments to **EVP\_DigestInit\_ex**(), **EVP\_DigestUpdate**(), and **EVP\_DigestFinal\_ex**() internally. The *ENGINE*, *\*engine* argument is always ignored and passing NULL is recommended.

**EVP\_MD\_CTX\_copy\_ex**() can be used to copy the message digest state from *in* to *out*. This is useful if large amounts of data are to be hashed which only differ in the last few bytes.

**EVP\_DigestInit**() is a deprecated function behaving like **EVP\_DigestInit\_ex**() except that it requires **EVP\_MD\_CTX\_reset**() before it can be used on a context that was already used.

**EVP\_DigestFinal**() is a deprecated function behaving like **EVP\_DigestFinal\_ex**() except that the digest context *ctx* is automatically cleaned up after use by calling **EVP\_MD\_CTX\_reset**() internally.

**EVP\_MD\_CTX\_copy**() is a deprecated function behaving like **EVP\_MD\_CTX\_copy\_ex**() except that it requires **EVP\_MD\_CTX\_reset**() before a context that was already used can be passed as *out*.

**EVP\_sha224**(), **EVP\_sha256**(), **EVP\_sha384**(), **EVP\_sha512**(), and **EVP\_ripemd160**() return *EVP\_MD* structures for the SHA-224, SHA-256, SHA-384, SHA-512 and RIPEMD-160 digest algorithms respectively.

**EVP\_sha512\_224**() and **EVP\_sha512\_256**() return an *EVP\_MD* structure that provides the truncated SHA-512 variants SHA-512/224 and SHA-512/256, respectively.

**EVP\_md\_null**() is a "null" message digest that does nothing: i.e. the hash it returns is of zero length.

**EVP\_get\_digestbyname**(), **EVP\_get\_digestbynid**(), and **EVP\_get\_digestbyobj**() return an *EVP\_MD* structure when passed a digest name, a digest NID, or an ASN1\_OBJECT structure respectively.

**EVP\_get\_digestbynid**() and **EVP\_get\_digestbyobj**() are implemented as macros.

The EVP interface to message digests should almost always be used in preference to the low-level interfaces. This is because the code then becomes transparent to the digest used and much more flexible.

The *ENGINE*, *\*engine* argument is always ignored and passing NULL is recommended.

The functions **EVP\_DigestInit**(), **EVP\_DigestFinal**(), and **EVP\_MD\_CTX\_copy**() are obsolete but are retained to maintain compatibility with existing code. New applications should use **EVP\_DigestInit\_ex**(), **EVP\_DigestFinal\_ex**(), and **EVP\_MD\_CTX\_copy\_ex**() because they can efficiently reuse a digest context instead of initializing and cleaning it up on each call.

If digest contexts are not cleaned up after use, memory leaks will occur.

## RETURN VALUES

**EVP\_MD\_CTX\_new**() and **EVP\_MD\_CTX\_create**() return the new *EVP\_MD\_CTX* object or NULL for failure.

**EVP\_MD\_CTX\_reset**(), **EVP\_MD\_CTX\_init**(), and **EVP\_MD\_CTX\_cleanup**() always return 1.

**EVP\_DigestInit\_ex**(), **EVP\_DigestUpdate**(), **EVP\_DigestFinal\_ex**(), **EVP\_Digest**(), **EVP\_MD\_CTX\_copy\_ex**(), **EVP\_DigestInit**(), **EVP\_DigestFinal**(), and **EVP\_MD\_CTX\_copy**() return 1 for success or 0 for failure.

**EVP\_MD\_CTX\_md**() returns the *EVP\_MD* object used by *ctx*, or NULL if *ctx* is NULL or does not have any message digest algorithm assigned yet.

**EVP\_md\_null**(), **EVP\_sha224**(), **EVP\_sha256**(), **EVP\_sha384**(), **EVP\_sha512**(), **EVP\_sha512\_224**(), **EVP\_sha512\_256**(), and **EVP\_ripemd160**() return pointers to constant static objects owned by the library.

**EVP\_get\_digestbyname**(), **EVP\_get\_digestbynid**(), and **EVP\_get\_digestbyobj**() return either an *EVP\_MD* structure or NULL if an error occurs.

## EXAMPLES

This example digests the data "Test Message\\n" and "Hello World\\n", using the digest name passed on the command line.

```
#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
int
main(int argc, char *argv[])
{
	EVP_MD_CTX *mdctx;
	const EVP_MD *md;
	const char mess1[] = "Test Message\n";
	const char mess2[] = "Hello World\n";
	unsigned char md_value[EVP_MAX_MD_SIZE];
	unsigned int md_len, i;
	if (argc <= 1) {
		printf("Usage: mdtest digestname\n");
		exit(1);
	}
	md = EVP_get_digestbyname(argv[1]);
	if (md == NULL) {
		printf("Unknown message digest %s\n", argv[1]);
		exit(1);
	}
	mdctx = EVP_MD_CTX_new();
	EVP_DigestInit_ex(mdctx, md, NULL);
	EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
	EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
	EVP_DigestFinal_ex(mdctx, md_value, &md_len);
	EVP_MD_CTX_free(mdctx);
	printf("Digest is: ");
	for(i = 0; i < md_len; i++)
		printf("%02x", md_value[i]);
	printf("\n");
	return 0;
}
```

## SEE ALSO

BIO\_f\_md(3), CMAC\_Init(3), evp(3), EVP\_BytesToKey(3), EVP\_DigestSignInit(3), EVP\_DigestVerifyInit(3), EVP\_MD\_CTX\_ctrl(3), EVP\_MD\_nid(3), EVP\_PKEY\_CTX\_set\_signature\_md(3), EVP\_sha1(3), EVP\_sha3\_224(3), EVP\_SignInit(3), EVP\_sm3(3), EVP\_VerifyInit(3), HMAC(3), OCSP\_basic\_sign(3), OCSP\_request\_sign(3), PKCS5\_PBKDF2\_HMAC(3), PKCS7\_sign\_add\_signer(3), X509\_ALGOR\_set0(3), X509\_digest(3), X509\_sign(3)

## HISTORY

**EVP\_DigestInit**(), **EVP\_DigestUpdate**(), and **EVP\_DigestFinal**() first appeared in SSLeay 0.5.1. **EVP\_md\_null**() and **EVP\_get\_digestbyname**() first appeared in SSLeay 0.8.0. **EVP\_get\_digestbynid**() and **EVP\_get\_digestbyobj**() first appeared in SSLeay 0.8.1. **EVP\_ripemd160**() first appeared in SSLeay 0.9.0. All these functions have been available since OpenBSD 2.4.

**EVP\_MD\_CTX\_copy**() first appeared in OpenSSL 0.9.2b and has been available since OpenBSD 2.6.

**EVP\_MD\_CTX\_md**() first appeared in OpenSSL 0.9.5 and has been available since OpenBSD 2.7.

**EVP\_MD\_CTX\_init**(), **EVP\_MD\_CTX\_create**(), **EVP\_MD\_CTX\_cleanup**(), **EVP\_MD\_CTX\_destroy**(), **EVP\_DigestInit\_ex**(), **EVP\_DigestFinal\_ex**(), **EVP\_Digest**(), and **EVP\_MD\_CTX\_copy\_ex**() first appeared in OpenSSL 0.9.7 and have been available since OpenBSD 3.2.

**EVP\_sha224**(), **EVP\_sha256**(), **EVP\_sha384**(), and **EVP\_sha512**() first appeared in OpenSSL 0.9.7h and 0.9.8a and have been available since OpenBSD 4.0.

**EVP\_MD\_CTX\_new**(), **EVP\_MD\_CTX\_reset**(), and **EVP\_MD\_CTX\_free**() first appeared in OpenSSL 1.1.0 and have been available since OpenBSD 6.3.

**EVP\_sha512\_224**() and **EVP\_sha512\_256**() first appeared in OpenSSL 1.1.1 and have been available since OpenBSD 7.4.
