harmony 鸿蒙Generating a CMAC
Generating a CMAC
A Cipher-based Message Authentication Code (CMAC) is generated by using a block cipher (such as AES) and a key. It is used to verify that a message has not been alerted during transmission.
How to Develop
During the CMAC operation, you can pass in all the data at a time or pass in data by segment. The same data will produce the same result no matter how the data is passed. Use the appropriate method based on the data size.
The following provides examples of CMAC operations with different data passing methods.
Generating a CMAC by Passing In Full Data
Call cryptoFramework.createMac with the MAC algorithm set to CMAC and symmetric key algorithm set to AES256 to create a MAc instance.
Call cryptoFramework.createSymKeyGenerator and SymKeyGenerator.convertKey to generate a symmetric key (SymKey) using AES256. For details, see Converting Binary Data into a Symmetric Key.
Call Mac.init to initialize the Mac instance using the shared symmetric key (SymKey).
Call Mac.update to pass in the full data. The amount of data to be passed in by a single Mac.update() call is not limited.
Call Mac.doFinal to generate a MAC.
Call Mac.getMacLength to obtain the length of the MAC, in bytes.
Example: Pass in the full data to generate a CMAC using await.
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer } from '@kit.ArkTS';
async function genSymKeyByData(symKeyData: Uint8Array) {
let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128');
let symKey = await aesGenerator.convertKey(symKeyBlob);
console.info('convertKey success');
return symKey;
}
async function doCmac() {
// Convert the string into a Uint8Array in UTF-8 format and use it as the private key, which is 128 bits (16 bytes).
let keyData = new Uint8Array(buffer.from("12345678abcdefgh", 'utf-8').buffer);
let key = await genSymKeyByData(keyData);
let spec: cryptoFramework.CmacSpec = {
algName: "CMAC",
cipherName: "AES128",
};
let message = 'cmacTestMessage'; // Message used to generate a CMAC.
let mac = cryptoFramework.createMac(spec);
await mac.init(key);
// If there is a small amount of data to be processed, call update() to pass in all the data at a time. The amount of data to be passed in by a single update() call is not limited.
await mac.update({ data: new Uint8Array(buffer.from(message, 'utf-8').buffer) });
let macResult = await mac.doFinal();
console.info('CMAC result:' + macResult.data);
let macLen = mac.getMacLength();
console.info('CMAC len:' + macLen);
}
- Example: Pass in the full data to calculate a CMAC using synchronous APIs.
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer } from '@kit.ArkTS';
function genSymKeyByData(symKeyData: Uint8Array) {
let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128');
let symKey = aesGenerator.convertKeySync(symKeyBlob);
console.info('[Sync]convertKey success');
return symKey;
}
function doCmacBySync() {
// Convert the string into a Uint8Array in UTF-8 format and use it as the private key, which is 128 bits (16 bytes).
let keyData = new Uint8Array(buffer.from("12345678abcdefgh", 'utf-8').buffer);
let key = genSymKeyByData(keyData);
let spec: cryptoFramework.CmacSpec = {
algName: "CMAC",
cipherName: "AES128",
};
let message = 'cmacTestMessage'; // Message used to generate a CMAC.
let mac = cryptoFramework.createMac(spec);
mac.initSync(key);
// If there is a small amount of data to be processed, call update() to pass in all the data at a time. The amount of data to be passed in by a single update() call is not limited.
mac.updateSync({ data: new Uint8Array(buffer.from(message, 'utf-8').buffer) });
let macResult = mac.doFinalSync();
console.info('[Sync]CMAC result:' + macResult.data);
let macLen = mac.getMacLength();
console.info('CMAC len:' + macLen);
}
Generating a CMAC by Passing In Data by Segment
Call cryptoFramework.createMac with the MAC algorithm set to CMAC and symmetric key algorithm set to AES256 to create a MAc instance.
Call cryptoFramework.createSymKeyGenerator and SymKeyGenerator.convertKey to generate a symmetric key (SymKey) using AES256. For details, see Converting Binary Data into a Symmetric Key.
Call Mac.init to initialize the Mac instance using the shared symmetric key (SymKey).
Call Mac.update multiple times to pass in 20 bytes each time.
Call Mac.doFinal to generate a MAC.
Call Mac.getMacLength to obtain the length of the MAC, in bytes.
Example: Pass in data by segment to generate a CMAC using await.
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer } from '@kit.ArkTS';
async function genSymKeyByData(symKeyData: Uint8Array) {
let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128');
let symKey = await aesGenerator.convertKey(symKeyBlob);
console.info('convertKey success');
return symKey;
}
async function doLoopCmac() {
// Convert the string into a Uint8Array in UTF-8 format and use it as the private key, which is 128 bits (16 bytes).
let keyData = new Uint8Array(buffer.from("12345678abcdefgh", 'utf-8').buffer);
let key = await genSymKeyByData(keyData);
let spec: cryptoFramework.CmacSpec = {
algName: "CMAC",
cipherName: "AES128",
};
let mac = cryptoFramework.createMac(spec);
// In this example, the message is of 43 bytes. After decoded in UTF-8 format, the message is also of 43 bytes.
let messageText = "aaaaa......bbbbb......ccccc......ddddd......eee";
let messageData = new Uint8Array(buffer.from(messageText, 'utf-8').buffer);
let updateLength = 20; // Pass in 20 bytes each time. You can set this parameter as required.
await mac.init(key);
for (let i = 0; i < messageData.length; i += updateLength) {
let updateMessage = messageData.subarray(i, i + updateLength);
let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
await mac.update(updateMessageBlob);
}
let macOutput = await mac.doFinal();
console.info("CMAC result: " + macOutput.data);
let macLen = mac.getMacLength();
console.info('CMAC len:' + macLen);
}
- Example: Pass in data by segment to calculate a CMAC using synchronous APIs.
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer } from '@kit.ArkTS';
function genSymKeyByData(symKeyData: Uint8Array) {
let symKeyBlob: cryptoFramework.DataBlob = { data: symKeyData };
let aesGenerator = cryptoFramework.createSymKeyGenerator('AES128');
let symKey = aesGenerator.convertKeySync(symKeyBlob);
console.info('[Sync]convertKey success');
return symKey;
}
function doLoopCmacBySync() {
// Convert the string into a Uint8Array in UTF-8 format and use it as the private key, which is 128 bits (16 bytes).
let keyData = new Uint8Array(buffer.from("12345678abcdefgh", 'utf-8').buffer);
let key = genSymKeyByData(keyData);
let spec: cryptoFramework.CmacSpec = {
algName: "CMAC",
cipherName: "AES128",
};
let mac = cryptoFramework.createMac(spec);
// In this example, the message is of 43 bytes. After decoded in UTF-8 format, the message is also of 43 bytes.
let messageText = "aaaaa.....bbbbb.....ccccc.....ddddd.....eee";
let messageData = new Uint8Array(buffer.from(messageText, 'utf-8').buffer);
let updateLength = 20; // Pass in 20 bytes each time. You can set this parameter as required.
mac.initSync(key);
for (let i = 0; i < messageData.length; i += updateLength) {
let updateMessage = messageData.subarray(i, i + updateLength);
let updateMessageBlob: cryptoFramework.DataBlob = { data: updateMessage };
mac.updateSync(updateMessageBlob);
}
let macOutput = mac.doFinalSync();
console.info("[Sync]CMAC result: " + macOutput.data);
let macLen = mac.getMacLength();
console.info('CMAC len:' + macLen);
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Crypto Architecture Kit
harmony 鸿蒙Encryption and Decryption with a 3DES Symmetric Key (ECB Mode) (C/C++)
harmony 鸿蒙Encryption and Decryption with a 3DES Symmetric Key (ECB Mode) (ArkTS)
harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CBC Mode) (C/C++)
harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CBC Mode) (ArkTS)
harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CCM Mode) (C/C++)
harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (CCM Mode) (ArkTS)
harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (ECB Mode) (C/C++)
harmony 鸿蒙Encryption and Decryption with an AES Symmetric Key (ECB Mode) (ArkTS)
harmony 鸿蒙Encryption and Decryption by Segment with an AES Symmetric Key (GCM Mode) (C/C++)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦