harmony 鸿蒙Converting Binary Data into a Symmetric Key (ArkTS)
Converting Binary Data into a Symmetric Key (ArkTS)
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.
Converting Binary Data into a 3DES Key
For details about the algorithm specifications, see 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 error, ${e.code}, ${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, ${e.code}, ${e.message}`);
}
}
- Example using synchronous API convertKeySync: “`ts import { cryptoFramework } from ‘@kit.CryptoArchitectureKit’;
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(); let key = symKeyGenerator.convertKeySync(keyMaterialBlob); 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); }
## Converting Binary Data into an HMAC Key
For details about the algorithm specifications, see [HMAC](crypto-sym-key-generation-conversion-spec.md#hmac).
1. Obtain the HMAC binary key and encapsulate it into a **DataBlob** object.
2. Call [cryptoFramework.createSymKeyGenerator](https://m.seaxiang.com/blog/GRG9o4) with the string parameter **'HMAC'** to create a symmetric key generator (**SymKeyGenerator**) object for an HMAC key of [1, 32768] bits.
3. Call [SymKeyGenerator.convertKey](https://m.seaxiang.com/blog/GRG9o4) to convert the binary data into a symmetric key (**SymKey**).
4. Call [SymKey.getEncoded](https://m.seaxiang.com/blog/GRG9o4) to obtain the binary data of the key.
- Example: Convert binary data into an HMAC key in await mode.
```ts
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
import { buffer } from '@kit.ArkTS';
async function testConvertHmacKey() {
// The symmetric key length is 64 bytes (512 bits).
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);
}
- Example using synchronous API convertKeySync: “`ts import { cryptoFramework } from ‘@kit.CryptoArchitectureKit’; import { buffer } from ‘@kit.ArkTS’;
function testConvertKeySync() { // The symmetric key length is 64 bytes (512 bits). 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); } “`
你可能感兴趣的鸿蒙文章
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框自动聚焦