diff --git a/LiteWearable/entry/src/main/js/MainAbility/cryptoFramework/cryptoFramework.js b/LiteWearable/entry/src/main/js/MainAbility/cryptoFramework/cryptoFramework.js index a15bc8599dcf52fc8e6fa527d9e1455e2df25335..335bb5f101208a074d75b58f2b43d7f48571ba94 100644 --- a/LiteWearable/entry/src/main/js/MainAbility/cryptoFramework/cryptoFramework.js +++ b/LiteWearable/entry/src/main/js/MainAbility/cryptoFramework/cryptoFramework.js @@ -6,7 +6,7 @@ export default { title: "" }, onInit() { - this.title = "Hello World"; + this.title = 'Hello World'; } }; // [EndExclude do_md] @@ -20,31 +20,32 @@ function stringToUint8Array(str) { } function doMd() { - let mdAlgName = 'SHA256'; // 摘要算法名 - let message = 'mdTestMessage'; // 待摘要的数据 + let mdAlgName = 'SHA256'; // Abstract algorithm name. + let message = 'mdTestMessage'; // The data to be summarized. let handle; let mdResult; let mdLen; - //指定摘要算法SHA256,生成摘要操作实例 + // Specify the digest algorithm SHA256 and generate an instance of the digest operation. try { handle = cryptoFramework.createMd(mdAlgName); } catch (error) { console.error(`createMd error, code: ${error.code}, msg: ${error.message}`); } try { - // 数据量较少时,可以只做一次update,将数据全部传入,接口未对入参长度做限制 + // When the data volume is small, only one update operation can be performed, and all the data can be sent in. + // The interface does not impose any restrictions on the length of the input parameters. handle?.updateSync({ data: stringToUint8Array(message) }); } catch (error) { console.error(`updateSync error, code:+${error.code}, msg: ${error.message}`); } - // 获取摘要计算结果。 + // Obtain the summary calculation results. try { mdResult = handle?.digest(); } catch (error) { console.error(`digest error, code: ${error.code}, msg: ${error.message}`); } console.info('Md result:' + mdResult?.data); - // 获取摘要计算长度,单位为字节 + // Obtain the length of the summary calculation, with the unit being bytes. try { mdLen = handle?.getMdLength(); } catch (error) { @@ -58,7 +59,7 @@ function doRand() { let rand; let ret = true; let randData - // 生成随机数操作实例 + // Example of generating random numbers operation. try { rand = cryptoFramework.createRandom(); } catch (error) { @@ -66,7 +67,7 @@ function doRand() { console.error(`createRandom error, code:+${error.code}, msg: ${error.message}`); } let len = 24; // Generate a 24-byte random number. - // (可选)调用Random.setSeed,为随机数生成池设置种子 + // (Optional) Call Random.setSeed to set the seed for the random number generation pool. let seed = new Uint8Array([1, 2, 3]); try { rand?.setSeed({ data: seed }); @@ -76,7 +77,7 @@ function doRand() { } try { - //生成安全随机数 + // Generate secure random numbers. randData = rand?.generateRandomSync(len); } catch (error) { ret = false; diff --git a/LiteWearable/entry/src/main/js/MainAbility/security/HMAC.js b/LiteWearable/entry/src/main/js/MainAbility/security/HMAC.js index c3048f3cf141a0733ac714f956ba137d2ed27a00..97784c83dc3cb4e4135985f1decb8ff259cb3194 100644 --- a/LiteWearable/entry/src/main/js/MainAbility/security/HMAC.js +++ b/LiteWearable/entry/src/main/js/MainAbility/security/HMAC.js @@ -2,31 +2,31 @@ // [Start hmac_process] import huks from '@ohos.security.huks'; -// HMACKeyAlias 别名,用于区分生成的KEY +// HMACKeyAlias - Alias used to distinguish the generated KEY. const HMAC_KEY_ALIAS = 'HMACKeyAlias'; // [StartExclude hmac_generate] -// 明文,加密前数据 +// Plain text, data before encryption. let plainText = 'HMACSAdffssghABC5612345612345192'; -// 密文,存放加密后数据 +// Ciphertext, storing the encrypted data. let cipherText = ''; -// 操作句柄 +// Operation handle. let handle; // [EndExclude hmac_generate] // [StartExclude hmac_process] function getHMACGenProperties() { let properties = new Array(); let index = 0; - // 算法 + // algorithm properties[index++] = { tag: huks.HuksTag.HUKS_TAG_ALGORITHM, value: huks.HuksKeyAlg.HUKS_ALG_AES }; - // 密钥长度 + // key length. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256 }; - // 密钥用途 + // Key usage. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_PURPOSE, value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_MAC @@ -69,22 +69,22 @@ function stringToUnit8Array(str) { function getHMACProperties() { let properties = new Array(); let index = 0; - // 算法 + // algorithm. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_ALGORITHM, value: huks.HuksKeyAlg.HUKS_ALG_HMAC }; - // 密钥长度 + // key length. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_256 }; - // 密钥用途 + // Key usage. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_PURPOSE, value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_MAC }; - // 摘要算法 + // digest algorithm. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_DIGEST, value: huks.HuksKeyPurpose.HUKS_DIGEST_SHA256 @@ -152,7 +152,7 @@ function HMACProcess() { } }); } else { - // HMAC密文接收 + // HMAC ciphertext reception. cipherText = uint8ArrayToString(finishData.outData); } }); diff --git a/LiteWearable/entry/src/main/js/MainAbility/security/KeyAlias.js b/LiteWearable/entry/src/main/js/MainAbility/security/KeyAlias.js index 2bf669097719b0037752f788cfc25662954772b7..af84aa3556a8c250d5f4590f82a081f6bed208f8 100644 --- a/LiteWearable/entry/src/main/js/MainAbility/security/KeyAlias.js +++ b/LiteWearable/entry/src/main/js/MainAbility/security/KeyAlias.js @@ -3,30 +3,31 @@ import huks from '@ohos.security.huks'; // [StartExclude export_key] -// 密钥材料 +// Key material. let plainTextKey = new Uint8Array([ 0x1d, 0x2c, 0x3a, 0x4b, 0x5e, 0x6f, 0x7d, 0x8a, 0x9c, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf1, 0x23 ]); // [EndExclude export_key] -// 确定密钥别名 +// Confirm the key alias. const KEY_ALIAS = 'keyAlias'; // [StartExclude export_key] -// 封装密钥属性集和密钥材料 +// Package the set of key attributes and key materials. function getImportKeyProperties() { let properties = new Array(); let index = 0; - // 算法 + // algorithm. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_ALGORITHM, value: huks.HuksKeyAlg.HUKS_ALG_AES }; - // 秘钥长度 (128/192/256) + // Key length (128/192/256). properties[index++] = { tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, value: huks.HuksKeySize.HUKS_AES_KEY_SIZE_128 }; - // 秘钥用途,生成秘钥时使用可以限制秘钥的使用权限 (AES一般用于加密、解密) + // Key usage: When generating the key, + // using it can limit the usage rights of the key (AES is generally used for encryption and decryption). properties[index++] = { tag: huks.HuksTag.HUKS_TAG_PURPOSE, value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT @@ -34,7 +35,7 @@ function getImportKeyProperties() { return properties } -// 明文导入密钥 +// Explicitly imported key. function importKey() { let huksInfo; let ret = true; diff --git a/LiteWearable/entry/src/main/js/MainAbility/security/RSA.js b/LiteWearable/entry/src/main/js/MainAbility/security/RSA.js index 73469f3132bcde33a2a243a376f22607370589ec..c3b77afedb9c552737cfa71d15236e8ce211acdb 100644 --- a/LiteWearable/entry/src/main/js/MainAbility/security/RSA.js +++ b/LiteWearable/entry/src/main/js/MainAbility/security/RSA.js @@ -3,18 +3,18 @@ import huks from '@ohos.security.huks'; // [Start rsa_encrypt] -// 别名,用于区分生成的KEY +// Alias, used to distinguish the generated KEY. const RSA_KEY_ALIAS = 'RSAKeyAlias'; -// 自定义密钥长度必须在1024 -2048之间,且是8的倍数 +// The custom key length must be between 1024 and 2048, and it must be a multiple of 8. const HUKS_RSA_KEY_SIZE_1024 = 1024; // [StartExclude rsa_generate] -// 明文,加密前数据 +// Plain text, data before encryption. let plainText = 'RSASSAdffssghCBC5612345612345192'; -// 明文,加密前数据的长度 +// Plain text, the length of the data before encryption. let plainTextLen = 32; -// 密文,存放加密后数据 +// Ciphertext, storing the encrypted data. let cipherText = ''; -// 操作句柄 +// Operation handle. let handle; // [EndExclude rsa_generate] @@ -23,17 +23,17 @@ let handle; function getRSAGenProperties() { let properties = new Array(); let index = 0; - // 算法 + // algorithm. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_ALGORITHM, value: huks.HuksKeyAlg.HUKS_ALG_RSA }; - // 密钥长度 + // key length. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, value: HUKS_RSA_KEY_SIZE_1024 }; - // 密钥用途 + // Key usage. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_PURPOSE, value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT | huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT @@ -59,23 +59,23 @@ function generateRSAKey() { function getRSAEncryptProperties() { let properties = new Array(); let index = 0; - // 算法 + // algorithm. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_ALGORITHM, value: huks.HuksKeyAlg.HUKS_ALG_RSA }; - // 密钥长度 + // key length. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, value: HUKS_RSA_KEY_SIZE_1024 }; - // 密钥用途 + // Key usage. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_PURPOSE, value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT }; - // 密钥 PADDING方式 + // Key PADDING method. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_PADDING, value: huks.HuksKeyPadding.HUKS_PADDING_PKCS1_V1_5 @@ -148,7 +148,7 @@ function encryptProcess() { } }); } else { - // 密文接收 + // Encrypted message reception. cipherText = uint8ArrayToString(finishData.outData); huksInfo = cipherText; } @@ -177,30 +177,30 @@ function stringToUint8Array(str) { function getRSADecryptProperties() { let properties = new Array(); let index = 0; - // 算法 + // algorithm. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_ALGORITHM, value: huks.HuksKeyAlg.HUKS_ALG_RSA }; - // 密钥长度 + // key length. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_KEY_SIZE, value: HUKS_RSA_KEY_SIZE_1024 }; - // 密钥用途 + // Key usage. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_PURPOSE, value: huks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT }; - // 密钥 PADDING方式 + // Key PADDING method. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_PADDING, value: huks.HuksKeyPadding.HUKS_PADDING_PKCS1_V1_5 }; - // 摘要算法 + // digest algorithm. properties[index++] = { tag: huks.HuksTag.HUKS_TAG_DIGEST, value: huks.HuksKeyDigest.HUKS_DIGEST_SHA256 @@ -210,7 +210,7 @@ function getRSADecryptProperties() { } function decryptProcess() { - // 加密密文长度 + // Length of encrypted ciphertext. let len = HUKS_RSA_KEY_SIZE_1024 / 8; let ret = true; let outPlainText; @@ -271,7 +271,7 @@ function decryptProcess() { } }); } else { - // 明文接收 + // Clear text reception. outPlainText = uint8ArrayToString(finishData.outData); } });