func verifyEncryptedPayload()

in sdk/communication/AzureCommunicationChat/Source/NotificationUtils/CryptoUtils.swift [75:96]


func verifyEncryptedPayload(cipherModeIVCipherText: [UInt8], authKey: String, actualHmac: [UInt8]) throws -> Bool {
    // 1.Calculate SHA256 key
    guard let data = Data(base64Encoded: authKey) else {
        throw AzureError
            .client(
                "Failed to initialize a data object with the given authKey. Please ensure the authKey is a Base64 encoded string."
            )
    }
    let digest = SHA256.hash(data: data)
    let key = SymmetricKey(data: digest.data)

    // 2.Computed HMAC signature
    let signature = HMAC<SHA256>.authenticationCode(for: Data(cipherModeIVCipherText), using: key)
    let calculatedHMACHex = Data(signature).map { String(format: "%02hhx", $0) }.joined()
    print("calculatedMac:\(calculatedHMACHex)")

    // 3.Included HMAC signature
    let actualHMACHex = Data(actualHmac).map { String(format: "%02hhx", $0) }.joined()
    print("actualHmac:\(actualHMACHex)")

    return actualHMACHex == calculatedHMACHex
}