Message Digest Calculation MD5

Note:

Currently in the beta phase.

For the corresponding algorithm specifications, please refer to Message Digest Calculation Algorithm Specifications.

Development Steps

When passing data through the update interface, you can either pass all data at once or manually segment the data and then update in segments. For the same segment of data, the calculation results will be identical. For large volumes of data, developers can choose whether to pass the data in segments based on actual requirements.

Below are example codes for both approaches.

Digest Algorithm (Single Pass)

  1. Call createMd, specify the digest algorithm MD5, and generate a digest instance (Md).

  2. Call update, pass the custom message, and perform the digest update calculation. There is no limit on the length of a single update.

  3. Call digest to obtain the digest calculation result.

  4. Call getMdLength to get the digest calculation length in bytes.

Example: Single Pass Data to Obtain Digest Calculation Result

import kit.CryptoArchitectureKit.*
import ohos.hilog.Hilog

func doMd() {
    let mdAlgName = 'MD5' // Digest algorithm name.
    let message = 'mdTestMessage' // Data to be digested.
    let md = createMd(mdAlgName)
    // When the data volume is small, you can perform a single update and pass all data at once. The interface does not limit the input length.
    md.update(DataBlob(message.toArray()))
    let mdResult = md.digest()
    Hilog.info(0,"",'[Sync]:Md result: ${mdResult.data}')
    let mdLen = md.getMdLength()
    Hilog.info(0,"","md len: ${mdLen}")
}

Segmented Digest Algorithm

  1. Call createMd, specify the digest algorithm MD5, and generate a digest instance (Md).

  2. Pass the custom message, set the single-pass data volume to 20 bytes, and call update multiple times to perform the digest update calculation.

  3. Call digest to obtain the digest calculation result.

  4. Call getMdLength to get the digest calculation length in bytes.

Example: Segmented Data Pass to Obtain Digest Calculation Result

import kit.CryptoArchitectureKit.*
import ohos.hilog.Hilog

func doLoopMd() {
    let mdAlgName = "MD5" // Digest algorithm name.
    let md = createMd(mdAlgName)
    // Assume the total message is 43 bytes, which remains 43 bytes after UTF-8 decoding.
    let messageText = "aaaaa.....bbbbb.....ccccc.....ddddd.....eee"
    let messageData = messageText.toArray()
    let updateLength = 20 // Assume segmenting updates in 20-byte units (no actual requirement exists).
    let size = messageData.size
    for (i in 0..size : updateLength) {
        let len = if (i + updateLength > size) {
            size
        } else {
            i + updateLength
        }
        let updateMessage = messageData[i..len]
        let updateMessageBlob: DataBlob = DataBlob(updateMessage)
        md.update(updateMessageBlob)
    }
    let mdOutput = md.digest()
    Hilog.info(0,"",'[Sync]:Md result: ${mdOutput.data}')
    let mdLen = md.getMdLength()
    Hilog.info(0,"","md len: ${mdLen}")
}