From daa8e7417bf9d6246280c906b7d25de80dddfbb9 Mon Sep 17 00:00:00 2001 From: qiaozzzh Date: Wed, 28 Sep 2022 17:39:10 +0800 Subject: [PATCH] =?UTF-8?q?ActsCryptoFrameworkJSNormalTest=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=A5=97=E8=A1=A5=E5=85=85=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: qiaozzzh Change-Id: I0994a7b2782ef0c88535df8e7c8685a747fafa49 --- .../js/test/AsymmetricCryptography.test.js | 146 +++++++++++ .../src/main/js/test/List.test.js | 2 + .../asymmetric/publicAsymmetricCallback.js | 235 +++++++++++++++++ .../asymmetric/publicAsymmetricPromise.js | 243 ++++++++++++++++++ 4 files changed, 626 insertions(+) create mode 100644 security/cryptoFramework/src/main/js/test/AsymmetricCryptography.test.js create mode 100644 security/cryptoFramework/src/main/js/test/utils/asymmetric/publicAsymmetricCallback.js create mode 100644 security/cryptoFramework/src/main/js/test/utils/asymmetric/publicAsymmetricPromise.js diff --git a/security/cryptoFramework/src/main/js/test/AsymmetricCryptography.test.js b/security/cryptoFramework/src/main/js/test/AsymmetricCryptography.test.js new file mode 100644 index 000000000..7512d8bd9 --- /dev/null +++ b/security/cryptoFramework/src/main/js/test/AsymmetricCryptography.test.js @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { describe, beforeAll, afterEach, it, expect } from "@ohos/hypium"; +import { + testAsyKeyEncryptDecryptPromise, + testAsyKeySignVerifyPromise, + testAsyKeyECDHPromise, + testAsyKeyConvertKeyPromise, + testAsyKeyPriKeyClearPromise, +} from "./utils/asymmetric/publicAsymmetricPromise"; + + +export default function AsymmetricCryptographyJsunit() { + describe("AsymmetricCryptographyJsunit", function () { + console.info("##########start AsymmetricCryptographyJsunit##########"); + beforeAll(function () { + }); + afterEach(function () { + }); + + /** + * @tc.number Security_crypto_framework_ASymmetric_Encryption_RSA_0100 + * @tc.name Test RSA1024|PRIMES_2 normal encryption and decryption + * @tc.desc Use the Promise Style of Interface + */ + it( + "Security_crypto_framework_ASymmetric_Encryption_RSA_0100", + 0, + async function (done) { + testAsyKeyEncryptDecryptPromise("RSA1024|PRIMES_2", "RSA1024|PKCS1|SHA256"); + done(); + } + ); + + /** + * @tc.number Security_crypto_framework_ASymmetric_Encryption_RSA_0200 + * @tc.name Test RSA2048|PRIMES_2 normal encryption and decryption + * @tc.desc Use the Promise Style of Interface + */ + it( + "Security_crypto_framework_ASymmetric_Encryption_RSA_0200", + 0, + async function (done) { + testAsyKeyEncryptDecryptPromise("RSA2048|PRIMES_2", "RSA2048|PKCS1|MD5"); + done(); + } + ); + + /** + * @tc.number Security_crypto_framework_ASymmetric_SignVerify_RSA_0100 + * @tc.name Test RSA1024|PKCS1|MD5 normal sign and verify + * @tc.desc Use the Promise Style of Interface + */ + it( + "Security_crypto_framework_ASymmetric_SignVerify_RSA_0100", + 0, + async function (done) { + testAsyKeySignVerifyPromise("RSA1024|PRIMES_2", "RSA1024|PKCS1|MD5"); + done(); + } + ); + + /** + * @tc.number Security_crypto_framework_ASymmetric_SignVerify_RSA_0200 + * @tc.name Test RSA2048|PKCS1|SHA1 normal sign and verify + * @tc.desc Use the Promise Style of Interface + */ + it( + "Security_crypto_framework_ASymmetric_SignVerify_RSA_0200", + 0, + async function (done) { + testAsyKeySignVerifyPromise("RSA2048|PRIMES_2", "RSA2048|PKCS1|SHA1"); + done(); + } + ); + + /** + * @tc.number Security_crypto_framework_ASymmetric_SignVerify_ECDH_0100 + * @tc.name Test ECC224 SignVerify ECDH + * @tc.desc Use the Promise Style of Interface + */ + it( + "Security_crypto_framework_ASymmetric_SignVerify_ECDH_0100", + 0, + async function (done) { + testAsyKeyECDHPromise("ECC224"); + done(); + } + ); + + /** + * @tc.number Security_crypto_framework_ASymmetric_SignVerify_ECDH_0200 + * @tc.name Test ECC256 SignVerify ECDH + * @tc.desc Use the Promise Style of Interface + */ + it( + "Security_crypto_framework_ASymmetric_SignVerify_ECDH_0200", + 0, + async function (done) { + testAsyKeyECDHPromise("ECC256"); + done(); + } + ); + + /** + * @tc.number Security_crypto_framework_ASymmetric_Encryption_RSA_3300 + * @tc.name Test convertKey + * @tc.desc Use RSA3072|PRIMES_2 algorithm + */ + it( + "Security_crypto_framework_ASymmetric_Encryption_RSA_3300", + 0, + async function (done) { + testAsyKeyConvertKeyPromise("RSA3072|PRIMES_2"); + done(); + } + ); + + /** + * @tc.number Security_crypto_framework_ASymmetric_Encryption_RSA_3100 + * @tc.name Test convertKey + * @tc.desc Use RSA1024|PRIMES_2 algorithm + */ + it( + "Security_crypto_framework_ASymmetric_Encryption_RSA_3100", + 0, + async function (done) { + testAsyKeyPriKeyClearPromise("RSA1024|PRIMES_2"); + done(); + } + ); + }); +} diff --git a/security/cryptoFramework/src/main/js/test/List.test.js b/security/cryptoFramework/src/main/js/test/List.test.js index a90fbe39b..089124d8e 100644 --- a/security/cryptoFramework/src/main/js/test/List.test.js +++ b/security/cryptoFramework/src/main/js/test/List.test.js @@ -17,11 +17,13 @@ import SecurityRandomJsunit from "./SecurityRandom.test.js"; import DigestAlgorithmJsunit from "./DigestAlgorithm.test.js"; import CertificateJsunit from "./Certificate.test.js"; import SymmetricCryptographyJsunit from "./SymmetricCryptography.test.js"; +import AsymmetricCryptographyJsunit from "./AsymmetricCryptography.test.js"; export default function testsuite() { CertificateJsunit(); DigestAlgorithmJsunit(); SymmetricCryptographyJsunit(); + AsymmetricCryptographyJsunit(); SecurityRandomJsunit(); } \ No newline at end of file diff --git a/security/cryptoFramework/src/main/js/test/utils/asymmetric/publicAsymmetricCallback.js b/security/cryptoFramework/src/main/js/test/utils/asymmetric/publicAsymmetricCallback.js new file mode 100644 index 000000000..c3f7caa94 --- /dev/null +++ b/security/cryptoFramework/src/main/js/test/utils/asymmetric/publicAsymmetricCallback.js @@ -0,0 +1,235 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { expect } from "@ohos/hypium"; +import cryptoFramework from "@ohos.security.cryptoFramework"; +import { stringTouInt8Array, uInt8ArrayToShowStr, uInt8ArrayToString } from "../common/publicDoString"; + +async function testAsyKeyEncryptDecryptCallback(asyAlgoName, cipherAlgoName) { + var globalPubKey; + var globalPriKey; + var globalCipherEncrypt; + var globalCipherDecrypt; + var globalText = "This is a cipher test"; + var globalInput = { data: stringTouInt8Array(globalText) }; + + let rsaGenerator = cryptoFramework.createAsyKeyGenerator(asyAlgoName); + console.warn("[Callback]: algName = " + rsaGenerator.algName); + expect(rsaGenerator != null).assertTrue(); + globalCipherEncrypt = cryptoFramework.createCipher(cipherAlgoName); + globalCipherDecrypt = cryptoFramework.createCipher(cipherAlgoName); + expect(globalCipherEncrypt != null).assertTrue(); + expect(globalCipherDecrypt != null).assertTrue(); + + rsaGenerator.generateKeyPair((err, keyPair)=> { + if (err) { + expect(err == null).assertFail(); + } + expect(keyPair != null).assertTrue(); + globalPubKey = keyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Callback]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Callback]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Callback]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = keyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Callback]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Callback]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Callback]: priKey.getFormat= " + globalPriKey.format); + console.info("[Callback]:start init =====================[encrypt]"); + globalCipherEncrypt.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, globalPubKey, null, (err0,) => { + if (err0) { + expect(err0 == null).assertFail(); + } + console.warn("plaintext hex: " + uInt8ArrayToShowStr(globalInput.data)); + console.info("[Callback]:start doFinal =====================[encrypt]"); + globalCipherEncrypt.doFinal(globalInput, (err1, cipherFinalEncrypt) => { + if (err1) { + expect(err1 == null).assertFail(); + } + expect(cipherFinalEncrypt != null).assertTrue(); + console.log("cipherOutput: " + uInt8ArrayToShowStr(cipherFinalEncrypt.data)); + expect(globalCipherDecrypt != null).assertTrue(); + console.log("[Callback]:start init =====================[decrypt]"); + globalCipherDecrypt.init(cryptoFramework.CryptoMode.DECRYPT_MODE, globalPriKey, null, (err,) => { + console.log("[Callback]:start doFinal =====================[decrypt]"); + globalCipherDecrypt.doFinal(cipherFinalEncrypt, (err2, cipherFinalDecrypt) => { + if (err2) { + expect(err2 == null).assertFail(); + } + expect(cipherFinalDecrypt != null).assertTrue(); + if (cipherFinalDecrypt == null) { + console.warn("decrypt doFinal out is null"); + } else { + console.warn("decrypt doFinal output hex:" + uInt8ArrayToShowStr(cipherFinalDecrypt.data)); + console.warn("decrypt doFinal output :" + uInt8ArrayToString(cipherFinalDecrypt.data)); + } + let decryptData = uInt8ArrayToString(cipherFinalDecrypt.data); + expect(decryptData == globalText).assertTrue(); + }); + }) + }); + }); + }); +} + +async function testAsyKeySignVerifyCallback(asyAlgoName, signAlgoName) { + var globalPubKey; + var globalPriKey; + var globalSigner; + var globalVerify; + var globalText = "This is a sign test"; + var globalInput = { data: stringTouInt8Array(globalText) }; + var globalSignBlob; + + let rsaGenerator = cryptoFramework.createAsyKeyGenerator(asyAlgoName); + console.warn("[Callback]: algName = " + rsaGenerator.algName); + expect(rsaGenerator != null).assertTrue(); + globalSigner = cryptoFramework.createSign(signAlgoName); + globalVerify = cryptoFramework.createVerify(signAlgoName); + expect(globalSigner != null).assertTrue(); + expect(globalVerify != null).assertTrue(); + + rsaGenerator.generateKeyPair((err, keyPair)=> { + if (err) { + expect(err == null).assertFail(); + } + expect(keyPair != null).assertTrue(); + globalPubKey = keyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Callback]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Callback]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Callback]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = keyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Callback]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Callback]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Callback]: priKey.getFormat= " + globalPriKey.format); + globalSigner.init(globalPriKey, (err0,) => { + if (err0) { + expect(err0 == null).assertFail(); + } + console.warn("plaintext hex: " + uInt8ArrayToShowStr(globalInput.data)); + globalSigner.update(globalInput, (err1,) => { + if (err1) { + expect(err1 == null).assertFail(); + } + }); + globalSigner.sign(globalInput, (err2, signData) => { + if (err2) { + expect(err2 == null).assertFail(); + } + globalSignBlob = signData; + console.log("signOutput: " + uInt8ArrayToShowStr(signData.data)); + globalVerify.init(globalPubKey, null, (err3,) => { + if (err3) { + expect(err3 == null).assertFail(); + } + globalVerify.update(globalInput, (err4,) => { + if (err4) { + expect(err4 == null).assertFail(); + } + }); + globalVerify.verify(globalInput,globalSignBlob,(err5, finalStatus) => { + if (err5) { + expect(err5 == null).assertFail(); + } + expect(finalStatus).assertTrue(); + }); + }); + }); + }); + }); +} + +async function testAsyKeyECDHCallback(ECDHAlgoName) { + var globalPubKey; + var globalPriKey; + var globalECDHData; + + let rsaGenerator = cryptoFramework.createAsyKeyGenerator(ECDHAlgoName); + console.warn("[Callback]: algName = " + rsaGenerator.algName); + expect(rsaGenerator != null).assertTrue(); + rsaGenerator.generateKeyPair((err, rsaKeyPair)=> { + if (err) { + expect(err == null).assertFail(); + } + globalPubKey = rsaKeyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Callback]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Callback]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Callback]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = rsaKeyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Callback]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Callback]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Callback]: priKey.getFormat= " + globalPriKey.format); + globalECDHData = cryptoFramework.createKeyAgreement(ECDHAlgoName); + expect(globalECDHData != null).assertTrue(); + console.log("ECDH algoname is: ", globalECDHData.algName); + globalECDHData.generateSecret(globalPriKey, globalPubKey, (err1, result) => { + if (err1) { + expect(err1 == null).assertFail(); + } + console.warn("result data is " + uInt8ArrayToShowStr(result.data)); + expect(result != null).assertTrue(); + }); + }); +} + +async function testAsyKeyConvertKeyCallback(asyAlgoName) { + var globalPubKey; + var globalPriKey; + + let rsaGenerator = cryptoFramework.createAsyKeyGenerator(asyAlgoName); + console.warn("[Callback]: algName = " + rsaGenerator.algName); + expect(rsaGenerator != null).assertTrue(); + rsaGenerator.generateKeyPair((err, rsaKeyPair)=> { + if (err) { + expect(err == null).assertFail(); + } + globalPubKey = rsaKeyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Callback]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Callback]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Callback]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = rsaKeyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Callback]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Callback]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Callback]: priKey.getFormat= " + globalPriKey.format); + rsaGenerator.convertKey(encodedPubKey, encodedPriKey, (err1, convertKeyPair)=>{ + if (err1) { + expect(err1 == null).assertFail(); + } + expect(convertKeyPair != null).assertTrue(); + let encodedConvertPubKey = convertKeyPair.pubKey.getEncoded(); + console.warn("[Callback]: pubKey.getAlgorithm= " + convertKeyPair.pubKey.algName); + console.warn("[Callback]: pubKey.getEncoded= " + encodedConvertPubKey.data); + console.warn("[Callback]: pubKey.getFormat= " + convertKeyPair.pubKey.format); + let encodedConvertPriKey = convertKeyPair.priKey.getEncoded(); + console.warn("[Callback]: priKey.getAlgorithm= " + convertKeyPair.priKey.algName); + console.warn("[Callback]: priKey.getEncoded= " + encodedConvertPriKey.data); + console.warn("[Callback]: priKey.getFormat= " + convertKeyPair.priKey.format); + }); + }); +} + +export { + testAsyKeyEncryptDecryptCallback, + testAsyKeySignVerifyCallback, + testAsyKeyECDHCallback, + testAsyKeyConvertKeyCallback, +}; \ No newline at end of file diff --git a/security/cryptoFramework/src/main/js/test/utils/asymmetric/publicAsymmetricPromise.js b/security/cryptoFramework/src/main/js/test/utils/asymmetric/publicAsymmetricPromise.js new file mode 100644 index 000000000..af6d019a6 --- /dev/null +++ b/security/cryptoFramework/src/main/js/test/utils/asymmetric/publicAsymmetricPromise.js @@ -0,0 +1,243 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { expect } from "@ohos/hypium"; +import cryptoFramework from "@ohos.security.cryptoFramework"; +import { stringTouInt8Array, uInt8ArrayToShowStr, uInt8ArrayToString } from "../common/publicDoString"; + +async function testAsyKeyEncryptDecryptPromise(asyAlgoName, cipherAlgoName) { + var globalPubKey; + var globalPriKey; + var globalCipherEncrypt; + var globalCipherDecrypt; + var globalCipherBlob; + var globalText = "This is a cipher test"; + var input = { data: stringTouInt8Array(globalText) }; + + let rsaGenerator = cryptoFramework.createAsyKeyGenerator(asyAlgoName); + console.warn("[Promise]: algName = " + rsaGenerator.algName); + expect(rsaGenerator != null).assertTrue(); + globalCipherEncrypt = cryptoFramework.createCipher(cipherAlgoName); + expect(globalCipherEncrypt != null).assertTrue(); + let keyGenPromise = rsaGenerator.generateKeyPair().then(rsaKeyPair => { + expect(keyGenPromise != null).assertTrue(); + globalPubKey = rsaKeyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Promise]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Promise]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Promise]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = rsaKeyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Promise]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Promise]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Promise]: priKey.getFormat= " + globalPriKey.format); + console.info("[Promise]:start init =====================[encrypt]"); + return globalCipherEncrypt.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, globalPubKey, null); + }).then(() => { + console.warn("plaintext hex: " + uInt8ArrayToShowStr(input.data)); + console.info("[Promise]:start doFinal =====================[encrypt]"); + return globalCipherEncrypt.doFinal(input); + }).then(dataBlob => { + expect(dataBlob != null).assertTrue(); + globalCipherBlob = dataBlob; + console.log("cipherOutput: " + uInt8ArrayToShowStr(dataBlob.data)); + globalCipherDecrypt = cryptoFramework.createCipher(cipherAlgoName); + expect(globalCipherDecrypt != null).assertTrue(); + console.log("[Promise]:start init =====================[decrypt]"); + return globalCipherDecrypt.init(cryptoFramework.CryptoMode.DECRYPT_MODE, globalPriKey, null); + }).then(() => { + console.log("[Promise]:start doFinal =====================[decrypt]"); + let promiseFinal = globalCipherDecrypt.doFinal(globalCipherBlob); + return promiseFinal; + }).then(finalOutput => { + expect(finalOutput != null).assertTrue(); + if (finalOutput == null) { + console.warn("decrypt doFinal out is null"); + } else { + console.warn("decrypt doFinal output hex:" + uInt8ArrayToShowStr(finalOutput.data)); + console.warn("decrypt doFinal output :" + uInt8ArrayToString(finalOutput.data)); + } + let decryptData = uInt8ArrayToString(finalOutput.data); + expect(decryptData == globalText).assertTrue(); + }).catch(err => { + console.error("err: " + err.code); + expect(err == null).assertFail(); + }) +} + +async function testAsyKeySignVerifyPromise(asyAlgoName, signAlgoName) { + var globalPubKey; + var globalPriKey; + var globalSigner; + var globalVerify; + var globalSignBlob; + var globalText1 = "This is a sign test1"; + var input1 = { data: stringTouInt8Array(globalText1) }; + let rsaGenerator = cryptoFramework.createAsyKeyGenerator(asyAlgoName); + console.warn("[Promise]: algName = " + rsaGenerator.algName); + expect(rsaGenerator != null).assertTrue(); + rsaGenerator.generateKeyPair().then(rsaKeyPair => { + expect(rsaKeyPair != null).assertTrue(); + globalPubKey = rsaKeyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Promise]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Promise]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Promise]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = rsaKeyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Promise]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Promise]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Promise]: priKey.getFormat= " + globalPriKey.format); + globalSigner = cryptoFramework.createSign(signAlgoName); + expect(globalSigner != null).assertTrue(); + console.log("sign algoname is: ", globalSigner.algName); + return globalSigner.init(globalPriKey); + }).then(() => { + console.warn("plaintext1 hex: " + uInt8ArrayToShowStr(input1.data)); + return globalSigner.update(input1); + }).then(() => { + return globalSigner.sign(input1); + }).then(promiseSign => { + console.log("promiseSign success: ", promiseSign); + globalSignBlob = promiseSign; + }).then(() => { + globalVerify = cryptoFramework.createVerify(signAlgoName); + expect(globalVerify != null).assertTrue(); + console.log("verify algname is: ", globalVerify.algName); + return globalVerify.init(globalPubKey); + }).then(() => { + console.warn("plaintext1 hex: " + uInt8ArrayToShowStr(input1.data)); + return globalVerify.update(input1); + }).then(() => { + console.log("111111111"); + return globalVerify.verify(input1, globalSignBlob); + }).then((status) => { + console.log("22222222222"); + expect(status).assertTrue(); + }).catch(err => { + console.error("err: " + err.code); + expect(err == null).assertFail(); + }) +} + +async function testAsyKeyECDHPromise(ECDHAlgoName) { + var globalPubKey; + var globalPriKey; + var globalECDHData; + + try { + let rsaGenerator = cryptoFramework.createAsyKeyGenerator(ECDHAlgoName); + console.warn("[Promise]: algName = " + rsaGenerator.algName); + expect(rsaGenerator != null).assertTrue(); + rsaGenerator.generateKeyPair().then(rsaKeyPair => { + expect(rsaKeyPair != null).assertTrue(); + globalPubKey = rsaKeyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Promise]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Promise]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Promise]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = rsaKeyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Promise]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Promise]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Promise]: priKey.getFormat= " + globalPriKey.format); + globalECDHData = cryptoFramework.createKeyAgreement(ECDHAlgoName); + expect(globalECDHData != null).assertTrue(); + console.warn("ECDH algoname is: " + globalECDHData.algName); + let result = globalECDHData.generateSecret(globalPriKey, globalPubKey); + return result; + }).then(secretResult => { + console.warn("result data is " + uInt8ArrayToShowStr(secretResult.data)); + expect(secretResult != null).assertTrue(); + }) + } catch (err) { + console.error("catch err: " + err.code); + expect(err == null).assertFail(); + } +} + +async function testAsyKeyConvertKeyPromise(asyAlgoName) { + var globalPubKey; + var globalPriKey; + + try { + var globalRsaGenerator = cryptoFramework.createAsyKeyGenerator(asyAlgoName); + console.warn("[Promise]: algName = " + globalRsaGenerator.algName); + expect(globalRsaGenerator != null).assertTrue(); + globalRsaGenerator.generateKeyPair().then(rsaKeyPair => { + expect(rsaKeyPair != null).assertTrue(); + globalPubKey = rsaKeyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Promise]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Promise]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Promise]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = rsaKeyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Promise]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Promise]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Promise]: priKey.getFormat= " + globalPriKey.format); + return globalRsaGenerator.convertKey(encodedPubKey, encodedPriKey); + }).then(convertKeyPair => { + expect(convertKeyPair != null).assertTrue(); + let encodedConvertPubKey = convertKeyPair.pubKey.getEncoded(); + console.warn("[Promise]: pubKey.getAlgorithm= " + convertKeyPair.pubKey.algName); + console.warn("[Promise]: pubKey.getEncoded= " + encodedConvertPubKey.data); + console.warn("[Promise]: pubKey.getFormat= " + convertKeyPair.pubKey.format); + let encodedConvertPriKey = convertKeyPair.priKey.getEncoded(); + console.warn("[Promise]: priKey.getAlgorithm= " + convertKeyPair.priKey.algName); + console.warn("[Promise]: priKey.getEncoded= " + encodedConvertPriKey.data); + console.warn("[Promise]: priKey.getFormat= " + convertKeyPair.priKey.format); + }) + } catch (err) { + console.error("err: " + err.code); + expect(err == null).assertFail(); + } +} + +async function testAsyKeyPriKeyClearPromise(asyAlgoName) { + var globalPubKey; + var globalPriKey; + + var globalRsaGenerator = cryptoFramework.createAsyKeyGenerator(asyAlgoName); + console.warn("[Promise]: algName = " + globalRsaGenerator.algName); + expect(globalRsaGenerator != null).assertTrue(); + globalRsaGenerator.generateKeyPair().then(rsaKeyPair => { + expect(rsaKeyPair != null).assertTrue(); + globalPubKey = rsaKeyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Promise]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Promise]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Promise]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = rsaKeyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Promise]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Promise]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Promise]: priKey.getFormat= " + globalPriKey.format); + let result = globalPriKey.clearMem(); + console.log("result is: " + result); + expect(result == undefined).assertTrue(); + }).catch(err => { + console.error("err: " + err.code); + expect(err == null).assertFail(); + }) +} + +export { + testAsyKeyEncryptDecryptPromise, + testAsyKeySignVerifyPromise, + testAsyKeyECDHPromise, + testAsyKeyConvertKeyPromise, + testAsyKeyPriKeyClearPromise, +}; \ No newline at end of file -- GitLab