HMAC (Cangjie)

Note:

Currently in the beta phase.

HMAC stands for Hash-based Message Authentication Code with keyed-hashing. For specific usage scenarios and supported algorithm specifications, please refer to HMAC Introduction and Algorithm Specifications.

Development Procedure

Generate Key

  1. Specify a key alias.

  2. Initialize the key property set.

  3. Call generateKeyItem to generate the key. For HMAC-supported specifications, see Key Generation.

Alternatively, developers can refer to the specifications in Key Import to import an existing key.

Perform HMAC

  1. Obtain the key alias.

  2. Obtain the data to be processed.

  3. Call initSession to initialize the key session and obtain the session handle.

  4. Call finishSession to complete the key session and obtain the HMAC result.

Example

/*
 * The following demonstrates HMAC key operations
 */
import kit.PerformanceAnalysisKit.Hilog
import kit.BasicServicesKit.*
import kit.CoreFileKit.*
import kit.AbilityKit.*
import kit.UniversalKeystoreKit.*

let HmacKeyAlias = 'test_HMAC' // Key alias, user-defined for key generation
var handle: ?HuksHandleId = None
let plainText = '123456' // Data to be processed
var hashData: ?Array<UInt8> = None // HMAC-processed data

func StringToUint8Array(str: String) {
    return str.toArray()
}

func Uint8ArrayToString(fileData: Array<UInt8>) {
    return String.fromUtf8(fileData)
}

func GetHMACProperties() {
    let properties: Array<HuksParam> = [
        HuksParam(
            HuksTag.HUKS_TAG_ALGORITHM,
            HuksParamValue.Uint32Value(HuksKeyAlg.HUKS_ALG_HMAC)
        ),
        HuksParam(
            HuksTag.HUKS_TAG_KEY_SIZE,
            HuksParamValue.Uint32Value(HuksKeySize.HUKS_AES_KEY_SIZE_256)
        ),
        HuksParam(
            HuksTag.HUKS_TAG_PURPOSE,
            HuksParamValue.Uint32Value(HuksKeyPurpose.HUKS_KEY_PURPOSE_MAC)
        ),
        HuksParam(
            HuksTag.HUKS_TAG_DIGEST,
            HuksParamValue.Uint32Value(HuksKeyDigest.HUKS_DIGEST_SHA384)
        )
    ]
    return properties
}

/*
 * Simulate key generation scenario
 */
func GenerateHMACKey() {
    // Obtain key generation algorithm parameter configuration
    let genProperties = GetHMACProperties()
    let options: HuksOptions = HuksOptions(properties: genProperties, inData: Bytes())
    // Call generateKeyItem to generate the key. HmacKeyAlias is the key alias specified during key generation
    generateKeyItem(HmacKeyAlias, options)
}

/*
 * Simulate HMAC computation scenario
 */
func HMACData() {
    // Obtain HMAC algorithm parameter configuration
    let hmacProperties = GetHMACProperties()
    let options: HuksOptions = HuksOptions(
        properties: hmacProperties,
        inData: StringToUint8Array(plainText)
    )
    // Call initSession to obtain the handle. HmacKeyAlias is the key alias specified during key generation
    handle = initSession(HmacKeyAlias, options)
    // Call finishSession to obtain the HMAC result
    hashData = finishSession(handle.getOrThrow(), options)
}