OpenSSL EVP hashing algorithm (md5,sha1,sha256)

Source: Internet
Author: User
Tags sha1

1. Brief description

OpenSSL offers a wealth of cryptographic tools, some commonly used hashing algorithms

For example, Md5,sha can be used directly with the provided md5.h, sha.h interface;

For the convenience of developers, OpenSSL also provides an evp, evp.h the file with a variety of common tools;

Man EVP can be informed that EVP is a higher-level cryptographic tool provided by openssl,

Can be understood as an encapsulation of the various interfaces provided

EVP files contain many more, this time mainly explains the hashing algorithm provided by EVP

2. Example SHA512 code

To import evp.h using

EVP_MD_CTX * evpCtx = EVP_MD_CTX_new ();
     EVP_DigestInit_ex (evpCtx, EVP_sha512 (), NULL);
     char * data = (char *) "hello";
     unsigned int len = strlen (data);
// hash calculation
     EVP_DigestUpdate (evpCtx, data, len);
     unsigned char result [SHA512_DIGEST_LENGTH] = {0};
// Return result
     EVP_DigestFinal_ex (evpCtx, result, & len);
     hex_print ("sha512", result, SHA512_DIGEST_LENGTH);
// Use a function directly
     unsigned char resultT [SHA512_DIGEST_LENGTH] = {0};
     EVP_Digest ("hello", 5, resultT, NULL, EVP_sha512 (), NULL);
     hex_print ("sha512", resultT, SHA512_DIGEST_LENGTH);

The above test EVP provides two ways to perform sha512

3. According to the internal Evp.h provided:

The above is used evp_512 (); then the others are:

const EVP_MD *EVP_md2(void);
const EVP_MD *EVP_md4(void);
const EVP_MD *EVP_md5(void);
const EVP_MD *EVP_md5_sha1(void);
const EVP_MD *EVP_blake2b512(void);
const EVP_MD *EVP_blake2s256(void);
const EVP_MD *EVP_sha1(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);

The calculated length of the defined hash algorithm

# define SHA224_DIGEST_LENGTH    28
# define SHA256_DIGEST_LENGTH    32
# define SHA384_DIGEST_LENGTH    48
# define SHA512_DIGEST_LENGTH    64

4. Other interfaces provided by OpenSSL can also be used directly if not provided by EVP

md5.h, sha.h

As in Sha.h about the interface provided by sha512

int SHA384_Init(SHA512_CTX *c);
int SHA384_Update(SHA512_CTX *c, const void *data, size_t len);
int SHA384_Final(unsigned char *md, SHA512_CTX *c);
unsigned char *SHA384(const unsigned char *d, size_t n, unsigned char *md);
int SHA512_Init(SHA512_CTX *c);
int SHA512_Update(SHA512_CTX *c, const void *data, size_t len);
int SHA512_Final(unsigned char *md, SHA512_CTX *c);
unsigned char *SHA512(const unsigned char *d, size_t n, unsigned char *md);

There are two different ways to call It: the following example

     SHA512_CTX ctx;
     SHA512_Init (& ctx);
     char * data = (char *) "hello";
     unsigned int len = strlen (data);
     unsigned char result [SHA512_DIGEST_LENGTH] = {0};
     // Calculate the hash
     SHA512_Update (& ctx, data, len);
     // take the result
     SHA512_Final (result, & ctx);
     hex_print ("sha512", result, SHA512_DIGEST_LENGTH);
     // a function call
     unsigned char resultT [SHA512_DIGEST_LENGTH] = {0};
     SHA512 ("hello", 5, resultT);
     hex_print ("sha512", resultT, SHA512_DIGEST_LENGTH);

5. Finally attached, the Hex_print code in the above example

static void hex_print(const char *name, const unsigned char *buf, size_t len)
    size_t i;
    fprintf(stderr, "%s ", name);
    for (i = 0; i < len; i++)
        fprintf(stderr, "%02X", buf[i]);
    fputs("\n", stderr);


OpenSSL 1.1.0c Source code

OpenSSL EVP hashing algorithm (md5,sha1,sha256)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.