OpenSSL EVP Series 9 --- EVP

zhaozj2021-02-16  39

OpenSSL EVP Series 9 --- Evp_Digest Series Function

--- Translation according to OpenSSL DOC / CRYPTO / EVP_Digestinit.pod

(Author: DragonKing, Mail: wzhah@263.net, released in:

Http://openssl.126.com OpenSSL Professional Forum, Version: OpenSSL-0.9.7)

This example is provided by the OpenSSL Help document. This example performs a summary operation of the "Test Message / N" and "Hello World / N" strings based on the information summary algorithm input of the command line.

#include

#include

Main (int Argc, char * argv [])

{

EVP_MD_CTX MDCTX;

Const EVP_MD * MD;

Char Mess1 [] = "Test Message / N";

Char Mess2 [] = "Hello WORLD / N";

Unsigned char md_value [evp_max_md_size];

INT MD_LEN, I;

/ / Make the EVP_DIGEST series function support all valid information summary algorithms

OpenSSL_ADD_ALL_Digests ();

IF (! argv [1]) {

Printf ("USAGE: MDTest DigestName / N);

Exit (1);

}

// Get the corresponding EVP_MD algorithm structure according to the name of the input information summary function

MD = EVP_GET_DigestByname (Argv [1]);

IF (! md) {

Printf ("Unknown Message Digest% S / N", Argv [1]);

Exit (1);

}

// Initialization Information Summary Structure MDCTX, this is necessary when calling the evp_digestinit_ex function.

EVP_MD_CTX_INIT (& MDCTX);

// Use the MD algorithm structure to set the MDCTX structure, the IMPL is NULL, that is, the algorithm for default (the information summary algorithm provided by OpenSSL itself)

EVP_DIGESTINIT_EX (& MDCTX, MD, NULL);

// Start truly information summary operation, you can call this function multiple times to handle more data, here only call twice

EVP_DIGESTUPDATE (& MDCTX, Mess1, Strlen (Mess1));

EVP_DIGESTUPDATE (& MDCTX, Mess2, Strlen (Mess2));

/ / Complete the information summary calculation process, store the completed summary information in MD_VALUE, the length information is stored in MD_LEN

EVP_DIGESTFINAL_EX (& MDCTX, MD_VALUE, & MD_LEN);

// Use this function to release the resource occupied by MDCTX, if you use the _ex series function, this is a must call.

EVP_MD_CTX_CLEANUP (& MDCTX);

Printf ("Digest IS:");

For (i = 0; i

Printf ("/ n");

}

转载请注明原文地址:https://www.9cbs.com/read-23989.html

New Post(0)