MD type BIO
--- Translation according to OpenSSL DOC / CRYPTO / BIO_F_MD.POD and its own understanding
(Author: DragonKing, Mail: wzhah@263.net, Posted on: http: //gdwzh.126.com of openssl professional forum)
This type is filter type BIO, which is defined as follows (OpenSSL / BIO.H, OpenSSL / EVP.H):
BIO_METHOD * BIO_F_MD (VOID);
INT BIO_SET_MD (BIO * B, EVP_MD * MD);
INT BIO_GET_MD (BIO * B, EVP_MD ** MDP);
INT BIO_GET_MD_CTX (BIO * B, EVP_MD_CTX ** MDCP);
As with the Cipher type, some definitions and implementations of this type are in EVP / BIO_MD.C instead of in the Bio directory. If you want to see the source file, please refer to this file.
[BIO_F_MD]
This function returns a MD type BIO_METHOD structure, which is defined as follows:
Static Bio_Method Methods_md =
{
BIO_TYPE_MD, "Message Digest",
MD_WRITE,
MD_READ,
NULL, / * MD_PUTS, * /
MD_GETS,
Md_ctrl,
MD_NEW,
Md_free,
Md_callback_ctrl,
}
MD type BIO performs a summary operation over any of its data, in fact, the type BIO encapsulates the functions and behaviors of EVP_DIGESTINIT, EVP_DIGESTUPDATE and EVP_DigestFinal three functions. This type of BIO is completely symmetrical, that is, no matter whether it is reading data (BIO_READ) or writing data (Bio_WRITE), the same summary operation is performed.
When the BIO_GETS function is executed, if a given Size parameter is large enough, you can complete the summary calculation, then the summary value will be returned. The BIO_PUTS function is not supported. If this function needs to be supported, you can add a BUFFER type Bio in front.
The BIO_RESET function re-initializes a summary type Bio, in fact, it is simple to call the EVP_Digestinit function for initialization.
Note that after reading the summary information from a summary BIO, the BIO_RESET or BIO_SET_MD must be called before re-use the BIO.
[BIO_SET_MD]
This function is a macro definition function of a BIO_CTRL function that uses parameter MD to set a summary algorithm for a given BIO. This function must be called before performing the read and write operation, used to initialize a summary type of BIO. The call successfully returns 1, otherwise it returns 0.
[BIO_GET_MD]
This function is also a macro definition of the BIO_CTRL function. It returns the pointer to the BIO summary method to the MDP parameter. The call successfully returns 1, otherwise it returns 0.
[BIO_GET_MD_CTX]
This function returns the method structure of the summary BIO to the MDCP parameter. This structure can be used as a parameter in the EVP_DIGESTFINAL, EVP_SIGNFINAL and EVP_VERIFINAL functions, which adds flexibility. Since the structure returned by the function is a structure inside a BIO, any change operation of the structure affects the corresponding BIO, and if the BIO is released, the structural pointer is invalid. The call successfully returns 1, otherwise it returns 0.
[Examples] 1. The following examples create a Bio chain containing the SHA1 and MD5 Type Summary BIO, and performs data "Hello World" through them.
BIO * BIO, * MDTMP;
Char message [] = "Hello World";
BIO = BIO_NEW (BIO_S_NULL ());
MDTMP = BIO_NEW (BIO_F_MD ());
BIO_SET_MD (MDTMP, EVP_SHA1 ());
// Use Bio_Push to add a Sink type Bio in front of the BIO chain, as a flag started as a Bio chain
BIO = BIO_PUSH (MDTMP, BIO);
MDTMP = BIO_NEW (BIO_F_MD ());
BIO_SET_MD (MDTMP, EVP_MD5 ());
BIO = BIO_PUSH (MDTMP, BIO);
/ * Note that the MDTMP variable is now available * /
Bio_write (Bio, Message, Strlen (Message)); // Because the last Bio is a Null type Bio, the data is actually automatically discarded.
2. The following example demonstrates the process of reading data from a summary type BIO:
BIO * BIO, * MDTMP;
Char BUF [1024];
Int rdlen;
BIO = BIO_NEW_FILE (File, "RB");
MDTMP = BIO_NEW (BIO_F_MD ());
BIO_SET_MD (MDTMP, EVP_SHA1 ());
BIO = BIO_PUSH (MDTMP, BIO);
MDTMP = BIO_NEW (BIO_F_MD ());
BIO_SET_MD (MDTMP, EVP_MD5 ());
BIO = BIO_PUSH (MDTMP, BIO);
Do {
Rdlen = BIO_READ (Bio, BUF, SIZEOF (BUF));
/ * Can join the code of processing data in this * /
WHILE (RDLEN> 0);
3. The following example reads the summary data from a BIO chain and outputs. It can be used with the example above.
BIO * MDTMP;
Unsigned char mdbuf [evp_max_md_size];
Int mdlen;
INT I;
MDTMP = BIO; / * Here, the BIO has been set up * /
Do {
EVP_MD * MD;
MDTMP = BIO_FIND_TYPE (MDTMP, BIO_TYPE_MD);
IF (! MDTMP) Break;
BIO_GET_MD (MDTMP, & MD);
Printf ("% s DiGest", obj_nid2sn (eVP_md_type (md)));
MDLEN = BIO_GETS (MDTMP, MDBUF, EVP_MAX_MD_SIZE);
For (i = 0; I Printf ("/ n"); MDTMP = BIO_NEXT (MDTMP); WHILE (MDTMP); BIO_FREE_ALL (BIO);