Encoding Hashed Data

When creating a hashed message, there can be multiple hash algorithms and multiple hashes. The following illustration depicts the tasks required to encode a hashed message and the tasks are described in the list that follows it.

    To create a hashed message
  1. Get a pointer to the data to be hashed.
  2. Determine the hash algorithm to be used.
  3. Put the data through a hashing function using the hash algorithm.
  4. Include the original data to be hashed, the hashing algorithms, and the hashes in the encoded message.

To use low-level message functions to accomplish the tasks just outlined, use the following procedure.

    To encode a hashed message using CryptoAPI
  1. Create or retrieve the content.
  2. Get a cryptographic provider.
  3. Initialize the CMSG_HASHED_ENCODE_INFO structure.
  4. Call CryptMsgCalculateEncodedLength to get the size of the encoded message blob. Allocate memory for it.
  5. Call CryptMsgOpenToEncode passing in CMSG_HASHED for dwMsgType, and a pointer to CMSG_HASHED_ENCODE_INFO for pvMsgEncodeInfo. As a result of this call, you get a handle to the opened message.
  6. Call CryptMsgUpdate, passing in the handle retrieved in step 5, and a pointer to the data that is to be hashed and encoded. This function can be called as many times as necessary to complete the encoding process.
  7. Call CryptMsgGetParam, passing in the handle retrieved in step 5, and the appropriate parameter types to access the desired, encoded data. For example, pass in CMSG_CONTENT_PARAM to get a pointer to the entire PKCS #7 message.

    If the result of this encoding is to be used as the inner data for another encoded message, such as an enveloped message, the CMSG_BARE_CONTENT_PARAM parameter must be passed. For example code showing this, see Enveloped Message Example 2.

  8. Close the message by calling CryptMsgClose.

The result of this procedure is an encoded message that contains the original data, the hashing algorithms, and the hash of that data. A pointer to the encoded message blob is obtained in step 7.