未验证 提交 69d20154 编写于 作者: O openharmony_ci 提交者: Gitee

!20879 【开发自提单】优化加解密算法库开发指南和api文档代码

Merge pull request !20879 from 胡啸天/master
...@@ -342,7 +342,7 @@ buffer数组。 ...@@ -342,7 +342,7 @@ buffer数组。
### 属性 ### 属性
**系统能力:** SystemCapability.Security.CryptoFramework **系统能力:** SystemCapability.Security.CryptoFramework
| 名称 | 类型 | 可读 | 可写 | 说明 | | 名称 | 类型 | 可读 | 可写 | 说明 |
| ------- | ------ | ---- | ---- | ---------------------------- | | ------- | ------ | ---- | ---- | ---------------------------- |
...@@ -426,7 +426,7 @@ console.info("key hex:" + uint8ArrayToShowStr(encodedKey.data)); // 输出全 ...@@ -426,7 +426,7 @@ console.info("key hex:" + uint8ArrayToShowStr(encodedKey.data)); // 输出全
### 属性 ### 属性
**系统能力:** SystemCapability.Security.CryptoFramework **系统能力:** SystemCapability.Security.CryptoFramework
| 名称 | 类型 | 可读 | 可写 | 说明 | | 名称 | 类型 | 可读 | 可写 | 说明 |
| ------- | ------ | ---- | ---- | ---------------------------- | | ------- | ------ | ---- | ---- | ---------------------------- |
...@@ -475,7 +475,7 @@ console.info("ecc item --- p: " + p.toString(16)); ...@@ -475,7 +475,7 @@ console.info("ecc item --- p: " + p.toString(16));
### 属性 ### 属性
**系统能力:** SystemCapability.Security.CryptoFramework **系统能力:** SystemCapability.Security.CryptoFramework
| 名称 | 类型 | 可读 | 可写 | 说明 | | 名称 | 类型 | 可读 | 可写 | 说明 |
| ------- | ------ | ---- | ---- | ---------------------------- | | ------- | ------ | ---- | ---- | ---------------------------- |
...@@ -494,7 +494,7 @@ clearMem(): void ...@@ -494,7 +494,7 @@ clearMem(): void
```js ```js
let key; // key为使用非对称密钥生成器生成的非对称密钥的私钥对象,此处省略生成过程 let key; // key为使用非对称密钥生成器生成的非对称密钥的私钥对象,此处省略生成过程
key.clearMem(); key.clearMem(); // 非对称密钥私钥clearMem会释放内部密钥结构体,clearMem后不支持getEncoded操作
``` ```
### getAsyKeySpec<sup>10+</sup> ### getAsyKeySpec<sup>10+</sup>
...@@ -915,8 +915,8 @@ convertKey(pubKey: DataBlob, priKey: DataBlob, callback: AsyncCallback\<KeyPair\ ...@@ -915,8 +915,8 @@ convertKey(pubKey: DataBlob, priKey: DataBlob, callback: AsyncCallback\<KeyPair\
```js ```js
import cryptoFramework from "@ohos.security.cryptoFramework" import cryptoFramework from "@ohos.security.cryptoFramework"
let pubKeyArray = new Uint8Array([48,89,48,19,6,7,42,134,72,206,61,2,1,6,8,42,134,72,206,61,3,1,7,3,66,0,4,83,96,142,9,86,214,126,106,247,233,92,125,4,128,138,105,246,162,215,71,81,58,202,121,26,105,211,55,130,45,236,143,55,16,248,75,167,160,167,106,2,152,243,44,68,66,0,167,99,92,235,215,159,239,28,106,124,171,34,145,124,174,57,92]); let pubKeyArray = new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, 83, 96, 142, 9, 86, 214, 126, 106, 247, 233, 92, 125, 4, 128, 138, 105, 246, 162, 215, 71, 81, 58, 202, 121, 26, 105, 211, 55, 130, 45, 236, 143, 55, 16, 248, 75, 167, 160, 167, 106, 2, 152, 243, 44, 68, 66, 0, 167, 99, 92, 235, 215, 159, 239, 28, 106, 124, 171, 34, 145, 124, 174, 57, 92]);
let priKeyArray = new Uint8Array([48,49,2,1,1,4,32,115,56,137,35,207,0,60,191,90,61,136,105,210,16,27,4,171,57,10,61,123,40,189,28,34,207,236,22,45,223,10,189,160,10,6,8,42,134,72,206,61,3,1,7]); let priKeyArray = new Uint8Array([48, 49, 2, 1, 1, 4, 32, 115, 56, 137, 35, 207, 0, 60, 191, 90, 61, 136, 105, 210, 16, 27, 4, 171, 57, 10, 61, 123, 40, 189, 28, 34, 207, 236, 22, 45, 223, 10, 189, 160, 10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7]);
let pubKeyBlob = { data: pubKeyArray }; // 公钥的密钥数据 let pubKeyBlob = { data: pubKeyArray }; // 公钥的密钥数据
let priKeyBlob = { data: priKeyArray }; // 私钥的密钥数据 let priKeyBlob = { data: priKeyArray }; // 私钥的密钥数据
let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator("ECC256"); let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator("ECC256");
...@@ -963,8 +963,8 @@ convertKey(pubKey: DataBlob, priKey: DataBlob): Promise\<KeyPair> ...@@ -963,8 +963,8 @@ convertKey(pubKey: DataBlob, priKey: DataBlob): Promise\<KeyPair>
```js ```js
import cryptoFramework from "@ohos.security.cryptoFramework" import cryptoFramework from "@ohos.security.cryptoFramework"
let pubKeyArray = new Uint8Array([48,89,48,19,6,7,42,134,72,206,61,2,1,6,8,42,134,72,206,61,3,1,7,3,66,0,4,83,96,142,9,86,214,126,106,247,233,92,125,4,128,138,105,246,162,215,71,81,58,202,121,26,105,211,55,130,45,236,143,55,16,248,75,167,160,167,106,2,152,243,44,68,66,0,167,99,92,235,215,159,239,28,106,124,171,34,145,124,174,57,92]); let pubKeyArray = new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, 83, 96, 142, 9, 86, 214, 126, 106, 247, 233, 92, 125, 4, 128, 138, 105, 246, 162, 215, 71, 81, 58, 202, 121, 26, 105, 211, 55, 130, 45, 236, 143, 55, 16, 248, 75, 167, 160, 167, 106, 2, 152, 243, 44, 68, 66, 0, 167, 99, 92, 235, 215, 159, 239, 28, 106, 124, 171, 34, 145, 124, 174, 57, 92]);
let priKeyArray = new Uint8Array([48,49,2,1,1,4,32,115,56,137,35,207,0,60,191,90,61,136,105,210,16,27,4,171,57,10,61,123,40,189,28,34,207,236,22,45,223,10,189,160,10,6,8,42,134,72,206,61,3,1,7]); let priKeyArray = new Uint8Array([48, 49, 2, 1, 1, 4, 32, 115, 56, 137, 35, 207, 0, 60, 191, 90, 61, 136, 105, 210, 16, 27, 4, 171, 57, 10, 61, 123, 40, 189, 28, 34, 207, 236, 22, 45, 223, 10, 189, 160, 10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7]);
let pubKeyBlob = { data: pubKeyArray }; // 公钥的密钥数据 let pubKeyBlob = { data: pubKeyArray }; // 公钥的密钥数据
let priKeyBlob = { data: priKeyArray }; // 私钥的密钥数据 let priKeyBlob = { data: priKeyArray }; // 私钥的密钥数据
let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator("ECC256"); let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator("ECC256");
...@@ -1572,7 +1572,7 @@ doFinal(data: DataBlob, callback: AsyncCallback\<DataBlob>): void ...@@ -1572,7 +1572,7 @@ doFinal(data: DataBlob, callback: AsyncCallback\<DataBlob>): void
(2)在RSA、SM2非对称加解密中,doFinal加/解密本次传入的数据,通过注册回调函数获取加密或者解密数据。如果数据量较大,可以多次调用doFinal,拼接结果得到完整的明文/密文。 (2)在RSA、SM2非对称加解密中,doFinal加/解密本次传入的数据,通过注册回调函数获取加密或者解密数据。如果数据量较大,可以多次调用doFinal,拼接结果得到完整的明文/密文。
> **说明:** > **说明:**
> >
> 1. 对称加解密中,调用doFinal标志着一次加解密流程已经完成,即[Cipher](#cipher)实例的状态被清除,因此当后续开启新一轮加解密流程时,需要重新调用[init()](init-2)并传入完整的参数列表进行初始化<br/>(比如即使是对同一个Cipher实例,采用同样的对称密钥,进行加密然后解密,则解密中调用init的时候仍需填写params参数,而不能直接省略为null)。 > 1. 对称加解密中,调用doFinal标志着一次加解密流程已经完成,即[Cipher](#cipher)实例的状态被清除,因此当后续开启新一轮加解密流程时,需要重新调用[init()](init-2)并传入完整的参数列表进行初始化<br/>(比如即使是对同一个Cipher实例,采用同样的对称密钥,进行加密然后解密,则解密中调用init的时候仍需填写params参数,而不能直接省略为null)。
> 2. 如果遇到解密失败,需检查加解密数据和[init](#init-2)时的参数是否匹配,包括GCM模式下加密得到的authTag是否填入解密时的GcmParamsSpec等。 > 2. 如果遇到解密失败,需检查加解密数据和[init](#init-2)时的参数是否匹配,包括GCM模式下加密得到的authTag是否填入解密时的GcmParamsSpec等。
...@@ -1843,11 +1843,11 @@ import cryptoFramework from "@ohos.security.cryptoFramework" ...@@ -1843,11 +1843,11 @@ import cryptoFramework from "@ohos.security.cryptoFramework"
let signer1 = cryptoFramework.createSign("RSA1024|PKCS1|SHA256"); let signer1 = cryptoFramework.createSign("RSA1024|PKCS1|SHA256");
let singer2 = cryptoFramework.createSign("RSA1024|PSS|SHA256|MGF1_SHA256"); let signer2 = cryptoFramework.createSign("RSA1024|PSS|SHA256|MGF1_SHA256");
let singer3 = cryptoFramework.createSign("ECC224|SHA256"); let signer3 = cryptoFramework.createSign("ECC224|SHA256");
let singer4 = cryptoFramework.createSign("DSA2048|SHA256"); let signer4 = cryptoFramework.createSign("DSA2048|SHA256");
``` ```
## Sign ## Sign
...@@ -2070,8 +2070,8 @@ function signMessageCallback() { ...@@ -2070,8 +2070,8 @@ function signMessageCallback() {
rsaGenerator.generateKeyPair(function (err, keyPair) { rsaGenerator.generateKeyPair(function (err, keyPair) {
globalKeyPair = keyPair; globalKeyPair = keyPair;
let priKey = globalKeyPair.priKey; let priKey = globalKeyPair.priKey;
signer.init(priKey, function (err, data) { signer.init(priKey, err => {
signer.update(input1, function (err, data) { // add first segment of data signer.update(input1, err => { // add first segment of data
signer.sign(input2, function (err, data) { // add second segment of data, sign input1 and input2 signer.sign(input2, function (err, data) { // add second segment of data, sign input1 and input2
SignMessageBlob = data; SignMessageBlob = data;
AlertDialog.show({message: "res" + SignMessageBlob.data}); AlertDialog.show({message: "res" + SignMessageBlob.data});
...@@ -2713,7 +2713,7 @@ try { ...@@ -2713,7 +2713,7 @@ try {
// 参数选择请参考上述算法支持范围 // 参数选择请参考上述算法支持范围
md = cryptoFramework.createMd("SHA256"); md = cryptoFramework.createMd("SHA256");
} catch (error) { } catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
``` ```
...@@ -2764,7 +2764,7 @@ var md; ...@@ -2764,7 +2764,7 @@ var md;
try { try {
md = cryptoFramework.createMd("SHA256"); md = cryptoFramework.createMd("SHA256");
} catch (error) { } catch (error) {
console.error("[Callback]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
console.error("Md algName is: " + md.algName); console.error("Md algName is: " + md.algName);
...@@ -2814,7 +2814,7 @@ var md; ...@@ -2814,7 +2814,7 @@ var md;
try { try {
md = cryptoFramework.createMd("SHA256"); md = cryptoFramework.createMd("SHA256");
} catch (error) { } catch (error) {
console.error("[Callback]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
console.error("Md algName is: " + md.algName); console.error("Md algName is: " + md.algName);
...@@ -2855,7 +2855,7 @@ var md; ...@@ -2855,7 +2855,7 @@ var md;
try { try {
md = cryptoFramework.createMd("SHA256"); md = cryptoFramework.createMd("SHA256");
} catch (error) { } catch (error) {
console.error("[Callback]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
console.error("Md algName is: " + md.algName); console.error("Md algName is: " + md.algName);
...@@ -2904,7 +2904,7 @@ var md; ...@@ -2904,7 +2904,7 @@ var md;
try { try {
md = cryptoFramework.createMd("SHA256"); md = cryptoFramework.createMd("SHA256");
} catch (error) { } catch (error) {
console.error("[Callback]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
console.error("Md algName is: " + md.algName); console.error("Md algName is: " + md.algName);
...@@ -2949,7 +2949,7 @@ var md; ...@@ -2949,7 +2949,7 @@ var md;
try { try {
md = cryptoFramework.createMd("SHA256"); md = cryptoFramework.createMd("SHA256");
} catch (error) { } catch (error) {
console.error("[Callback]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
console.error("Md algName is: " + md.algName); console.error("Md algName is: " + md.algName);
...@@ -3004,7 +3004,7 @@ try { ...@@ -3004,7 +3004,7 @@ try {
// 参数选择请参考上述算法支持范围 // 参数选择请参考上述算法支持范围
mac = cryptoFramework.createMac("SHA256"); mac = cryptoFramework.createMac("SHA256");
} catch (error) { } catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
``` ```
...@@ -3051,7 +3051,7 @@ var mac; ...@@ -3051,7 +3051,7 @@ var mac;
try { try {
mac = cryptoFramework.createMac("SHA256"); mac = cryptoFramework.createMac("SHA256");
} catch (error) { } catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
var KeyBlob; var KeyBlob;
var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128"); var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128");
...@@ -3103,7 +3103,7 @@ var mac; ...@@ -3103,7 +3103,7 @@ var mac;
try { try {
mac = cryptoFramework.createMac("SHA256"); mac = cryptoFramework.createMac("SHA256");
} catch (error) { } catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
console.error("Mac algName is: " + mac.algName); console.error("Mac algName is: " + mac.algName);
...@@ -3155,7 +3155,7 @@ var mac; ...@@ -3155,7 +3155,7 @@ var mac;
try { try {
mac = cryptoFramework.createMac("SHA256"); mac = cryptoFramework.createMac("SHA256");
} catch (error) { } catch (error) {
console.error("[Callback]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128"); var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128");
symKeyGenerator.convertKey(KeyBlob, (err, symKey) => { symKeyGenerator.convertKey(KeyBlob, (err, symKey) => {
...@@ -3216,7 +3216,7 @@ var mac; ...@@ -3216,7 +3216,7 @@ var mac;
try { try {
mac = cryptoFramework.createMac("SHA256"); mac = cryptoFramework.createMac("SHA256");
} catch (error) { } catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
console.error("Mac algName is: " + mac.algName); console.error("Mac algName is: " + mac.algName);
...@@ -3267,7 +3267,7 @@ var mac; ...@@ -3267,7 +3267,7 @@ var mac;
try { try {
mac = cryptoFramework.createMac("SHA256"); mac = cryptoFramework.createMac("SHA256");
} catch (error) { } catch (error) {
console.error("[Callback]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128"); var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128");
symKeyGenerator.convertKey(KeyBlob, (err, symKey) => { symKeyGenerator.convertKey(KeyBlob, (err, symKey) => {
...@@ -3325,7 +3325,7 @@ var mac; ...@@ -3325,7 +3325,7 @@ var mac;
try { try {
mac = cryptoFramework.createMac("SHA256"); mac = cryptoFramework.createMac("SHA256");
} catch (error) { } catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
console.error("Mac algName is: " + mac.algName); console.error("Mac algName is: " + mac.algName);
...@@ -3378,7 +3378,7 @@ var mac; ...@@ -3378,7 +3378,7 @@ var mac;
try { try {
mac = cryptoFramework.createMac("SHA256"); mac = cryptoFramework.createMac("SHA256");
} catch (error) { } catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
console.error("Mac algName is: " + mac.algName); console.error("Mac algName is: " + mac.algName);
...@@ -3432,7 +3432,7 @@ import cryptoFramework from "@ohos.security.cryptoFramework" ...@@ -3432,7 +3432,7 @@ import cryptoFramework from "@ohos.security.cryptoFramework"
try { try {
var rand = cryptoFramework.createRandom(); var rand = cryptoFramework.createRandom();
} catch (error) { } catch (error) {
console.error("[Callback]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
``` ```
...@@ -3480,7 +3480,7 @@ var rand; ...@@ -3480,7 +3480,7 @@ var rand;
try { try {
rand = cryptoFramework.createRandom(); rand = cryptoFramework.createRandom();
} catch (error) { } catch (error) {
console.error("[Callback]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
rand.generateRandom(12, (err, randData) => { rand.generateRandom(12, (err, randData) => {
if (err) { if (err) {
...@@ -3528,7 +3528,7 @@ var rand; ...@@ -3528,7 +3528,7 @@ var rand;
try { try {
rand = cryptoFramework.createRandom(); rand = cryptoFramework.createRandom();
} catch (error) { } catch (error) {
console.error("[Callback]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
var promiseGenerateRand = rand.generateRandom(12); var promiseGenerateRand = rand.generateRandom(12);
...@@ -3576,7 +3576,7 @@ var rand; ...@@ -3576,7 +3576,7 @@ var rand;
try { try {
rand = cryptoFramework.createRandom(); rand = cryptoFramework.createRandom();
} catch (error) { } catch (error) {
console.error("[Callback]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
try { try {
...@@ -3618,18 +3618,18 @@ var rand; ...@@ -3618,18 +3618,18 @@ var rand;
try { try {
rand = cryptoFramework.createRandom(); rand = cryptoFramework.createRandom();
} catch (error) { } catch (error) {
console.error("[Callback]: error code: " + error.code + ", message is: " + error.message); console.error("[Sync]: error code: " + error.code + ", message is: " + error.message);
} }
rand.generateRandom(12, (err, randData) => { rand.generateRandom(12, (err, randData) => {
if (err) { if (err) {
console.error("[Callback] err: " + err.code); console.error("[Callback] err: " + err.code);
} else { } else {
console.error("[Callback]: generate random result: " + randData.data); console.info("[Callback]: generate random result: " + randData.data);
try { try {
rand.setSeed(randData); rand.setSeed(randData);
} catch (error) { } catch (error) {
console.log("setSeed failed, errCode: " + error.code + ", errMsg: " + error.message); console.error("setSeed failed, errCode: " + error.code + ", errMsg: " + error.message);
} }
} }
}); });
......
...@@ -56,17 +56,17 @@ import cryptoFramework from '@ohos.security.cryptoFramework'; ...@@ -56,17 +56,17 @@ import cryptoFramework from '@ohos.security.cryptoFramework';
function generateAsyKey() { function generateAsyKey() {
// 创建非对称密钥生成器 // 创建非对称密钥生成器
let rsaGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_2"); let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024|PRIMES_2');
// 通过非对称密钥生成器,随机生成非对称密钥 // 通过非对称密钥生成器,随机生成非对称密钥
let keyGenPromise = rsaGenerator.generateKeyPair(); let keyGenPromise = rsaGenerator.generateKeyPair();
keyGenPromise.then( keyPair => { keyGenPromise.then(keyPair => {
let pubKey = keyPair.pubKey; let pubKey = keyPair.pubKey;
let priKey = keyPair.priKey; let priKey = keyPair.priKey;
// 获取非对称密钥的二进制数据 // 获取非对称密钥的二进制数据
let pkBlob = pubKey.getEncoded(); let pkBlob = pubKey.getEncoded();
let skBlob = priKey.getEncoded(); let skBlob = priKey.getEncoded();
AlertDialog.show({ message : "pk bin data" + pkBlob.data} ); AlertDialog.show({ message: 'pk bin data' + pkBlob.data });
AlertDialog.show({ message : "sk bin data" + skBlob.data} ); AlertDialog.show({ message: 'sk bin data' + skBlob.data });
}) })
} }
``` ```
...@@ -96,8 +96,8 @@ function testGenerateAesKey() { ...@@ -96,8 +96,8 @@ function testGenerateAesKey() {
let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES256'); let symKeyGenerator = cryptoFramework.createSymKeyGenerator('AES256');
// 通过密钥生成器随机生成对称密钥 // 通过密钥生成器随机生成对称密钥
let promiseSymKey = symKeyGenerator.generateSymKey(); let promiseSymKey = symKeyGenerator.generateSymKey();
promiseSymKey.then( key => { promiseSymKey.then(key => {
// 获取对称密钥的二进制数据,输出长度为256bit的字节流 // 获取对称密钥的二进制数据,输出长度为256bit,以16进制表示,长度为64,即32字节
let encodedKey = key.getEncoded(); let encodedKey = key.getEncoded();
console.info('key hex:' + uint8ArrayToShowStr(encodedKey.data)); console.info('key hex:' + uint8ArrayToShowStr(encodedKey.data));
}) })
...@@ -115,14 +115,15 @@ function testGenerateAesKey() { ...@@ -115,14 +115,15 @@ function testGenerateAesKey() {
import cryptoFramework from '@ohos.security.cryptoFramework'; import cryptoFramework from '@ohos.security.cryptoFramework';
function convertAsyKey() { function convertAsyKey() {
let rsaGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024"); let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA1024');
let pkval = new Uint8Array([48,129,159,48,13,6,9,42,134,72,134,247,13,1,1,1,5,0,3,129,141,0,48,129,137,2,129,129,0,174,203,113,83,113,3,143,213,194,79,91,9,51,142,87,45,97,65,136,24,166,35,5,179,42,47,212,79,111,74,134,120,73,67,21,19,235,80,46,152,209,133,232,87,192,140,18,206,27,106,106,169,106,46,135,111,118,32,129,27,89,255,183,116,247,38,12,7,238,77,151,167,6,102,153,126,66,28,253,253,216,64,20,138,117,72,15,216,178,37,208,179,63,204,39,94,244,170,48,190,21,11,73,169,156,104,193,3,17,100,28,60,50,92,235,218,57,73,119,19,101,164,192,161,197,106,105,73,2,3,1,0,1]); let pkVal = new Uint8Array([48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, 2, 129, 129, 0, 174, 203, 113, 83, 113, 3, 143, 213, 194, 79, 91, 9, 51, 142, 87, 45, 97, 65, 136, 24, 166, 35, 5, 179, 42, 47, 212, 79, 111, 74, 134, 120, 73, 67, 21, 19, 235, 80, 46, 152, 209, 133, 232, 87, 192, 140, 18, 206, 27, 106, 106, 169, 106, 46, 135, 111, 118, 32, 129, 27, 89, 255, 183, 116, 247, 38, 12, 7, 238, 77, 151, 167, 6, 102, 153, 126, 66, 28, 253, 253, 216, 64, 20, 138, 117, 72, 15, 216, 178, 37, 208, 179, 63, 204, 39, 94, 244, 170, 48, 190, 21, 11, 73, 169, 156, 104, 193, 3, 17, 100, 28, 60, 50, 92, 235, 218, 57, 73, 119, 19, 101, 164, 192, 161, 197, 106, 105, 73, 2, 3, 1, 0, 1]);
let pkBlob = {data : pkval}; let pkBlob = { data: pkVal };
rsaGenerator.convertKey(pkBlob, null, function(err, keyPair) { rsaGenerator.convertKey(pkBlob, null, (err, keyPair) => {
if (keyPair == null) { if (err) {
AlertDialog.show({message : "Convert keypair fail"}); AlertDialog.show({ message: 'Convert keyPair fail' });
return;
} }
AlertDialog.show({message : "Convert KeyPair success"}); AlertDialog.show({ message: 'Convert keyPair success' });
}) })
} }
``` ```
...@@ -142,17 +143,18 @@ function convertAsyKey() { ...@@ -142,17 +143,18 @@ function convertAsyKey() {
import cryptoFramework from '@ohos.security.cryptoFramework'; import cryptoFramework from '@ohos.security.cryptoFramework';
function convertEccAsyKey() { function convertEccAsyKey() {
let pubKeyArray = new Uint8Array([48,89,48,19,6,7,42,134,72,206,61,2,1,6,8,42,134,72,206,61,3,1,7,3,66,0,4,83,96,142,9,86,214,126,106,247,233,92,125,4,128,138,105,246,162,215,71,81,58,202,121,26,105,211,55,130,45,236,143,55,16,248,75,167,160,167,106,2,152,243,44,68,66,0,167,99,92,235,215,159,239,28,106,124,171,34,145,124,174,57,92]); let pubKeyArray = new Uint8Array([48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7, 3, 66, 0, 4, 83, 96, 142, 9, 86, 214, 126, 106, 247, 233, 92, 125, 4, 128, 138, 105, 246, 162, 215, 71, 81, 58, 202, 121, 26, 105, 211, 55, 130, 45, 236, 143, 55, 16, 248, 75, 167, 160, 167, 106, 2, 152, 243, 44, 68, 66, 0, 167, 99, 92, 235, 215, 159, 239, 28, 106, 124, 171, 34, 145, 124, 174, 57, 92]);
let priKeyArray = new Uint8Array([48,49,2,1,1,4,32,115,56,137,35,207,0,60,191,90,61,136,105,210,16,27,4,171,57,10,61,123,40,189,28,34,207,236,22,45,223,10,189,160,10,6,8,42,134,72,206,61,3,1,7]); let priKeyArray = new Uint8Array([48, 49, 2, 1, 1, 4, 32, 115, 56, 137, 35, 207, 0, 60, 191, 90, 61, 136, 105, 210, 16, 27, 4, 171, 57, 10, 61, 123, 40, 189, 28, 34, 207, 236, 22, 45, 223, 10, 189, 160, 10, 6, 8, 42, 134, 72, 206, 61, 3, 1, 7]);
let pubKeyBlob = { data: pubKeyArray }; let pubKeyBlob = { data: pubKeyArray };
let priKeyBlob = { data: priKeyArray }; let priKeyBlob = { data: priKeyArray };
let generator = cryptoFramework.createAsyKeyGenerator("ECC256"); let generator = cryptoFramework.createAsyKeyGenerator('ECC256');
generator.convertKey(pubKeyBlob, priKeyBlob, (error, data) => { generator.convertKey(pubKeyBlob, priKeyBlob, (error, data) => {
if (error) { if (error) {
AlertDialog.show({message : "Convert keypair fail"}); AlertDialog.show({ message: 'Convert keyPair fail' });
} return;
AlertDialog.show({message : "Convert KeyPair success"}); }
}) AlertDialog.show({ message: 'Convert keyPair success' });
})
} }
``` ```
...@@ -180,28 +182,28 @@ function genKeyMaterialBlob() { ...@@ -180,28 +182,28 @@ function genKeyMaterialBlob() {
let arr = [ let arr = [
0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, 0xba, 0x3d, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56,
0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c, 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c,
0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30, 0x72]; // keyLen = 192 (24 bytes) 0xba, 0x3b, 0xc2, 0x71, 0xab, 0xa0, 0x30, 0x72]; // keyLen = 192 (24 bytes)
let keyMaterial = new Uint8Array(arr); let keyMaterial = new Uint8Array(arr);
return {data : keyMaterial}; return { data: keyMaterial };
} }
function testConvertAesKey() { function testConvertSymKey() {
// 生成对称密钥生成器 // 生成对称密钥生成器
let symKeyGenerator = cryptoFramework.createSymKeyGenerator('3DES192'); let symKeyGenerator = cryptoFramework.createSymKeyGenerator('3DES192');
// 根据用户指定的数据,生成对称密钥 // 根据用户指定的数据,生成对称密钥
let keyMaterialBlob = genKeyMaterialBlob(); let keyMaterialBlob = genKeyMaterialBlob();
try { try {
symKeyGenerator.convertKey(keyMaterialBlob, (error, key) => { symKeyGenerator.convertKey(keyMaterialBlob, (error, key) => {
if (error) { // 业务逻辑执行错误通过callback的第一个参数返回错误信息 if (error) { // 业务逻辑执行错误通过callback的第一个参数返回错误信息,即抛出异步异常
console.error(`convertKey error, ${error.code}, ${error.message}`); console.error(`convertKey error, ${error.code}, ${error.message}`);
return; return;
} }
console.info(`key algName: ${key.algName}`); console.info(`key algName: ${key.algName}`);
console.info(`key format: ${key.format}`); console.info(`key format: ${key.format}`);
let encodedKey = key.getEncoded(); // 获取对称密钥的二进制数据,输出长度为192bit的字节流 let encodedKey = key.getEncoded(); // 获取对称密钥的二进制数据,输出长度为192bit的字节流,以16进制表示,长度为48,即24字节
console.info('key getEncoded hex: ' + uint8ArrayToShowStr(encodedKey.data)); console.info('key getEncoded hex: ' + uint8ArrayToShowStr(encodedKey.data));
}) })
} catch (error) { // 参数检查的错误以同步的方式立即抛出异常 } catch (error) { // 参数检查的错误以同步的方式立即抛出异常
console.error(`convertKey failed, ${error.code}, ${error.message}`); console.error(`convertKey failed, ${error.code}, ${error.message}`);
return; return;
} }
...@@ -230,14 +232,14 @@ function generateAsyKey() { ...@@ -230,14 +232,14 @@ function generateAsyKey() {
let rsaGenerator = cryptoFramework.createAsyKeyGenerator("SM2_256"); let rsaGenerator = cryptoFramework.createAsyKeyGenerator("SM2_256");
// 通过非对称密钥生成器,随机生成非对称密钥 // 通过非对称密钥生成器,随机生成非对称密钥
let keyGenPromise = rsaGenerator.generateKeyPair(); let keyGenPromise = rsaGenerator.generateKeyPair();
keyGenPromise.then( keyPair => { keyGenPromise.then(keyPair => {
let pubKey = keyPair.pubKey; let pubKey = keyPair.pubKey;
let priKey = keyPair.priKey; let priKey = keyPair.priKey;
// 获取非对称密钥的二进制数据 // 获取非对称密钥的二进制数据
let pkBlob = pubKey.getEncoded(); let pkBlob = pubKey.getEncoded();
let skBlob = priKey.getEncoded(); let skBlob = priKey.getEncoded();
AlertDialog.show({ message : "pk bin data" + pkBlob.data} ); AlertDialog.show({ message: "pk bin data" + pkBlob.data });
AlertDialog.show({ message : "sk bin data" + skBlob.data} ); AlertDialog.show({ message: "sk bin data" + skBlob.data });
}) })
} }
``` ```
...@@ -266,13 +268,13 @@ function uint8ArrayToShowStr(uint8Array) { ...@@ -266,13 +268,13 @@ function uint8ArrayToShowStr(uint8Array) {
.join(''); .join('');
} }
function testGenerateAesKey() { function testGenerateSM4Key() {
// 创建对称密钥生成器 // 创建对称密钥生成器
let symKeyGenerator = cryptoFramework.createSymKeyGenerator("SM4_128"); let symKeyGenerator = cryptoFramework.createSymKeyGenerator("SM4_128");
// 通过密钥生成器随机生成对称密钥 // 通过密钥生成器随机生成对称密钥
let promiseSymKey = symKeyGenerator.generateSymKey(); let promiseSymKey = symKeyGenerator.generateSymKey();
promiseSymKey.then( key => { promiseSymKey.then(key => {
// 获取对称密钥的二进制数据,输出长度为256bit的字节流 // 获取对称密钥的二进制数据,输出长度为128bit的字节流,以16进制表示,长度为32,即16字节
let encodedKey = key.getEncoded(); let encodedKey = key.getEncoded();
console.info('key hex:' + uint8ArrayToShowStr(encodedKey.data)); console.info('key hex:' + uint8ArrayToShowStr(encodedKey.data));
}) })
...@@ -302,6 +304,7 @@ function convertSM2AsyKey() { ...@@ -302,6 +304,7 @@ function convertSM2AsyKey() {
generator.convertKey(pubKeyBlob, priKeyBlob, (error, data) => { generator.convertKey(pubKeyBlob, priKeyBlob, (error, data) => {
if (error) { if (error) {
AlertDialog.show({message : "Convert keypair fail"}); AlertDialog.show({message : "Convert keypair fail"});
return;
} }
AlertDialog.show({message : "Convert KeyPair success"}); AlertDialog.show({message : "Convert KeyPair success"});
}) })
...@@ -362,24 +365,24 @@ function showBigIntInfo(bnName, bnValue) { ...@@ -362,24 +365,24 @@ function showBigIntInfo(bnName, bnValue) {
// 根据密钥参数属性,构造ECC公私钥共有参数的sepc结构体 // 根据密钥参数属性,构造ECC公私钥共有参数的sepc结构体
function genEccCommonSpec() { function genEccCommonSpec() {
let fieldFp = { let fieldFp = {
fieldType : "Fp", fieldType: "Fp",
p : BigInt("0xffffffffffffffffffffffffffffffff000000000000000000000001") p: BigInt("0xffffffffffffffffffffffffffffffff000000000000000000000001")
} }
let G = { let G = {
x : BigInt("0xb70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21"), x: BigInt("0xb70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21"),
y : BigInt("0xbd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34") y: BigInt("0xbd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34")
} }
let eccCommonSpec = { let eccCommonSpec = {
algName : "ECC", algName: "ECC",
specType : cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC, specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC,
field : fieldFp, field: fieldFp,
a : BigInt("0xfffffffffffffffffffffffffffffffefffffffffffffffffffffffe"), a: BigInt("0xfffffffffffffffffffffffffffffffefffffffffffffffffffffffe"),
b : BigInt("0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4"), b: BigInt("0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4"),
g : G, g: G,
n : BigInt("0xffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d"), n: BigInt("0xffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d"),
h : 1 h: 1
} }
return eccCommonSpec; return eccCommonSpec;
} }
...@@ -389,7 +392,7 @@ function showEccSpecDetailInfo(key, keyType) { ...@@ -389,7 +392,7 @@ function showEccSpecDetailInfo(key, keyType) {
console.info("show detail of " + keyType + ":"); console.info("show detail of " + keyType + ":");
try { try {
let p = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FP_P_BN); let p = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FP_P_BN);
showBigIntInfo("--- p", p); //length is 224, hex : ffffffffffffffffffffffffffffffff000000000000000000000001 showBigIntInfo("--- p", p); // length is 224, hex : ffffffffffffffffffffffffffffffff000000000000000000000001
let a = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_A_BN); let a = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_A_BN);
showBigIntInfo("--- a", a); // length is 224, hex : fffffffffffffffffffffffffffffffefffffffffffffffffffffffe showBigIntInfo("--- a", a); // length is 224, hex : fffffffffffffffffffffffffffffffefffffffffffffffffffffffe
...@@ -407,7 +410,7 @@ function showEccSpecDetailInfo(key, keyType) { ...@@ -407,7 +410,7 @@ function showEccSpecDetailInfo(key, keyType) {
showBigIntInfo("--- n", n); // length is 224, hex : ffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d showBigIntInfo("--- n", n); // length is 224, hex : ffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d
let h = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_H_NUM); let h = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_H_NUM);
console.warn("--- h: " + h); //key h: 1 console.warn("--- h: " + h); // key h: 1
let fieldType = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FIELD_TYPE_STR); let fieldType = key.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.ECC_FIELD_TYPE_STR);
console.warn("--- field type: " + fieldType); // key field type: Fp console.warn("--- field type: " + fieldType); // key field type: Fp
...@@ -434,20 +437,21 @@ function showEccSpecDetailInfo(key, keyType) { ...@@ -434,20 +437,21 @@ function showEccSpecDetailInfo(key, keyType) {
} }
// 测试根据ECC公私钥公共密钥参数生成ECC密钥对,并获得其密钥参数属性 // 测试根据ECC公私钥公共密钥参数生成ECC密钥对,并获得其密钥参数属性
function testEccUseCommKeySpecGet() function testEccUseCommKeySpecGet() {
{
try { try {
let commKeySpec = genEccCommonSpec(); // 使用参数属性,构造ECC公私钥公共密钥参数对象 let commKeySpec = genEccCommonSpec(); // 使用参数属性,构造ECC公私钥公共密钥参数对象
let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(commKeySpec); // 使用密钥参数对象创建生成器 let generatorBySpec = cryptoFramework.createAsyKeyGeneratorBySpec(commKeySpec); // 使用密钥参数对象创建生成器
let keyPairPromise = generatorBySpec.generateKeyPair(); // 使用生成器创建ECC密钥对 let keyPairPromise = generatorBySpec.generateKeyPair(); // 使用生成器创建ECC密钥对
keyPairPromise.then( keyPair => { keyPairPromise.then(keyPair => {
showEccSpecDetailInfo(keyPair.priKey, "priKey"); // 对私钥获取相关密钥参数属性 showEccSpecDetailInfo(keyPair.priKey, "priKey"); // 对私钥获取相关密钥参数属性
showEccSpecDetailInfo(keyPair.pubKey, "pubKey"); // 对公钥获取相关密钥参数属性 showEccSpecDetailInfo(keyPair.pubKey, "pubKey"); // 对公钥获取相关密钥参数属性
}).catch(error => { }).catch(error => {
// 逻辑错误等异步异常在此捕获
console.error("generateComm error"); console.error("generateComm error");
console.error("error code: " + error.code + ", message is: " + error.message); console.error("error code: " + error.code + ", message is: " + error.message);
}) })
} catch(error) { } catch (error) {
// 参数错误等同步异常在此捕获
console.error("testEccUseCommSpec error"); console.error("testEccUseCommSpec error");
console.error("error code: " + error.code + ", message is: " + error.message); console.error("error code: " + error.code + ", message is: " + error.message);
} }
...@@ -464,12 +468,15 @@ function testEccUseCommKeySpecGet() ...@@ -464,12 +468,15 @@ function testEccUseCommKeySpecGet()
以使用Callback方式根据密钥参数生成RSA公钥为例: 以使用Callback方式根据密钥参数生成RSA公钥为例:
```js ```js
import cryptoFramework from '@ohos.security.cryptoFramework';
// RSA公钥密钥参数生成函数 // RSA公钥密钥参数生成函数
function genRsaPubKeySpec(nIn : bigint, eIn : bigint) { function genRsaPubKeySpec(nIn: bigint, eIn: bigint) {
let rsaCommSpec = { n : nIn, algName : "RSA", specType : cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC }; let rsaCommSpec = { n: nIn, algName: "RSA", specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC };
let rsaPubKeySpec = { params: rsaCommSpec, pk : eIn, algName : "RSA", specType : cryptoFramework.AsyKeySpecType.PUBLIC_KEY_SPEC }; let rsaPubKeySpec = {
params: rsaCommSpec,
pk: eIn,
algName: "RSA",
specType: cryptoFramework.AsyKeySpecType.PUBLIC_KEY_SPEC
};
return rsaPubKeySpec; return rsaPubKeySpec;
} }
...@@ -500,7 +507,7 @@ function rsaUsePubKeySpecGetCallback() { ...@@ -500,7 +507,7 @@ function rsaUsePubKeySpecGetCallback() {
let nBN = pubKey.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.RSA_N_BN); let nBN = pubKey.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.RSA_N_BN);
let eBN = pubKey.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.RSA_PK_BN); let eBN = pubKey.getAsyKeySpec(cryptoFramework.AsyKeySpecItem.RSA_PK_BN);
if (compareRsaPubKeyBySpec(rsaPubKeySpec, nBN, eBN) != true) { if (compareRsaPubKeyBySpec(rsaPubKeySpec, nBN, eBN) != true) {
AlertDialog.show({ message : "error pub key big number"} ); AlertDialog.show({ message: "error pub key big number" });
} else { } else {
console.info("n, e in the pubKey are same as the spec."); console.info("n, e in the pubKey are same as the spec.");
} }
...@@ -561,19 +568,19 @@ var globalKey; ...@@ -561,19 +568,19 @@ var globalKey;
var globalCipherText; var globalCipherText;
function genGcmParamsSpec() { function genGcmParamsSpec() {
let arr = [0, 0, 0, 0 , 0, 0, 0, 0, 0, 0 , 0, 0]; // 12 bytes let arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 12 bytes
let dataIv = new Uint8Array(arr); let dataIv = new Uint8Array(arr);
let ivBlob = {data : dataIv}; let ivBlob = { data: dataIv };
arr = [0, 0, 0, 0 , 0, 0, 0, 0]; // 8 bytes arr = [0, 0, 0, 0, 0, 0, 0, 0]; // 8 bytes
let dataAad = new Uint8Array(arr); let dataAad = new Uint8Array(arr);
let aadBlob = {data : dataAad}; let aadBlob = { data: dataAad };
arr = [0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0]; // 16 bytes arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; // 16 bytes
let dataTag = new Uint8Array(arr); let dataTag = new Uint8Array(arr);
let tagBlob = {data : dataTag}; // GCM的authTag在加密时从doFinal结果中获取,在解密时填入init函数的params参数中 let tagBlob = { data: dataTag }; // GCM的authTag在加密时从doFinal结果中获取,在解密时填入init函数的params参数中
let gcmParamsSpec = {iv : ivBlob, aad : aadBlob, authTag : tagBlob, algName : "GcmParamsSpec"}; let gcmParamsSpec = { iv: ivBlob, aad: aadBlob, authTag: tagBlob, algName: "GcmParamsSpec" };
return gcmParamsSpec; return gcmParamsSpec;
} }
...@@ -633,45 +640,45 @@ function testAesGcm() { ...@@ -633,45 +640,45 @@ function testAesGcm() {
} }
return promiseSymKey; return promiseSymKey;
}).then(key => { }).then(key => {
let encodedKey = key.getEncoded(); let encodedKey = key.getEncoded();
console.info('key hex:' + uint8ArrayToShowStr(encodedKey.data)); console.info('key hex:' + uint8ArrayToShowStr(encodedKey.data));
globalKey = key; globalKey = key;
return key; return key;
}).then(key => { }).then(key => {
// 初始化加解密操作环境:开始加密 // 初始化加解密操作环境:开始加密
let mode = cryptoFramework.CryptoMode.ENCRYPT_MODE; let mode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
let promiseInit = globalCipher.init(mode, key, globalGcmParams); // init let promiseInit = globalCipher.init(mode, key, globalGcmParams); // init
return promiseInit; return promiseInit;
}).then(() => { }).then(() => {
let plainText = {data : stringToUint8Array('this is test!')}; let plainText = {data : stringToUint8Array('this is test!')};
let promiseUpdate = globalCipher.update(plainText); // update let promiseUpdate = globalCipher.update(plainText); // update
return promiseUpdate; return promiseUpdate;
}).then(updateOutput => { }).then(updateOutput => {
globalCipherText = updateOutput; globalCipherText = updateOutput;
let promiseFinal = globalCipher.doFinal(null); // doFinal let promiseFinal = globalCipher.doFinal(null); // doFinal
return promiseFinal; return promiseFinal;
}).then(authTag => { }).then(authTag => {
// GCM模式需要从doFinal的输出中取出加密后的认证信息并填入globalGcmParams,在解密时传入init() // GCM模式需要从doFinal的输出中取出加密后的认证信息并填入globalGcmParams,在解密时传入init()
globalGcmParams.authTag = authTag; globalGcmParams.authTag = authTag;
return; return;
}).then(() => { }).then(() => {
// 初始化加解密操作环境:开始解密 // 初始化加解密操作环境:开始解密
let mode = cryptoFramework.CryptoMode.DECRYPT_MODE; let mode = cryptoFramework.CryptoMode.DECRYPT_MODE;
let promiseInit = globalCipher.init(mode, globalKey, globalGcmParams); // init let promiseInit = globalCipher.init(mode, globalKey, globalGcmParams); // init
return promiseInit; return promiseInit;
}).then(() => { }).then(() => {
let promiseUpdate = globalCipher.update(globalCipherText); // update let promiseUpdate = globalCipher.update(globalCipherText); // update
return promiseUpdate; return promiseUpdate;
}).then(updateOutput => { }).then(updateOutput => {
console.info('decrypt plainText: ' + uint8ArrayToString(updateOutput.data)); console.info('decrypt plainText: ' + uint8ArrayToString(updateOutput.data));
let promiseFinal = globalCipher.doFinal(null); // doFinal let promiseFinal = globalCipher.doFinal(null); // doFinal
return promiseFinal; return promiseFinal;
}).then(finalOutput => { }).then(finalOutput => {
if (finalOutput == null) { // 使用finalOutput.data前,先判断结果是否为null if (finalOutput == null) { // 使用finalOutput.data前,先判断结果是否为null
console.info('GCM finalOutput is null'); console.info('GCM finalOutput is null');
} }
}).catch(error => { }).catch(error => {
console.error(`catch error, ${error.code}, ${error.message}`); console.error(`catch error, ${error.code}, ${error.message}`);
}) })
} }
``` ```
...@@ -765,8 +772,8 @@ function test3DesEcb() { ...@@ -765,8 +772,8 @@ function test3DesEcb() {
// 初始化加解密操作环境:开始加密 // 初始化加解密操作环境:开始加密
let mode = cryptoFramework.CryptoMode.ENCRYPT_MODE; let mode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
// init // init
globalCipher.init(mode, key, null, (err, ) => { globalCipher.init(mode, key, null, (err,) => {
let plainText = {data : stringToUint8Array('this is test!')}; let plainText = { data: stringToUint8Array('this is test!') };
// update // update
globalCipher.update(plainText, (err, updateOutput) => { globalCipher.update(plainText, (err, updateOutput) => {
globalCipherText = updateOutput; globalCipherText = updateOutput;
...@@ -781,19 +788,19 @@ function test3DesEcb() { ...@@ -781,19 +788,19 @@ function test3DesEcb() {
finalOutput = Array.from(finalOutput.data); finalOutput = Array.from(finalOutput.data);
globalCipherText = globalCipherText.concat(finalOutput); globalCipherText = globalCipherText.concat(finalOutput);
globalCipherText = new Uint8Array(globalCipherText); globalCipherText = new Uint8Array(globalCipherText);
globalCipherText = {data : globalCipherText}; globalCipherText = { data: globalCipherText };
} }
// 初始化加解密操作环境:开始解密 // 初始化加解密操作环境:开始解密
let mode = cryptoFramework.CryptoMode.DECRYPT_MODE; let mode = cryptoFramework.CryptoMode.DECRYPT_MODE;
// init // init
globalCipher.init(mode, globalKey, null, (err, ) => { globalCipher.init(mode, globalKey, null, (err,) => {
// update // update
globalCipher.update(globalCipherText, (err, updateOutput) => { globalCipher.update(globalCipherText, (err, updateOutput) => {
console.info('decrypt plainText: ' + uint8ArrayToString(updateOutput.data)); console.info('decrypt plainText: ' + uint8ArrayToString(updateOutput.data));
// doFinal // doFinal
globalCipher.doFinal(null, (error, finalOutput) => { globalCipher.doFinal(null, (error, finalOutput) => {
if (finalOutput != null) { // 使用finalOutput.data前,先判断结果是否为null if (finalOutput != null) { // 使用finalOutput.data前,先判断结果是否为null
console.info("decrypt plainText:" + uint8ArrayToString(finalOutput.data)); console.info('decrypt plainText: ' + uint8ArrayToString(finalOutput.data));
} }
}) })
}) })
...@@ -891,72 +898,82 @@ function testAesMultiUpdate() { ...@@ -891,72 +898,82 @@ function testAesMultiUpdate() {
return; return;
} }
return promiseSymKey; return promiseSymKey;
}).then(key => { })
let encodedKey = key.getEncoded(); .then(key => {
console.info('key hex:' + uint8ArrayToShowStr(encodedKey.data)); let encodedKey = key.getEncoded();
globalKey = key; console.info('key hex:' + uint8ArrayToShowStr(encodedKey.data));
return key; globalKey = key;
}).then(key => { return key;
// 初始化加解密操作环境:开始加密 })
let mode = cryptoFramework.CryptoMode.ENCRYPT_MODE; .then(key => {
let promiseInit = globalCipher.init(mode, key, globalGcmParams); // init // 初始化加解密操作环境:开始加密
return promiseInit; let mode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
}).then(async () => { let promiseInit = globalCipher.init(mode, key, globalGcmParams); // init
let plainText = "aaaaa.....bbbbb.....ccccc.....ddddd.....eee"; // 假设明文总共43字节 return promiseInit;
let messageArr = []; })
let updateLength = 20; // 假设每20字节分段update一次 .then(async () => {
globalCipherText = []; let plainText = "aaaaa.....bbbbb.....ccccc.....ddddd.....eee"; // 假设明文总共43字节
let messageArr = [];
for (let i = 0; i <= plainText.length; i++) { let updateLength = 20; // 假设每20字节分段update一次
if ((i % updateLength == 0 || i == plainText.length) && messageArr.length != 0) { globalCipherText = [];
for (let i = 0; i <= plainText.length; i++) {
if ((i % updateLength == 0 || i == plainText.length) && messageArr.length != 0) {
let message = new Uint8Array(messageArr);
let messageBlob = { data: message };
let updateOutput = await globalCipher.update(messageBlob); // 分段update
// 把update的结果拼接起来,得到密文(有些情况下还需拼接doFinal的结果,这取决于分组模式
// 和填充模式,本例中GCM模式的doFinal结果只包含authTag而不含密文,所以不需要拼接)
globalCipherText = globalCipherText.concat(Array.from(updateOutput.data));
messageArr = [];
}
if (i < plainText.length) {
messageArr.push(plainText.charCodeAt(i));
}
}
return;
})
.then(() => {
let promiseFinal = globalCipher.doFinal(null); // doFinal
return promiseFinal;
})
.then(authTag => {
// 获取加密后的认证信息
globalGcmParams.authTag = authTag;
return;
})
.then(() => {
// 初始化加解密操作环境:开始解密
let mode = cryptoFramework.CryptoMode.DECRYPT_MODE;
let promiseInit = globalCipher.init(mode, globalKey, globalGcmParams); // init
return promiseInit;
})
.then(async () => {
let updateLength = 20;
let updateTimes = Math.ceil(globalCipherText.length / updateLength); // 上取整
globalPlainText = "";
for (let i = 0; i < updateTimes; i++) {
let messageArr = globalCipherText.slice(i * updateLength, (i + 1) * updateLength);
let message = new Uint8Array(messageArr); let message = new Uint8Array(messageArr);
let messageBlob = { data : message }; let messageBlob = { data: message };
let updateOutput = await globalCipher.update(messageBlob); // 分段update let updateOutput = await globalCipher.update(messageBlob); // 分段update
// 把update的结果拼接起来,得到密文(有些情况下还需拼接doFinal的结果,这取决于分组模式 globalPlainText += uint8ArrayToString(updateOutput.data); // 恢复出原始明文
// 和填充模式,本例中GCM模式的doFinal结果只包含authTag而不含密文,所以不需要拼接)
globalCipherText = globalCipherText.concat(Array.from(updateOutput.data));
messageArr = [];
} }
if (i < plainText.length) { return;
messageArr.push(plainText.charCodeAt(i)); })
.then(() => {
let promiseFinal = globalCipher.doFinal(null); // doFinal
return promiseFinal;
})
.then(finalOutput => {
if (finalOutput == null) {
console.info('GCM finalOutput is null');
} }
} console.info(`decrypt output: ${globalPlainText}`);
return; })
}).then(() => { .catch(error => {
let promiseFinal = globalCipher.doFinal(null); // doFinal
return promiseFinal;
}).then(authTag => {
// 获取加密后的认证信息
globalGcmParams.authTag = authTag;
return;
}).then(() => {
// 初始化加解密操作环境:开始解密
let mode = cryptoFramework.CryptoMode.DECRYPT_MODE;
let promiseInit = globalCipher.init(mode, globalKey, globalGcmParams); // init
return promiseInit;
}).then(async () => {
let updateLength = 20;
let updateTimes = Math.ceil(globalCipherText.length / updateLength); // 上取整
globalPlainText = "";
for (let i = 0; i < updateTimes; i++) {
let messageArr = globalCipherText.slice(i * updateLength, (i + 1) * updateLength);
let message = new Uint8Array(messageArr);
let messageBlob = { data : message };
let updateOutput = await globalCipher.update(messageBlob); // 分段update
globalPlainText += uint8ArrayToString(updateOutput.data); // 恢复出原始明文
}
return;
}).then(() => {
let promiseFinal = globalCipher.doFinal(null); // doFinal
return promiseFinal;
}).then(finalOutput => {
if (finalOutput == null) {
console.info('GCM finalOutput is null');
}
console.info(`decrypt output: ${globalPlainText}`);
}).catch(error => {
console.error(`catch error, ${error.code}, ${error.message}`); console.error(`catch error, ${error.code}, ${error.message}`);
}) })
} }
``` ```
...@@ -996,7 +1013,7 @@ function encryptMessagePromise() { ...@@ -996,7 +1013,7 @@ function encryptMessagePromise() {
return cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null); return cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null);
}).then(() => { }).then(() => {
// doFinal // doFinal
let input = { data : stringToUint8Array(plan) }; let input = { data: stringToUint8Array(plan) };
return cipher.doFinal(input); return cipher.doFinal(input);
}).then(dataBlob => { }).then(dataBlob => {
// 获取加密后的信息 // 获取加密后的信息
...@@ -1015,7 +1032,7 @@ function encryptMessageCallback() { ...@@ -1015,7 +1032,7 @@ function encryptMessageCallback() {
let pubKey = keyPair.pubKey; let pubKey = keyPair.pubKey;
// 初始化加解密操作环境:使用公钥开始加密 // 初始化加解密操作环境:使用公钥开始加密
cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null, function (err, data) { cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null, function (err, data) {
let input = {data : stringToUint8Array(plan) }; let input = { data: stringToUint8Array(plan) };
// doFinal // doFinal
cipher.doFinal(input, function (err, data) { cipher.doFinal(input, function (err, data) {
// 获取加密后的信息 // 获取加密后的信息
...@@ -1037,32 +1054,36 @@ function decryptMessagePromise() { ...@@ -1037,32 +1054,36 @@ function decryptMessagePromise() {
let keyGenPromise = rsaGenerator.generateKeyPair(); let keyGenPromise = rsaGenerator.generateKeyPair();
let keyPair; let keyPair;
let cipherDataBlob; let cipherDataBlob;
let input = { data : stringToUint8Array(plan) }; let input = { data: stringToUint8Array(plan) };
keyGenPromise.then(rsaKeyPair => { keyGenPromise.then(rsaKeyPair => {
keyPair = rsaKeyPair; keyPair = rsaKeyPair;
// 初始化加解密操作环境:使用公钥开始加密 // 初始化加解密操作环境:使用公钥开始加密
return cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyPair.pubKey, null); return cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyPair.pubKey, null);
}).then(() => { })
// 加密doFinal .then(() => {
return cipher.doFinal(input); // 加密doFinal
}).then(dataBlob => { return cipher.doFinal(input);
// 获取加密后的信息,并用于解密的入参 })
console.info("EncryptOutPut is " + dataBlob.data); .then(dataBlob => {
AlertDialog.show({message : "output" + dataBlob.data}); // 获取加密后的信息,并用于解密的入参
cipherDataBlob = dataBlob; console.info("EncryptOutPut is " + dataBlob.data);
// 初始化加解密操作环境:使用私钥开始解密 AlertDialog.show({ message: "output" + dataBlob.data });
return decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.priKey, null); cipherDataBlob = dataBlob;
}).then(() => { // 初始化加解密操作环境:使用私钥开始解密
// 解密doFinal return decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.priKey, null);
return decoder.doFinal(cipherDataBlob); })
}).then(decodeData => { .then(() => {
// 验证解密后,数据与原先数据是否保持一致 // 解密doFinal
if (decodeData.data.toString() === input.data.toString()) { return decoder.doFinal(cipherDataBlob);
AlertDialog.show({message : "decrypt success"}); })
return; .then(decodeData => {
} // 验证解密后,数据与原先数据是否保持一致
AlertDialog.show({message : "decrypt fail"}); if (decodeData.data.toString() === input.data.toString()) {
}); AlertDialog.show({ message: "decrypt success" });
return;
}
AlertDialog.show({ message: "decrypt fail" });
});
} }
// 以Callback方式加解密 // 以Callback方式加解密
...@@ -1074,7 +1095,7 @@ function decryptMessageCallback() { ...@@ -1074,7 +1095,7 @@ function decryptMessageCallback() {
// 生成加解密生成器,用于解密 // 生成加解密生成器,用于解密
let decoder = cryptoFramework.createCipher("RSA1024|PKCS1"); let decoder = cryptoFramework.createCipher("RSA1024|PKCS1");
let plainText = "this is cipher text"; let plainText = "this is cipher text";
let input = {data : stringToUint8Array(plainText) }; let input = { data: stringToUint8Array(plainText) };
let cipherData; let cipherData;
let keyPair; let keyPair;
// 通过非对称秘钥生成器生成非对称密钥对 // 通过非对称秘钥生成器生成非对称密钥对
...@@ -1085,7 +1106,7 @@ function decryptMessageCallback() { ...@@ -1085,7 +1106,7 @@ function decryptMessageCallback() {
// 加密doFinal // 加密doFinal
cipher.doFinal(input, function (err, data) { cipher.doFinal(input, function (err, data) {
// 获取加密后的信息,并用于解密的入参 // 获取加密后的信息,并用于解密的入参
AlertDialog.show({ message : "EncryptOutPut is " + data.data} ); AlertDialog.show({ message: "EncryptOutPut is " + data.data });
cipherData = data; cipherData = data;
// 初始化加解密操作环境:使用私钥开始解密 // 初始化加解密操作环境:使用私钥开始解密
decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.priKey, null, function (err, data) { decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.priKey, null, function (err, data) {
...@@ -1093,10 +1114,10 @@ function decryptMessageCallback() { ...@@ -1093,10 +1114,10 @@ function decryptMessageCallback() {
decoder.doFinal(cipherData, function (err, data) { decoder.doFinal(cipherData, function (err, data) {
// 验证解密后,数据与原先数据是否保持一致 // 验证解密后,数据与原先数据是否保持一致
if (input.data.toString() === data.data.toString()) { if (input.data.toString() === data.data.toString()) {
AlertDialog.show({ message : "decrype success"} ); AlertDialog.show({ message: "decrype success" });
return; return;
} }
AlertDialog.show({ message : "decrype fail"} ); AlertDialog.show({ message: "decrype fail" });
}); });
}); });
}); });
...@@ -1224,9 +1245,15 @@ function stringToUint8Array(str) { ...@@ -1224,9 +1245,15 @@ function stringToUint8Array(str) {
} }
// 根据密钥参数属性构造RSA非对称密钥对密钥参数 // 根据密钥参数属性构造RSA非对称密钥对密钥参数
function genRsaKeyPairSpec(nIn : bigint, eIn : bigint, dIn : bigint) { function genRsaKeyPairSpec(nIn: bigint, eIn: bigint, dIn: bigint) {
let rsaCommSpec = { n : nIn, algName : "RSA", specType : cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC }; let rsaCommSpec = { n: nIn, algName: "RSA", specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC };
let rsaKeyPairSpec = { params: rsaCommSpec, sk : dIn, pk : eIn, algName : "RSA", specType : cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC }; let rsaKeyPairSpec = {
params: rsaCommSpec,
sk: dIn,
pk: eIn,
algName: "RSA",
specType: cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC
};
return rsaKeyPairSpec; return rsaKeyPairSpec;
} }
...@@ -1250,63 +1277,67 @@ function rsaUseSpecDecryptOAEPPromise() { ...@@ -1250,63 +1277,67 @@ function rsaUseSpecDecryptOAEPPromise() {
let keyPair; let keyPair;
let cipherDataBlob; let cipherDataBlob;
// RSA加解密PKCS1-OAEP模式填充字节流P // RSA加解密PKCS1-OAEP模式填充字节流P
let pSource = new Uint8Array([1,2,3,4]); let pSource = new Uint8Array([1, 2, 3, 4]);
let input = { data : stringToUint8Array(plan) }; let input = { data: stringToUint8Array(plan) };
// 生成密钥 // 生成密钥
keyGenPromise.then(rsaKeyPair => { keyGenPromise.then(rsaKeyPair => {
keyPair = rsaKeyPair; keyPair = rsaKeyPair;
// 进行加密操作初始化 // 进行加密操作初始化
return cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyPair.pubKey, null); return cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyPair.pubKey, null);
}).then(() => { })
// get和set操作可以放在Cipher对象init之后,此处对cipher进行set和get操作 .then(() => {
cipher.setCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR, pSource); // get和set操作可以放在Cipher对象init之后,此处对cipher进行set和get操作
let retP = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR); cipher.setCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR, pSource);
// 比较get出来的P字节流与set进去的P字节流是否一致 let retP = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR);
if (retP.toString() != pSource.toString()) { // 比较get出来的P字节流与set进去的P字节流是否一致
AlertDialog.show({message : "error init pSource" + retP}); if (retP.toString() != pSource.toString()) {
} else { AlertDialog.show({ message: "error init pSource" + retP });
console.info("pSource changed ==" + retP); } else {
} console.info("pSource changed ==" + retP);
// 进行OAEP其他参数的get操作 }
let md = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MD_NAME_STR); // 进行OAEP其他参数的get操作
console.info("md == " + md); let md = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MD_NAME_STR);
let mgf = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF_NAME_STR); console.info("md == " + md);
console.info("mgf == " + mgf); let mgf = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF_NAME_STR);
let mgf1Md = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_MD_STR); console.info("mgf == " + mgf);
console.info("mgf1Md == " + mgf1Md); let mgf1Md = cipher.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_MD_STR);
return cipher.doFinal(input); console.info("mgf1Md == " + mgf1Md);
}).then(dataBlob => { return cipher.doFinal(input);
console.info("EncryptOutPut is " + dataBlob.data); })
cipherDataBlob = dataBlob; .then(dataBlob => {
// get和set操作可以放在Cipher对象init之前,且与init之后等价,此处对decoder进行set和get操作 console.info("EncryptOutPut is " + dataBlob.data);
decoder.setCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR, pSource); cipherDataBlob = dataBlob;
let retP = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR); // get和set操作可以放在Cipher对象init之前,且与init之后等价,此处对decoder进行set和get操作
// 比较get出来的P字节流与set进去的P字节流是否一致 decoder.setCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR, pSource);
if (retP.toString() != pSource.toString()) { let retP = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR);
AlertDialog.show({message : "error init pSource" + retP}); // 比较get出来的P字节流与set进去的P字节流是否一致
} else { if (retP.toString() != pSource.toString()) {
console.info("pSource changed ==" + retP); AlertDialog.show({ message: "error init pSource" + retP });
} } else {
// 进行OAEP其他参数的get操作 console.info("pSource changed ==" + retP);
let md = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MD_NAME_STR); }
console.info("md == " + md); // 进行OAEP其他参数的get操作
let mgf = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF_NAME_STR); let md = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MD_NAME_STR);
console.info("mgf == " + mgf); console.info("md == " + md);
let mgf1Md = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_MD_STR); let mgf = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF_NAME_STR);
console.info("mgf1Md == " + mgf1Md); console.info("mgf == " + mgf);
// 初始化解密操作 let mgf1Md = decoder.getCipherSpec(cryptoFramework.CipherSpecItem.OAEP_MGF1_MD_STR);
return decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.priKey, null); console.info("mgf1Md == " + mgf1Md);
}).then(() => { // 初始化解密操作
return decoder.doFinal(cipherDataBlob); return decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.priKey, null);
}).then(decodeData => { })
// 解密成功 .then(() => {
if (decodeData.data.toString() === input.data.toString()) { return decoder.doFinal(cipherDataBlob);
console.info("oaep decrypt success"); })
AlertDialog.show({message : " oaep decrypt success"}); .then(decodeData => {
} else { // 解密成功
AlertDialog.show({message : "oeap decrypt fail"}); if (decodeData.data.toString() === input.data.toString()) {
} console.info("oaep decrypt success");
}); AlertDialog.show({ message: " oaep decrypt success" });
} else {
AlertDialog.show({ message: "oeap decrypt fail" });
}
});
} }
``` ```
...@@ -1350,7 +1381,7 @@ function encryptMessagePromise() { ...@@ -1350,7 +1381,7 @@ function encryptMessagePromise() {
return cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null); return cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null);
}).then(() => { }).then(() => {
// doFinal // doFinal
let input = { data : stringToUint8Array(plan) }; let input = { data: stringToUint8Array(plan) };
return cipher.doFinal(input); return cipher.doFinal(input);
}).then(dataBlob => { }).then(dataBlob => {
// 获取加密后的信息 // 获取加密后的信息
...@@ -1369,7 +1400,7 @@ function encryptMessageCallback() { ...@@ -1369,7 +1400,7 @@ function encryptMessageCallback() {
let pubKey = keyPair.pubKey; let pubKey = keyPair.pubKey;
// 初始化加解密操作环境:使用公钥开始加密 // 初始化加解密操作环境:使用公钥开始加密
cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null, function (err, data) { cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, pubKey, null, function (err, data) {
let input = {data : stringToUint8Array(plan) }; let input = { data: stringToUint8Array(plan) };
// doFinal // doFinal
cipher.doFinal(input, function (err, data) { cipher.doFinal(input, function (err, data) {
// 获取加密后的信息 // 获取加密后的信息
...@@ -1382,64 +1413,68 @@ function encryptMessageCallback() { ...@@ -1382,64 +1413,68 @@ function encryptMessageCallback() {
// 以Promise方式加解密 // 以Promise方式加解密
function decryptMessagePromise() { function decryptMessagePromise() {
// 生成非对称密钥生成器 // 生成非对称密钥生成器
let rsaGenerator = cryptoFramework.createAsyKeyGenerator("SM2_256"); let sm2Generator = cryptoFramework.createAsyKeyGenerator("SM2_256");
// 生成加解密生成器,用于加密 // 生成加解密生成器,用于加密
let cipher = cryptoFramework.createCipher("SM2_256|SM3"); let cipher = cryptoFramework.createCipher("SM2_256|SM3");
// 生成加解密生成器,用于解密 // 生成加解密生成器,用于解密
let decoder = cryptoFramework.createCipher("SM2_256|SM3"); let decoder = cryptoFramework.createCipher("SM2_256|SM3");
// 通过非对称秘钥生成器生成非对称密钥对 // 通过非对称秘钥生成器生成非对称密钥对
let keyGenPromise = rsaGenerator.generateKeyPair(); let keyGenPromise = sm2Generator.generateKeyPair();
let keyPair; let keyPair;
let cipherDataBlob; let cipherDataBlob;
let input = { data : stringToUint8Array(plan) }; let input = { data: stringToUint8Array(plan) };
keyGenPromise.then(rsaKeyPair => { keyGenPromise.then(rsaKeyPair => {
keyPair = rsaKeyPair; keyPair = rsaKeyPair;
// 初始化加解密操作环境:使用公钥开始加密 // 初始化加解密操作环境:使用公钥开始加密
return cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyPair.pubKey, null); return cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyPair.pubKey, null);
}).then(() => { })
// 加密doFinal .then(() => {
return cipher.doFinal(input); // 加密doFinal
}).then(dataBlob => { return cipher.doFinal(input);
// 获取加密后的信息,并用于解密的入参 })
console.info("EncryptOutPut is " + dataBlob.data); .then(dataBlob => {
AlertDialog.show({message : "output" + dataBlob.data}); // 获取加密后的信息,并用于解密的入参
cipherDataBlob = dataBlob; console.info("EncryptOutPut is " + dataBlob.data);
// 初始化加解密操作环境:使用私钥开始解密 AlertDialog.show({ message: "output" + dataBlob.data });
return decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.priKey, null); cipherDataBlob = dataBlob;
}).then(() => { // 初始化加解密操作环境:使用私钥开始解密
// 解密doFinal return decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.priKey, null);
return decoder.doFinal(cipherDataBlob); })
}).then(decodeData => { .then(() => {
// 验证解密后,数据与原先数据是否保持一致 // 解密doFinal
if (decodeData.data.toString() === input.data.toString()) { return decoder.doFinal(cipherDataBlob);
AlertDialog.show({message : "decrypt success"}); })
return; .then(decodeData => {
} // 验证解密后,数据与原先数据是否保持一致
AlertDialog.show({message : "decrypt fail"}); if (decodeData.data.toString() === input.data.toString()) {
}); AlertDialog.show({ message: "decrypt success" });
return;
}
AlertDialog.show({ message: "decrypt fail" });
});
} }
// 以Callback方式加解密 // 以Callback方式加解密
function decryptMessageCallback() { function decryptMessageCallback() {
// 生成非对称密钥生成器 // 生成非对称密钥生成器
let rsaGenerator = cryptoFramework.createAsyKeyGenerator("SM2_256"); let sm2Generator = cryptoFramework.createAsyKeyGenerator("SM2_256");
// 生成加解密生成器,用于加密 // 生成加解密生成器,用于加密
let cipher = cryptoFramework.createCipher("SM2_256|SM3"); let cipher = cryptoFramework.createCipher("SM2_256|SM3");
// 生成加解密生成器,用于解密 // 生成加解密生成器,用于解密
let decoder = cryptoFramework.createCipher("SM2_256|SM3"); let decoder = cryptoFramework.createCipher("SM2_256|SM3");
let plainText = "this is cipher text"; let plainText = "this is cipher text";
let input = {data : stringToUint8Array(plainText) }; let input = { data: stringToUint8Array(plainText) };
let cipherData; let cipherData;
let keyPair; let keyPair;
// 通过非对称秘钥生成器生成非对称密钥对 // 通过非对称秘钥生成器生成非对称密钥对
rsaGenerator.generateKeyPair(function (err, newKeyPair) { sm2Generator.generateKeyPair(function (err, newKeyPair) {
keyPair = newKeyPair; keyPair = newKeyPair;
// 初始化加解密操作环境:使用公钥开始加密 // 初始化加解密操作环境:使用公钥开始加密
cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyPair.pubKey, null, function (err, data) { cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyPair.pubKey, null, function (err, data) {
// 加密doFinal // 加密doFinal
cipher.doFinal(input, function (err, data) { cipher.doFinal(input, function (err, data) {
// 获取加密后的信息,并用于解密的入参 // 获取加密后的信息,并用于解密的入参
AlertDialog.show({ message : "EncryptOutPut is " + data.data} ); AlertDialog.show({ message: "EncryptOutPut is " + data.data });
cipherData = data; cipherData = data;
// 初始化加解密操作环境:使用私钥开始解密 // 初始化加解密操作环境:使用私钥开始解密
decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.priKey, null, function (err, data) { decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.priKey, null, function (err, data) {
...@@ -1447,10 +1482,10 @@ function decryptMessageCallback() { ...@@ -1447,10 +1482,10 @@ function decryptMessageCallback() {
decoder.doFinal(cipherData, function (err, data) { decoder.doFinal(cipherData, function (err, data) {
// 验证解密后,数据与原先数据是否保持一致 // 验证解密后,数据与原先数据是否保持一致
if (input.data.toString() === data.data.toString()) { if (input.data.toString() === data.data.toString()) {
AlertDialog.show({ message : "decrype success"} ); AlertDialog.show({ message: "decrype success" });
return; return;
} }
AlertDialog.show({ message : "decrype fail"} ); AlertDialog.show({ message: "decrype fail" });
}); });
}); });
}); });
...@@ -1495,24 +1530,24 @@ function uint8ArrayToString(array) { ...@@ -1495,24 +1530,24 @@ function uint8ArrayToString(array) {
// SM4 ECB模式示例,callback写法 // SM4 ECB模式示例,callback写法
function testSM4Ecb() { function testSM4Ecb() {
// 生成非对称密钥生成器 // 生成非对称密钥生成器
let rsaGenerator = cryptoFramework.createSymKeyGenerator('SM4_128'); let sm4Generator = cryptoFramework.createSymKeyGenerator('SM4_128');
// 生成加解密生成器,用于加密 // 生成加解密生成器,用于加密
let cipher = cryptoFramework.createCipher("SM4_128|ECB|PKCS7"); let cipher = cryptoFramework.createCipher("SM4_128|ECB|PKCS7");
// 生成加解密生成器,用于解密 // 生成加解密生成器,用于解密
let decoder = cryptoFramework.createCipher("SM4_128|ECB|PKCS7"); let decoder = cryptoFramework.createCipher("SM4_128|ECB|PKCS7");
let plainText = "this is cipher text"; let plainText = "this is cipher text";
let input = {data : stringToUint8Array(plainText) }; let input = { data: stringToUint8Array(plainText) };
let cipherData; let cipherData;
let key; let key;
// 通过非对称秘钥生成器生成非对称密钥对 // 通过非对称秘钥生成器生成非对称密钥对
rsaGenerator.generateSymKey(function (err, newKey) { sm4Generator.generateSymKey(function (err, newKey) {
key = newKey; key = newKey;
// 初始化加解密操作环境:使用公钥开始加密 // 初始化加解密操作环境:使用公钥开始加密
cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, key, null, function (err, data) { cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, key, null, function (err, data) {
// 加密doFinal // 加密doFinal
cipher.doFinal(input, function (err, data) { cipher.doFinal(input, function (err, data) {
// 获取加密后的信息,并用于解密的入参 // 获取加密后的信息,并用于解密的入参
AlertDialog.show({ message : "EncryptOutPut is " + data.data} ); AlertDialog.show({ message: "EncryptOutPut is " + data.data });
cipherData = data; cipherData = data;
// 初始化加解密操作环境:使用私钥开始解密 // 初始化加解密操作环境:使用私钥开始解密
decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, key, null, function (err, data) { decoder.init(cryptoFramework.CryptoMode.DECRYPT_MODE, key, null, function (err, data) {
...@@ -1520,10 +1555,10 @@ function testSM4Ecb() { ...@@ -1520,10 +1555,10 @@ function testSM4Ecb() {
decoder.doFinal(cipherData, function (err, data) { decoder.doFinal(cipherData, function (err, data) {
// 验证解密后,数据与原先数据是否保持一致 // 验证解密后,数据与原先数据是否保持一致
if (input.data.toString() === data.data.toString()) { if (input.data.toString() === data.data.toString()) {
AlertDialog.show({ message : "decrype success"} ); AlertDialog.show({ message: "decrype success" });
return; return;
} }
AlertDialog.show({ message : "decrype fail"} ); AlertDialog.show({ message: "decrype fail" });
}); });
}); });
}); });
...@@ -1598,14 +1633,14 @@ let globalKeyPair; ...@@ -1598,14 +1633,14 @@ let globalKeyPair;
let SignMessageBlob; let SignMessageBlob;
let plan1 = "This is Sign test plan1"; let plan1 = "This is Sign test plan1";
let plan2 = "This is Sign test plan1"; let plan2 = "This is Sign test plan1";
let input1 = { data : stringToUint8Array(plan1) }; let input1 = { data: stringToUint8Array(plan1) };
let input2 = { data : stringToUint8Array(plan2) }; let input2 = { data: stringToUint8Array(plan2) };
function signMessagePromise() { function signMessagePromise() {
let rsaGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_2"); let rsaGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_2");
let signer = cryptoFramework.createSign("RSA1024|PKCS1|SHA256"); // API version 10开始,支持"RSA|PKCS1|SHA256" let signer = cryptoFramework.createSign("RSA1024|PKCS1|SHA256"); // API version 10开始,支持"RSA|PKCS1|SHA256"
let keyGenPromise = rsaGenerator.generateKeyPair(); let keyGenPromise = rsaGenerator.generateKeyPair();
keyGenPromise.then( keyPair => { keyGenPromise.then(keyPair => {
globalKeyPair = keyPair; globalKeyPair = keyPair;
let priKey = globalKeyPair.priKey; let priKey = globalKeyPair.priKey;
return signer.init(priKey); return signer.init(priKey);
...@@ -1619,6 +1654,7 @@ function signMessagePromise() { ...@@ -1619,6 +1654,7 @@ function signMessagePromise() {
}); });
} }
// 调用完sign操作后调用verify
function verifyMessagePromise() { function verifyMessagePromise() {
let verifyer = cryptoFramework.createVerify("RSA1024|PKCS1|SHA256"); let verifyer = cryptoFramework.createVerify("RSA1024|PKCS1|SHA256");
let verifyInitPromise = verifyer.init(globalKeyPair.pubKey); let verifyInitPromise = verifyer.init(globalKeyPair.pubKey);
...@@ -1637,9 +1673,9 @@ function signMessageCallback() { ...@@ -1637,9 +1673,9 @@ function signMessageCallback() {
rsaGenerator.generateKeyPair(function (err, keyPair) { rsaGenerator.generateKeyPair(function (err, keyPair) {
globalKeyPair = keyPair; globalKeyPair = keyPair;
let priKey = globalKeyPair.priKey; let priKey = globalKeyPair.priKey;
signer.init(priKey, function (err, data) { signer.init(priKey, err => {
signer.update(input1, function (err, data) { signer.update(input1, err => {
signer.sign(input2, function (err, data) { signer.sign(input2, (err, data) => {
SignMessageBlob = data; SignMessageBlob = data;
console.info("sign output is " + SignMessageBlob.data); console.info("sign output is " + SignMessageBlob.data);
}); });
...@@ -1648,11 +1684,12 @@ function signMessageCallback() { ...@@ -1648,11 +1684,12 @@ function signMessageCallback() {
}); });
} }
// 调用完sign操作后调用verify
function verifyMessageCallback() { function verifyMessageCallback() {
let verifyer = cryptoFramework.createVerify("RSA1024|PKCS1|SHA256"); let verifyer = cryptoFramework.createVerify("RSA1024|PKCS1|SHA256");
verifyer.init(globalKeyPair.pubKey, function (err, data) { verifyer.init(globalKeyPair.pubKey, err => {
verifyer.update(input1, function(err, data) { verifyer.update(input1, err => {
verifyer.verify(input2, SignMessageBlob, function(err, data) { verifyer.verify(input2, SignMessageBlob, function (err, data) {
console.info("verify result is " + data); console.info("verify result is " + data);
}); });
}); });
...@@ -1686,14 +1723,14 @@ let globalKeyPair; ...@@ -1686,14 +1723,14 @@ let globalKeyPair;
let SignMessageBlob; let SignMessageBlob;
let plan1 = "This is Sign test plan1"; let plan1 = "This is Sign test plan1";
let plan2 = "This is Sign test plan1"; let plan2 = "This is Sign test plan1";
let input1 = { data : stringToUint8Array(plan1) }; let input1 = { data: stringToUint8Array(plan1) };
let input2 = { data : stringToUint8Array(plan2) }; let input2 = { data: stringToUint8Array(plan2) };
function signMessagePromise() { function signMessagePromise() {
let eccGenerator = cryptoFramework.createAsyKeyGenerator("ECC256"); let eccGenerator = cryptoFramework.createAsyKeyGenerator("ECC256");
let signer = cryptoFramework.createSign("ECC256|SHA256"); let signer = cryptoFramework.createSign("ECC256|SHA256");
let keyGenPromise = eccGenerator.generateKeyPair(); let keyGenPromise = eccGenerator.generateKeyPair();
keyGenPromise.then( keyPair => { keyGenPromise.then(keyPair => {
globalKeyPair = keyPair; globalKeyPair = keyPair;
let priKey = globalKeyPair.priKey; let priKey = globalKeyPair.priKey;
return signer.init(priKey); return signer.init(priKey);
...@@ -1725,9 +1762,9 @@ function signMessageCallback() { ...@@ -1725,9 +1762,9 @@ function signMessageCallback() {
eccGenerator.generateKeyPair(function (err, keyPair) { eccGenerator.generateKeyPair(function (err, keyPair) {
globalKeyPair = keyPair; globalKeyPair = keyPair;
let priKey = globalKeyPair.priKey; let priKey = globalKeyPair.priKey;
signer.init(priKey, function (err, data) { signer.init(priKey, err => {
signer.update(input1, function (err, data) { signer.update(input1, err => {
signer.sign(input2, function (err, data) { signer.sign(input2, (err, data) => {
SignMessageBlob = data; SignMessageBlob = data;
console.info("sign output is " + SignMessageBlob.data); console.info("sign output is " + SignMessageBlob.data);
}); });
...@@ -1738,9 +1775,9 @@ function signMessageCallback() { ...@@ -1738,9 +1775,9 @@ function signMessageCallback() {
function verifyMessageCallback() { function verifyMessageCallback() {
let verifyer = cryptoFramework.createVerify("ECC256|SHA256"); let verifyer = cryptoFramework.createVerify("ECC256|SHA256");
verifyer.init(globalKeyPair.pubKey, function (err, data) { verifyer.init(globalKeyPair.pubKey, err => {
verifyer.update(input1, function(err, data) { verifyer.update(input1, err => {
verifyer.verify(input2, SignMessageBlob, function(err, data) { verifyer.verify(input2, SignMessageBlob, function (err, data) {
console.info("verify result is " + data); console.info("verify result is " + data);
}); });
}); });
...@@ -1847,9 +1884,15 @@ function stringToUint8Array(str) { ...@@ -1847,9 +1884,15 @@ function stringToUint8Array(str) {
} }
// 根据密钥参数属性构造RSA非对称密钥对密钥参数 // 根据密钥参数属性构造RSA非对称密钥对密钥参数
function genRsaKeyPairSpec(nIn : bigint, eIn : bigint, dIn : bigint) { function genRsaKeyPairSpec(nIn: bigint, eIn: bigint, dIn: bigint) {
let rsaCommSpec = { n : nIn, algName : "RSA", specType : cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC }; let rsaCommSpec = { n: nIn, algName: "RSA", specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC };
let rsaKeyPairSpec = { params: rsaCommSpec, sk : dIn, pk : eIn, algName : "RSA", specType : cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC }; let rsaKeyPairSpec = {
params: rsaCommSpec,
sk: dIn,
pk: eIn,
algName: "RSA",
specType: cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC
};
return rsaKeyPairSpec; return rsaKeyPairSpec;
} }
...@@ -1864,8 +1907,8 @@ function genRsa2048KeyPairSpec() { ...@@ -1864,8 +1907,8 @@ function genRsa2048KeyPairSpec() {
function verifyMessageCallbackPSS() { function verifyMessageCallbackPSS() {
let plan1 = "This is Sign test plan1"; let plan1 = "This is Sign test plan1";
let plan2 = "This is Sign test plan1"; let plan2 = "This is Sign test plan1";
let input1 = { data : stringToUint8Array(plan1) }; let input1 = { data: stringToUint8Array(plan1) };
let input2 = { data : stringToUint8Array(plan2) }; let input2 = { data: stringToUint8Array(plan2) };
let globalKeyPair; let globalKeyPair;
let signMessageBlob; let signMessageBlob;
// 获得RSA密钥对密钥参数对象 // 获得RSA密钥对密钥参数对象
...@@ -1877,7 +1920,7 @@ function verifyMessageCallbackPSS() { ...@@ -1877,7 +1920,7 @@ function verifyMessageCallbackPSS() {
let verifyer = cryptoFramework.createVerify("RSA2048|PSS|SHA256|MGF1_SHA256"); let verifyer = cryptoFramework.createVerify("RSA2048|PSS|SHA256|MGF1_SHA256");
rsaGeneratorSpec.generateKeyPair(function (err, keyPair) { rsaGeneratorSpec.generateKeyPair(function (err, keyPair) {
globalKeyPair = keyPair; globalKeyPair = keyPair;
signer.init(globalKeyPair.priKey, function (err, data) { signer.init(globalKeyPair.priKey, err => {
// 在签名初始化后,对PSS参数进行set和get操作 // 在签名初始化后,对PSS参数进行set和get操作
let setN = 32; let setN = 32;
signer.setSignSpec(cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM, setN); signer.setSignSpec(cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM, setN);
...@@ -1891,11 +1934,11 @@ function verifyMessageCallbackPSS() { ...@@ -1891,11 +1934,11 @@ function verifyMessageCallbackPSS() {
console.info("mgf == " + mgf); console.info("mgf == " + mgf);
let mgf1Md = signer.getSignSpec(cryptoFramework.SignSpecItem.PSS_MGF1_MD_STR); let mgf1Md = signer.getSignSpec(cryptoFramework.SignSpecItem.PSS_MGF1_MD_STR);
console.info("mgf1Md == " + mgf1Md); console.info("mgf1Md == " + mgf1Md);
signer.update(input1, function (err, data) { signer.update(input1, err => {
signer.sign(input2, function (err, data) { signer.sign(input2, function (err, data) {
// 在验签初始化前,对PSS参数进行set和get操作,功能与初始化后一致 // 在验签初始化前,对PSS参数进行set和get操作,功能与初始化后一致
signMessageBlob = data; signMessageBlob = data;
AlertDialog.show({message : "res" + signMessageBlob.data}); AlertDialog.show({ message: "res" + signMessageBlob.data });
let setN = 32; let setN = 32;
verifyer.setVerifySpec(cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM, setN); verifyer.setVerifySpec(cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM, setN);
let saltLen = verifyer.getVerifySpec(cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM); let saltLen = verifyer.getVerifySpec(cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM);
...@@ -1908,10 +1951,10 @@ function verifyMessageCallbackPSS() { ...@@ -1908,10 +1951,10 @@ function verifyMessageCallbackPSS() {
console.info("mgf == " + mgf); console.info("mgf == " + mgf);
let mgf1Md = verifyer.getVerifySpec(cryptoFramework.SignSpecItem.PSS_MGF1_MD_STR); let mgf1Md = verifyer.getVerifySpec(cryptoFramework.SignSpecItem.PSS_MGF1_MD_STR);
console.info("mgf1Md == " + mgf1Md); console.info("mgf1Md == " + mgf1Md);
verifyer.init(globalKeyPair.pubKey, function (err, data) { verifyer.init(globalKeyPair.pubKey, err => {
verifyer.update(input1, function(err, data) { verifyer.update(input1, err => {
verifyer.verify(input2, signMessageBlob, function(err, data) { verifyer.verify(input2, signMessageBlob, function (err, data) {
AlertDialog.show({message : "res " + data}); AlertDialog.show({ message: "res " + data });
}) })
}); });
}); });
...@@ -1949,30 +1992,30 @@ function stringToUint8Array(str) { ...@@ -1949,30 +1992,30 @@ function stringToUint8Array(str) {
return tmpArray; return tmpArray;
} }
let globalKeyPair;
let SignMessageBlob;
let plan1 = "This is Sign test plan1"; let plan1 = "This is Sign test plan1";
let plan2 = "This is Sign test plan2"; let plan2 = "This is Sign test plan2";
let input1 = { data: stringToUint8Array(plan1) }; let input1 = { data: stringToUint8Array(plan1) };
let input2 = { data: stringToUint8Array(plan2) }; let input2 = { data: stringToUint8Array(plan2) };
function signAndVerify() { function signAndVerify() {
let rsaGenerator = cryptoFramework.createAsyKeyGenerator("SM2_256"); let signMessageBlob;
let globalKeyPair;
let sm2Generator = cryptoFramework.createAsyKeyGenerator("SM2_256");
let signer = cryptoFramework.createSign("SM2_256|SM3"); let signer = cryptoFramework.createSign("SM2_256|SM3");
rsaGenerator.generateKeyPair(function (err, keyPair) { sm2Generator.generateKeyPair(function (err, keyPair) {
globalKeyPair = keyPair; globalKeyPair = keyPair;
let priKey = globalKeyPair.priKey; let priKey = globalKeyPair.priKey;
signer.init(priKey, function (err, data) { signer.init(priKey, err => {
signer.update(input1, function (err, data) { signer.update(input1, err => {
signer.sign(input2, function (err, data) { signer.sign(input2, function (err, data) {
SignMessageBlob = data; signMessageBlob = data;
console.info("sign output is " + SignMessageBlob.data); console.info("sign output is " + signMessageBlob.data);
let verifyer = cryptoFramework.createVerify("SM2_256|SM3"); let verifyer = cryptoFramework.createVerify("SM2_256|SM3");
verifyer.init(globalKeyPair.pubKey, function (err, data) { verifyer.init(globalKeyPair.pubKey, err => {
verifyer.update(input1, function (err, data) { verifyer.update(input1, err => {
verifyer.verify(input2, SignMessageBlob, function (err, data) { verifyer.verify(input2, signMessageBlob, function (err, data) {
console.info("verify result is " + data); console.info("verify result is " + data);
AlertDialog.show({message:"decrype success"}) AlertDialog.show({ message: "verify success" })
}); });
}); });
}) })
...@@ -2019,7 +2062,7 @@ function ecdhPromise() { ...@@ -2019,7 +2062,7 @@ function ecdhPromise() {
let eccGenerator = cryptoFramework.createAsyKeyGenerator("ECC256"); let eccGenerator = cryptoFramework.createAsyKeyGenerator("ECC256");
let eccKeyAgreement = cryptoFramework.createKeyAgreement("ECC256"); // API version 10开始,支持输入"ECC"来进行密钥协商 let eccKeyAgreement = cryptoFramework.createKeyAgreement("ECC256"); // API version 10开始,支持输入"ECC"来进行密钥协商
let keyGenPromise = eccGenerator.generateKeyPair(); let keyGenPromise = eccGenerator.generateKeyPair();
keyGenPromise.then( keyPair => { keyGenPromise.then(keyPair => {
globalKeyPair = keyPair; globalKeyPair = keyPair;
return eccKeyAgreement.generateSecret(keyPair.priKey, keyPair.pubKey); return eccKeyAgreement.generateSecret(keyPair.priKey, keyPair.pubKey);
}).then((secret) => { }).then((secret) => {
...@@ -2186,7 +2229,7 @@ async function doLoopMdPromise() { ...@@ -2186,7 +2229,7 @@ async function doLoopMdPromise() {
for (let i = 0; i <= messageText.length; i++) { for (let i = 0; i <= messageText.length; i++) {
if ((i % updateLength == 0 || i == messageText.length) && messageArr.length != 0) { if ((i % updateLength == 0 || i == messageText.length) && messageArr.length != 0) {
let message = new Uint8Array(messageArr); let message = new Uint8Array(messageArr);
let messageBlob = { data : message }; let messageBlob = { data: message };
// 使用await处理for循环里的update // 使用await处理for循环里的update
try { try {
await md.update(messageBlob); // 分段update await md.update(messageBlob); // 分段update
...@@ -2273,7 +2316,7 @@ function doHmacByPromise() { ...@@ -2273,7 +2316,7 @@ function doHmacByPromise() {
console.info("[Promise]: Mac algName is: " + mac.algName); console.info("[Promise]: Mac algName is: " + mac.algName);
let KeyBlob = { let KeyBlob = {
// 128位密钥 // 128位密钥
data : stringToUint8Array("12345678abcdefgh") data: stringToUint8Array("12345678abcdefgh")
} }
let symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128"); let symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128");
// 将二进制密钥转换为算法库密钥 // 将二进制密钥转换为算法库密钥
...@@ -2313,7 +2356,7 @@ function doHmacByCallback() { ...@@ -2313,7 +2356,7 @@ function doHmacByCallback() {
console.info("[Promise]: Mac algName is: " + mac.algName); console.info("[Promise]: Mac algName is: " + mac.algName);
let KeyBlob = { let KeyBlob = {
// 128位密钥 // 128位密钥
data : stringToUint8Array("12345678abcdefgh") data: stringToUint8Array("12345678abcdefgh")
} }
let symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128"); let symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128");
// 将二进制密钥转换为算法库密钥 // 将二进制密钥转换为算法库密钥
...@@ -2396,7 +2439,7 @@ function doLoopHmacPromise() { ...@@ -2396,7 +2439,7 @@ function doLoopHmacPromise() {
for (let i = 0; i <= messageText.length; i++) { for (let i = 0; i <= messageText.length; i++) {
if ((i % updateLength == 0 || i == messageText.length) && messageArr.length != 0) { if ((i % updateLength == 0 || i == messageText.length) && messageArr.length != 0) {
let message = new Uint8Array(messageArr); let message = new Uint8Array(messageArr);
let messageBlob = { data : message }; let messageBlob = { data: message };
// 使用await处理for循环里的update // 使用await处理for循环里的update
try { try {
promiseMacUpdate = await mac.update(messageBlob); // 分段update promiseMacUpdate = await mac.update(messageBlob); // 分段update
...@@ -2457,29 +2500,31 @@ function doLoopHmacPromise() { ...@@ -2457,29 +2500,31 @@ function doLoopHmacPromise() {
import cryptoFramework from "@ohos.security.cryptoFramework" import cryptoFramework from "@ohos.security.cryptoFramework"
// 通过Promise方式生成随机数 // 通过Promise方式生成随机数
function doRandByPromise(len) { function doRandByPromise() {
var rand; let rand;
let len = 4; // 生成长度4字节的随机数
try { try {
rand = cryptoFramework.createRandom(); rand = cryptoFramework.createRandom();
} catch (error) { } catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message); console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
} }
var promiseGenerateRand = rand.generateRandom(len); let promiseGenerateRand = rand.generateRandom(len);
promiseGenerateRand.then(randData => { promiseGenerateRand.then(randData => {
console.error("[Promise]: rand result: " + randData.data); console.info("[Promise]: rand result: " + randData.data);
try { try {
rand.setSeed(randData); rand.setSeed(randData);
} catch (error) { } catch (error) {
console.log("setSeed failed, errCode: " + error.code + ", errMsg: " + error.message); console.error("setSeed failed, errCode: " + error.code + ", errMsg: " + error.message);
} }
}).catch(error => { }).catch(error => {
console.error("[Promise]: error: " + error.message); console.error("[Promise]: error: " + error.message);
}); });
} }
// 通过Callback方式生成随机数 // 通过Callback方式生成随机数
function doRandByCallback(len) { function doRandByCallback() {
var rand; let rand;
let len = 4; // 生成长度4字节的随机数
try { try {
rand = cryptoFramework.createRandom(); rand = cryptoFramework.createRandom();
} catch (error) { } catch (error) {
...@@ -2489,19 +2534,20 @@ function doRandByCallback(len) { ...@@ -2489,19 +2534,20 @@ function doRandByCallback(len) {
if (err) { if (err) {
console.error("[Callback]: err: " + err.code); console.error("[Callback]: err: " + err.code);
} else { } else {
console.error("[Callback]: generate random result: " + randData.data); console.info("[Callback]: generate random result: " + randData.data);
try { try {
rand.setSeed(randData); rand.setSeed(randData);
} catch (error) { } catch (error) {
console.log("setSeed failed, errCode: " + error.code + ", errMsg: " + error.message); console.error("setSeed failed, errCode: " + error.code + ", errMsg: " + error.message);
} }
} }
}); });
} }
// 通过同步接口生成随机数 // 通过同步接口生成随机数
function doRandBySync(len) { function doRandBySync() {
var rand; let rand;
let len = 24; // 生成长度24字节的随机数
try { try {
rand = cryptoFramework.createRandom(); rand = cryptoFramework.createRandom();
} catch (error) { } catch (error) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册