This topic uses 3DES and HMAC as an example to describe how to convert binary data into a symmetric key (SymKey). That is, convert a piece of external or internal binary data into a key object for subsequent operations, such as encryption and decryption.
For the corresponding algorithm specifications, see Symmetric Key Generation and Conversion Specifications: 3DES.
Obtain the 3DES binary key data and encapsulate it into a DataBlob object.
Call cryptoFramework.createSymKeyGenerator with the string parameter '3DES192' to create a symmetric key generator (SymKeyGenerator) object for a 192-bit 3DES key.
Call SymKeyGenerator.convertKey to convert the binary data into a symmetric key (SymKey).
Call SymKey.getEncoded to obtain the binary data of the key.
Example: Convert binary data into a 192-bit 3DES key (using callback-based APIs).
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
function genKeyMaterialBlob(): cryptoFramework.DataBlob {
let arr = [
0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c,
0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30, 0x72]; // The key length is 192 bits, that is, 24 bytes.
let keyMaterial = new Uint8Array(arr);
return { data: keyMaterial };
}
function testConvertSymKey() {
// Create a SymKeyGenerator instance.
let symKeyGenerator = cryptoFramework.createSymKeyGenerator('3DES192');
// Convert the data into a symmetric key.
let keyMaterialBlob = genKeyMaterialBlob();
try {
symKeyGenerator.convertKey(keyMaterialBlob, (error, key) => {
if (error) {// If the service logic fails to be executed, the first parameter of the callback returns error information, that is, throw an exception asynchronously.
let e: BusinessError = error as BusinessError;
console.error(`convertKey failed: errCode: ${e.code}, message: ${e.message}`);
return;
}
console.info('key algName: ' + key.algName);
console.info('key format: ' + key.format);
let encodedKey = key.getEncoded(); // Obtain the binary data of the symmetric key and output the data as a byte array. The length is 24 bytes.
console.info('key getEncoded hex: ' + encodedKey.data);
})
} catch (error) {// Throw an exception immediately when an error is detected during parameter check.
let e: BusinessError = error as BusinessError;
console.error(`convertKey failed: errCode: ${e.code}, message: ${e.message}`);
}
} Example using synchronous API convertKeySync:
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
function genKeyMaterialBlob(): cryptoFramework.DataBlob {
let arr = [
0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c,
0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30, 0x72]; // The key length is 192 bits, that is, 24 bytes.
let keyMaterial = new Uint8Array(arr);
return { data: keyMaterial };
}
function testConvertSymKey() {
// Create a SymKeyGenerator instance.
let symKeyGenerator = cryptoFramework.createSymKeyGenerator('3DES192');
// Convert the data into a symmetric key.
let keyMaterialBlob = genKeyMaterialBlob();
try {
let key = symKeyGenerator.convertKeySync(keyMaterialBlob);
console.info('key algName: ' + key.algName);
console.info('key format: ' + key.format);
let encodedKey = key.getEncoded(); // Obtain the binary data of the symmetric key, which is 24 bytes in length.
console.info('key getEncoded length: ' + encodedKey.data.length);
} catch (error) {// Throw an exception immediately when an error is detected during parameter check.
let e: BusinessError = error as BusinessError;
console.error(`convertKeySync failed: errCode: ${e.code}, message: ${e.message}`);
}
} For the corresponding algorithm specifications, see Symmetric Key Generation and Conversion Specifications: HMAC.
Obtain the HMAC binary key data and encapsulate it into a DataBlob object.
Call cryptoFramework.createSymKeyGenerator with the string parameter 'HMAC' to create a symmetric key generator (SymKeyGenerator) for the HMAC algorithm, which supports key lengths ranging from [1, 4096] bytes.
Call SymKeyGenerator.convertKey to convert the binary data into a symmetric key (SymKey).
Call SymKey.getEncoded to obtain the binary data of the key.
Example using await to generate a HMAC key:
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { buffer } from '@kit.ArkTS';
async function testConvertHmacKey() {
try {
// The symmetric key is 64 bytes (512 bits) long.
let keyMessage = '12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh';
let keyBlob: cryptoFramework.DataBlob = {
data: new Uint8Array(buffer.from(keyMessage, 'utf-8').buffer)
}
let symKeyGenerator = cryptoFramework.createSymKeyGenerator('HMAC');
let key = await symKeyGenerator.convertKey(keyBlob);
let encodedKey = key.getEncoded();
console.info('key encoded data: ' + encodedKey.data);
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error(`call failed: errCode: ${e.code}, errMsg: ${e.message}`);
}
} Example using synchronous API convertKeySync:
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { buffer } from '@kit.ArkTS';
function testConvertKeySync() {
try {
// The symmetric key is 64 bytes (512 bits) long.
let keyMessage = '12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh';
let keyBlob: cryptoFramework.DataBlob = {
data : new Uint8Array(buffer.from(keyMessage, 'utf-8').buffer)
}
let symKeyGenerator = cryptoFramework.createSymKeyGenerator('HMAC');
let key = symKeyGenerator.convertKeySync(keyBlob);
let encodedKey = key.getEncoded();
console.info('key encoded data: ' + encodedKey.data);
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error(`call failed: errCode: ${e.code}, errMsg: ${e.message}`);
}
}