提交 58721f07 编写于 作者: Q Qwink2016

修改加解密算法库XTS测试套摘要算法部分的逻辑

Signed-off-by: NQwink2016 <huangkun45@huawei.com>
Change-Id: Ic5b335777ae5704dda6cdf47933475a4c85f33d2
上级 67fed82b
/*
* 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 {
testHMACDigestPromise,
testMDDigestPromise,
testHMACDigestPromiseDatablobLong,
} from "./utils/digestalgorithm/publicDigestPromise";
import {
testHMACDigestCallback,
testMDDigestCallback,
testMDErrorAlgorithm,
testMDErrorAlgorithmNull,
testMDDigestCallbackLen,
} from "./utils/digestalgorithm/publicDigestCallback";
import {
stringTouInt8Array,
uInt8ArrayToShowStr,
uInt8ArrayToString,
} from "./utils/common/publicDoString";
import cryptoFramework from "@ohos.security.cryptoFramework";
export default function DigestAlgorithmJsunit() {
describe("DigestAlgorithmJsunit", function () {
console.info("##########start DigestAlgorithmJsunit##########");
beforeAll(function () {
});
afterEach(function () {
});
/**
* @tc.number Security_crypto_framework_MD_0100
* @tc.name Test MD5 for MD digest algotithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_MD_0100", 0, async function (done) {
await testMDDigestPromise("MD5")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0200
* @tc.name Test SHA1 for MD digest algotithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_MD_0200", 0, async function (done) {
await testMDDigestPromise("SHA1")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0300
* @tc.name Test SHA224 for MD digest algotithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_MD_0300", 0, async function (done) {
await testMDDigestPromise("SHA224")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0400
* @tc.name Test SHA256 for MD digest algotithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0400", 0, async function (done) {
await testMDDigestCallback("SHA256")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0500
*
* @tc.name Test SHA384 for MD digest algotithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0500", 0, async function (done) {
await testMDDigestCallback("SHA384")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0600
* @tc.name Test SHA512 for MD digest algotithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0600", 0, async function (done) {
await testMDDigestCallback("SHA512")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0700
* @tc.name The encryption and decryption framework supports MD calculation, and the algorithm parameters are abnormal
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0700", 0, async function (done) {
await testMDErrorAlgorithm("SHA5")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0800
* @tc.name The encryption and decryption framework supports MD calculation, and the algorithm parameter is NULL
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0800", 0, async function (done) {
await testMDErrorAlgorithmNull(null)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0900
* @tc.name The encryption and decryption framework supports MD calculation, and the Datablob value to be calculated is a super large value
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0900", 0, async function (done) {
await testMDDigestCallbackLen("SHA224", 10000)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_1000
* @tc.name The encryption and decryption framework supports MD calculation, and the Datablob value to be calculated is abnormal
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_1000", 0, async function (done) {
let mdGenerator = cryptoFramework.createMd("SHA224");
try {
await new Promise((resolve, reject) => {
mdGenerator.update(null, (err,) => {
if (err) {
reject(err);
} else {
resolve();
}
})
})
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
try {
await mdGenerator.update(0);
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
try {
await mdGenerator.digest();
} catch (err) {
console.error("err is " + err.code);
expect(err.code).assertEqual(401)
}
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0100
* @tc.name Test SHA1 for HMAC digest algorithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_HMAC_0100", 0, async function (done) {
await testHMACDigestCallback("SHA1", "AES128")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0200
* @tc.name Test SHA224 for HMAC digest algorithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0200", 0, async function (done) {
await testHMACDigestPromise("SHA224", "AES192")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0300
* @tc.name Test SHA256 for HMAC digest algorithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_HMAC_0300", 0, async function (done) {
await testHMACDigestCallback("SHA256", "AES256")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0400
* @tc.name Test SHA384 for HMAC digest algorithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0400", 0, async function (done) {
await testHMACDigestPromise("SHA384", "3DES192")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0500
* @tc.name Test SHA512 for HMAC digest algorithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0500", 0, async function (done) {
await testHMACDigestPromise("SHA512", "3DES192")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0600
* @tc.name The encryption and decryption framework supports HMAC calculation, and HMAC algorithm parameters are abnormal
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0600", 0, async function (done) {
try {
cryptoFramework.createMac("SHA5");
expect(null).assertFail();
} catch (err) {
console.error("[Promise]: error code: " + err.code + ", message is: " + err.message);
expect(err.code).assertEqual(801);
}
try {
cryptoFramework.createMac(null);
expect(null).assertFail();
} catch (err) {
console.error("[Promise]: error code: " + err.code + ", message is: " + err.message);
expect(err.code).assertEqual(401);
}
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0700
* @tc.name The encryption and decryption framework supports HMAC calculation, and the key algorithm parameters are abnormal
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0700", 0, async function (done) {
let globalHMAC = cryptoFramework.createMac("SHA512");
let globalsymKeyGenerator = cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_2");
let key = globalsymKeyGenerator.generateKeyPair();
try {
await globalHMAC.init(key);
expect(null).assertFail();
} catch (err) {
console.error("[Promise]init(key): error code: " + err.code + ", message is: " + err.message);
expect(err.code).assertEqual(401);
};
try {
await globalHMAC.init(null);
expect(null).assertFail();
} catch (err) {
console.error("[Promise]init(null): error code: " + err.code + ", message is: " + err.message);
expect(err.code).assertEqual(401);
};
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0800
* @tc.name The encryption and decryption framework supports HMAC calculation, and the Datablob of HMAC to be calculated is abnormal
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0800", 0, async function (done) {
let globalHMAC = cryptoFramework.createMac("SHA512");
let globalsymKeyGenerator = cryptoFramework.createSymKeyGenerator("3DES192");
let key = await globalsymKeyGenerator.generateSymKey();
await globalHMAC.init(key);
try {
await globalHMAC.update(null);
expect(null).assertFail();
} catch (err) {
console.error("[Promise]init(null): error code: " + err.code + ", message is: " + err.message);
expect(err.code).assertEqual(401);
}
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0900
* @tc.name The encryption and decryption framework supports HMAC calculation, and the Datablob of HMAC to be calculated is very large
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0900", 0, async function (done) {
await testHMACDigestPromiseDatablobLong("SHA512", "3DES192", 10000)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
});
}
/*
* 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 {
testHMACDigestPromise,
testMDDigestPromise,
} from "./utils/digestalgorithm/publicDigestPromise";
import {
testHMACDigestCallback,
testMDDigestCallback,
testMDDigestCallbackLen,
} from "./utils/digestalgorithm/publicDigestCallback";
import cryptoFramework from "@ohos.security.cryptoFramework";
import { stringTouInt8Array } from "./utils/common/publicDoString";
export default function DigestAlgorithmJsunit() {
describe("DigestAlgorithmJsunit", function () {
console.info("##########start DigestAlgorithmJsunit##########");
beforeAll(function () {});
afterEach(function () {});
/**
* @tc.number Security_crypto_framework_MD_0100
* @tc.name Test MD5 for MD digest algotithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_MD_0100", 0, async function (done) {
await testMDDigestPromise("MD5")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0200
* @tc.name Test SHA1 for MD digest algotithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_MD_0200", 0, async function (done) {
await testMDDigestPromise("SHA1")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0300
* @tc.name Test SHA224 for MD digest algotithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_MD_0300", 0, async function (done) {
await testMDDigestPromise("SHA224")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0400
* @tc.name Test SHA256 for MD digest algotithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0400", 0, async function (done) {
await testMDDigestCallback("SHA256")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0500
*
* @tc.name Test SHA384 for MD digest algotithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0500", 0, async function (done) {
await testMDDigestCallback("SHA384")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0600
* @tc.name Test SHA512 for MD digest algotithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0600", 0, async function (done) {
await testMDDigestCallback("SHA512")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_0700
* @tc.name The encryption and decryption framework supports MD calculation, and the algorithm parameters are abnormal
* @tc.desc Input SHA5
*/
it("Security_crypto_framework_MD_0700", 0, async function (done) {
try {
cryptoFramework.createMd("SHA5");
expect(null).assertFail();
} catch (err) {
expect(err.code == 801).assertTrue();
}
done();
});
/**
* @tc.number Security_crypto_framework_MD_0800
* @tc.name The encryption and decryption framework supports MD calculation, and the algorithm parameter is NULL
* @tc.desc Input null
*/
it("Security_crypto_framework_MD_0800", 0, async function (done) {
try {
cryptoFramework.createMd(null);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
});
/**
* @tc.number Security_crypto_framework_MD_0900
* @tc.name The encryption and decryption framework supports MD calculation, and the Datablob value to be calculated is a super large value
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_0900", 0, async function (done) {
await testMDDigestCallbackLen("SHA224", 10000)
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_MD_1000
* @tc.name The encryption and decryption framework supports MD calculation, and the Datablob value to be calculated is abnormal
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_MD_1000", 0, async function (done) {
let mdGenerator = cryptoFramework.createMd("SHA224");
try {
await new Promise((resolve, reject) => {
mdGenerator.update(null, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
try {
await mdGenerator.update(0);
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
try {
await mdGenerator.digest();
} catch (err) {
console.error("err is " + err.code);
expect(err.code).assertEqual(401);
}
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0100
* @tc.name Test SHA1 for HMAC digest algorithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_HMAC_0100", 0, async function (done) {
await testHMACDigestCallback("SHA1", "AES128")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0200
* @tc.name Test SHA224 for HMAC digest algorithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0200", 0, async function (done) {
await testHMACDigestPromise("SHA224", "AES192")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0300
* @tc.name Test SHA256 for HMAC digest algorithm
* @tc.desc Use the Callback Style of Interface
*/
it("Security_crypto_framework_HMAC_0300", 0, async function (done) {
await testHMACDigestCallback("SHA256", "AES256")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0400
* @tc.name Test SHA384 for HMAC digest algorithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0400", 0, async function (done) {
await testHMACDigestPromise("SHA384", "3DES192")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0500
* @tc.name Test SHA512 for HMAC digest algorithm
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0500", 0, async function (done) {
await testHMACDigestPromise("SHA512", "3DES192")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
expect(null).assertFail();
});
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0600
* @tc.name The encryption and decryption framework supports HMAC calculation, and HMAC algorithm parameters are abnormal
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0600", 0, async function (done) {
try {
cryptoFramework.createMac("SHA5");
expect(null).assertFail();
} catch (err) {
console.error(
"[Promise]: error code: " + err.code + ", message is: " + err.message
);
expect(err.code).assertEqual(801);
}
try {
cryptoFramework.createMac(null);
expect(null).assertFail();
} catch (err) {
console.error(
"[Promise]: error code: " + err.code + ", message is: " + err.message
);
expect(err.code).assertEqual(401);
}
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0700
* @tc.name The encryption and decryption framework supports HMAC calculation, and the key algorithm parameters are abnormal
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0700", 0, async function (done) {
let globalHMAC = cryptoFramework.createMac("SHA512");
let globalsymKeyGenerator =
cryptoFramework.createAsyKeyGenerator("RSA1024|PRIMES_2");
let key = globalsymKeyGenerator.generateKeyPair();
try {
await globalHMAC.init(key);
expect(null).assertFail();
} catch (err) {
console.error(
"[Promise]init(key): error code: " +
err.code +
", message is: " +
err.message
);
expect(err.code).assertEqual(401);
}
try {
await globalHMAC.init(null);
expect(null).assertFail();
} catch (err) {
console.error(
"[Promise]init(null): error code: " +
err.code +
", message is: " +
err.message
);
expect(err.code).assertEqual(401);
}
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0800
* @tc.name The encryption and decryption framework supports HMAC calculation, and the Datablob of HMAC to be calculated is abnormal
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0800", 0, async function (done) {
let globalHMAC = cryptoFramework.createMac("SHA512");
let globalsymKeyGenerator =
cryptoFramework.createSymKeyGenerator("3DES192");
let key = await globalsymKeyGenerator.generateSymKey();
await globalHMAC.init(key);
try {
await globalHMAC.update(null);
expect(null).assertFail();
} catch (err) {
console.error(
"[Promise]init(null): error code: " +
err.code +
", message is: " +
err.message
);
expect(err.code).assertEqual(401);
}
done();
});
/**
* @tc.number Security_crypto_framework_HMAC_0900
* @tc.name The encryption and decryption framework supports HMAC calculation, and the Datablob of HMAC to be calculated is very large
* @tc.desc Use the Promise Style of Interface
*/
it("Security_crypto_framework_HMAC_0900", 0, async function (done) {
let globalText = "";
let t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < 10000; i++) {
globalText += t.charAt(Math.floor(Math.random() * t.length));
}
console.log("Datablob = " + globalText);
let inBlob = {
data: stringTouInt8Array(globalText),
};
try {
let macObj = cryptoFramework.createMac("SHA512");
let symKeyGenerator = cryptoFramework.createSymKeyGenerator("3DES192");
let symKey = await symKeyGenerator.generateSymKey();
await macObj.init(symKey);
await macObj.update(inBlob);
await macObj.doFinal();
} catch (err) {
expect(null).assertFail();
}
done();
});
});
}
/*
* 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 generateSymKey(symKeyGenerator) {
return new Promise((resolve, reject) => {
symKeyGenerator.generateSymKey((err, symKey) => {
if (err) {
console.error("[Callback]generateSymKey failed. error is " + err);
reject(err);
} else {
console.log("[Callback]generateSymKey success. symKey is " + symKey);
console.warn("[Callback]key algName:" + symKey.algName);
console.warn("[Callback]key format:" + symKey.format);
var encodeKey = symKey.getEncoded();
console.warn(
"[Callback]key getEncoded hex: " + uInt8ArrayToShowStr(encodeKey.data)
);
resolve(symKey);
}
});
});
}
async function initMac(macGenerator, symKey) {
return new Promise((resolve, reject) => {
macGenerator.init(symKey, (err) => {
if (err) {
console.error("[Callback]macGenerator init failed. error is " + err);
reject(err);
} else {
console.log("[Callback]macGenerator init success!");
resolve("init success");
}
});
});
}
async function updateMac(macGenerator, dataBlob) {
return new Promise((resolve, reject) => {
macGenerator.update(dataBlob, (err) => {
if (err) {
console.error("[Callback]macGenerator update failed. error is " + err);
reject(err);
} else {
console.log("[Callback]macGenerator update success!");
resolve("update success");
}
});
});
}
async function doFinalMac(macGenerator) {
return new Promise((resolve, reject) => {
macGenerator.doFinal((err, output) => {
if (err) {
console.error("[Callback]macGenerator doFinal failed. error is " + err);
reject(err);
} else {
console.log(
"[Callback]macGenerator doFinal success! output is: " + output
);
resolve(output);
}
});
});
}
async function updateMd(mdGenerator, dataBlob) {
return new Promise((resolve, reject) => {
mdGenerator.update(dataBlob, (err) => {
if (err) {
console.error("[Callback]mdGenerator update failed. error is " + err);
reject(err);
} else {
console.log("[Callback]mdGenerator update success!");
resolve("update success");
}
});
});
}
async function digestMd(mdGenerator) {
return new Promise((resolve, reject) => {
mdGenerator.digest((err, output) => {
if (err) {
console.error("[Callback]mdGenerator digest failed. error is " + err);
reject(err);
} else {
console.log(
"[Callback]mdGenerator digest success! output is: " + output
);
resolve(output);
}
});
});
}
async function testMDDigestCallback(MDAlgoName) {
var globalMd;
var globalText = "my test data";
var inBlob = {
data: stringTouInt8Array(globalText),
};
return new Promise((resolve, reject) => {
globalMd = cryptoFramework.createMd(MDAlgoName);
expect(globalMd != null).assertTrue();
console.warn("md= " + globalMd);
console.warn("MD algName is: " + globalMd.algName);
updateMd(globalMd, inBlob)
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return digestMd(globalMd);
})
.then((digestBlob) => {
console.warn(
"[callback]: digest result: " + uInt8ArrayToShowStr(digestBlob.data)
);
let mdLen = globalMd.getMdLength();
console.warn("Md len: " + mdLen);
expect(digestBlob != null && mdLen != 0 && mdLen != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("testMDDigestCallback catch error: " + err);
reject(err);
});
});
}
async function testMDDigestCallbackLen(MDAlgoName, DatablobLen) {
var globalMd;
var i;
var globalText;
var t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz", n = t.length, s = "";
for (i = 0; i < DatablobLen; i++) {
globalText += t.charAt(Math.floor(Math.random() * n));
}
console.warn("Datablob = " + globalText);
var ginBlob = {
data: stringTouInt8Array(globalText),
};
return new Promise((resolve, reject) => {
globalMd = cryptoFramework.createMd(MDAlgoName);
expect(globalMd != null).assertTrue();
console.warn("md= " + globalMd);
console.warn("MD algName is: " + globalMd.algName);
updateMd(globalMd, ginBlob)
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return digestMd(globalMd);
})
.then((digestBlob) => {
console.warn(
"[callback]: digest result: " + uInt8ArrayToShowStr(digestBlob.data)
);
let mdLen = globalMd.getMdLength();
console.warn("Md len: " + mdLen);
expect(digestBlob != null && mdLen != 0 && mdLen != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("testMDDigestCallback catch error: " + err);
reject(err);
});
});
}
async function testMDErrorAlgorithm(MDAlgoName) {
var globalMd;
return new Promise((resolve, reject) => {
console.warn("md= test begin");
try {
globalMd = cryptoFramework.createMd(MDAlgoName);
reject();
} catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
expect(error.code == 801).assertTrue();
resolve();
}
});
}
async function testMDErrorAlgorithmNull(MDAlgoName) {
var globalMd;
return new Promise((resolve, reject) => {
try {
globalMd = cryptoFramework.createMd(MDAlgoName);
reject();
} catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
expect(error.code == 401).assertTrue();
resolve();
}
});
}
async function testHMACDigestCallback(HMACAlgoName, keyAlgoName) {
var globalHMAC;
var globalText = "my test data";
var globalsymKeyGenerator;
var ginBlob = {
data: stringTouInt8Array(globalText),
};
return new Promise((resolve, reject) => {
globalHMAC = cryptoFramework.createMac(HMACAlgoName);
expect(globalHMAC != null).assertTrue();
console.warn("mac= " + globalHMAC);
console.warn("HMAC algName is: " + globalHMAC.algName);
console.log("start to call createSymKeyGenerator()");
globalsymKeyGenerator = cryptoFramework.createSymKeyGenerator(keyAlgoName);
expect(globalsymKeyGenerator != null).assertTrue();
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
generateSymKey(globalsymKeyGenerator)
.then((symKey) => {
expect(symKey != null).assertTrue();
return initMac(globalHMAC, symKey);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return updateMac(globalHMAC, ginBlob);
})
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return doFinalMac(globalHMAC);
})
.then((macOutput) => {
console.warn("HMAC result:" + macOutput.data);
let macLen = globalHMAC.getMacLength();
console.warn("MAC len:" + macLen);
expect(macOutput != null && macLen != 0 && macLen != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("testHMACDigestCallback catch error: " + err);
reject(err);
});
});
}
export {
testMDDigestCallback,
testHMACDigestCallback,
testMDErrorAlgorithm,
testMDErrorAlgorithmNull,
testMDDigestCallbackLen
};
/*
* 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 generateSymKey(symKeyGenerator) {
return new Promise((resolve, reject) => {
symKeyGenerator.generateSymKey((err, symKey) => {
if (err) {
console.error("[Callback]generateSymKey failed. error is " + err);
reject(err);
} else {
console.log("[Callback]generateSymKey success. symKey is " + symKey);
console.warn("[Callback]key algName:" + symKey.algName);
console.warn("[Callback]key format:" + symKey.format);
var encodeKey = symKey.getEncoded();
console.warn(
"[Callback]key getEncoded hex: " + uInt8ArrayToShowStr(encodeKey.data)
);
resolve(symKey);
}
});
});
}
async function initMac(macGenerator, symKey) {
return new Promise((resolve, reject) => {
macGenerator.init(symKey, (err) => {
if (err) {
console.error("[Callback]macGenerator init failed. error is " + err);
reject(err);
} else {
console.log("[Callback]macGenerator init success!");
resolve("init success");
}
});
});
}
async function updateMac(macGenerator, dataBlob) {
return new Promise((resolve, reject) => {
macGenerator.update(dataBlob, (err) => {
if (err) {
console.error("[Callback]macGenerator update failed. error is " + err);
reject(err);
} else {
console.log("[Callback]macGenerator update success!");
resolve("update success");
}
});
});
}
async function doFinalMac(macGenerator) {
return new Promise((resolve, reject) => {
macGenerator.doFinal((err, output) => {
if (err) {
console.error("[Callback]macGenerator doFinal failed. error is " + err);
reject(err);
} else {
console.log(
"[Callback]macGenerator doFinal success! output is: " + output
);
resolve(output);
}
});
});
}
async function updateMd(mdGenerator, dataBlob) {
return new Promise((resolve, reject) => {
mdGenerator.update(dataBlob, (err) => {
if (err) {
console.error("[Callback]mdGenerator update failed. error is " + err);
reject(err);
} else {
console.log("[Callback]mdGenerator update success!");
resolve("update success");
}
});
});
}
async function digestMd(mdGenerator) {
return new Promise((resolve, reject) => {
mdGenerator.digest((err, output) => {
if (err) {
console.error("[Callback]mdGenerator digest failed. error is " + err);
reject(err);
} else {
console.log(
"[Callback]mdGenerator digest success! output is: " + output
);
resolve(output);
}
});
});
}
async function testMDDigestCallback(MDAlgoName) {
var globalMd;
var globalText = "my test data";
var inBlob = {
data: stringTouInt8Array(globalText),
};
return new Promise((resolve, reject) => {
globalMd = cryptoFramework.createMd(MDAlgoName);
expect(globalMd != null).assertTrue();
console.warn("md= " + globalMd);
console.warn("MD algName is: " + globalMd.algName);
updateMd(globalMd, inBlob)
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return digestMd(globalMd);
})
.then((digestBlob) => {
console.warn(
"[callback]: digest result: " + uInt8ArrayToShowStr(digestBlob.data)
);
let mdLen = globalMd.getMdLength();
console.warn("Md len: " + mdLen);
expect(digestBlob != null && mdLen != 0 && mdLen != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("testMDDigestCallback catch error: " + err);
reject(err);
});
});
}
async function testMDDigestCallbackLen(MDAlgoName, DatablobLen) {
let globalText = "";
let t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < DatablobLen; i++) {
globalText += t.charAt(Math.floor(Math.random() * t.length));
}
console.warn("Datablob = " + globalText);
var ginBlob = {
data: stringTouInt8Array(globalText),
};
try {
let mdObj = cryptoFramework.createMd(MDAlgoName);
expect(mdObj != null).assertTrue();
let updateData = await updateMd(mdObj, ginBlob);
expect(updateData === "update success").assertTrue();
let digestBlob = await digestMd(mdObj);
console.warn(
"[callback]: digest result: " + uInt8ArrayToShowStr(digestBlob.data)
);
let mdLen = mdObj.getMdLength();
console.log("mdLen is: " + mdLen);
expect(digestBlob != null && mdLen != 0 && mdLen != null).assertTrue();
} catch (err) {
console.error("testMDDigestCallback catch error: " + err);
throw err;
}
}
async function testHMACDigestCallback(HMACAlgoName, keyAlgoName) {
var globalHMAC;
var globalText = "my test data";
var globalsymKeyGenerator;
var ginBlob = {
data: stringTouInt8Array(globalText),
};
return new Promise((resolve, reject) => {
globalHMAC = cryptoFramework.createMac(HMACAlgoName);
expect(globalHMAC != null).assertTrue();
console.warn("mac= " + globalHMAC);
console.warn("HMAC algName is: " + globalHMAC.algName);
console.log("start to call createSymKeyGenerator()");
globalsymKeyGenerator = cryptoFramework.createSymKeyGenerator(keyAlgoName);
expect(globalsymKeyGenerator != null).assertTrue();
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
generateSymKey(globalsymKeyGenerator)
.then((symKey) => {
expect(symKey != null).assertTrue();
return initMac(globalHMAC, symKey);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return updateMac(globalHMAC, ginBlob);
})
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return doFinalMac(globalHMAC);
})
.then((macOutput) => {
console.warn("HMAC result:" + macOutput.data);
let macLen = globalHMAC.getMacLength();
console.warn("MAC len:" + macLen);
expect(macOutput != null && macLen != 0 && macLen != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("testHMACDigestCallback catch error: " + err);
reject(err);
});
});
}
export {
testMDDigestCallback,
testHMACDigestCallback,
testMDDigestCallbackLen,
};
/*
* 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 testMDDigestPromise(MDAlgoName) {
var globalMd;
var globalText = "my test data";
return new Promise((resolve, reject) => {
globalMd = cryptoFramework.createMd(MDAlgoName);
expect(globalMd != null).assertTrue();
console.warn("md= " + globalMd);
console.warn("MD algName is: " + globalMd.algName);
let inBlob = {
data: stringTouInt8Array(globalText),
};
globalMd
.update(inBlob)
.then(() => {
console.warn("[Promise]: update finished");
let digestBlob = globalMd.digest();
return digestBlob;
})
.then((mdOutput) => {
console.warn(
"[Promise]: digest result1: " + uInt8ArrayToShowStr(mdOutput.data)
);
console.warn("[Promise]: digest result2: " + mdOutput.data);
let mdLen = globalMd.getMdLength();
console.warn("Md len: " + mdLen);
expect(mdOutput != null && mdLen != 0 && mdLen != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("[promise]catch err:" + err);
reject(err);
});
});
}
async function testHMACDigestPromise(HMACAlgoName, keyAlgoName) {
var globalHMAC;
var globalText = "my test data";
var globalsymKeyGenerator;
var inBlob = {
data: stringTouInt8Array(globalText),
};
return new Promise((resolve, reject) => {
globalHMAC = cryptoFramework.createMac(HMACAlgoName);
expect(globalHMAC != null).assertTrue();
console.warn("mac= " + globalHMAC);
console.warn("HMAC algName is: " + globalHMAC.algName);
console.log("start to call createSymKeyGenerator()");
globalsymKeyGenerator = cryptoFramework.createSymKeyGenerator(keyAlgoName);
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
expect(globalsymKeyGenerator != null).assertTrue();
console.log("createSymKeyGenerator ok");
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
globalsymKeyGenerator
.generateSymKey()
.then((key) => {
expect(key != null).assertTrue();
console.warn("generateSymKey ok");
console.warn("key algName:" + key.algName);
console.warn("key format:" + key.format);
var encodedKey = key.getEncoded();
console.warn(
"key getEncoded hex: " + uInt8ArrayToShowStr(encodedKey.data)
);
var promiseMacInit = globalHMAC.init(key);
return promiseMacInit;
})
.then(() => {
var promiseMacUpdate = globalHMAC.update(inBlob);
return promiseMacUpdate;
})
.then(() => {
var promiseMacdoFinal = globalHMAC.doFinal();
return promiseMacdoFinal;
})
.then((macOutput) => {
console.warn("HMAC result:" + macOutput.data);
let macLen = globalHMAC.getMacLength();
console.warn("MAC len:" + macLen);
expect(macOutput != null && macLen != 0 && macLen != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("[promise]catch err:" + err);
reject(err);
});
});
}
async function testHMACDigestPromiseDatablobLong(HMACAlgoName, keyAlgoName, DatablobLen) {
var globalHMAC;
var globalsymKeyGenerator;
var i;
var globalText;
var t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz",n = t.length,s="";
for (i = 0; i < DatablobLen; i++){
globalText += t.charAt(Math.floor(Math.random() * n));
}
console.warn("Datablob = " + globalText);
var inBlob = {
data: stringTouInt8Array(globalText),
};
return new Promise((resolve, reject) => {
globalHMAC = cryptoFramework.createMac(HMACAlgoName);
resolve();
expect(globalHMAC != null).assertTrue();
console.warn("mac= " + globalHMAC);
console.warn("HMAC algName is: " + globalHMAC.algName);
console.log("start to call createSymKeyGenerator()");
globalsymKeyGenerator = cryptoFramework.createSymKeyGenerator(keyAlgoName);
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
expect(globalsymKeyGenerator != null).assertTrue();
console.log("createSymKeyGenerator ok");
globalsymKeyGenerator
.generateSymKey()
.then((key) => {
expect(key != null).assertTrue();
console.warn("generateSymKey ok");
console.warn("key algName:" + key.algName);
console.warn("key format:" + key.format);
var encodedKey = key.getEncoded();
console.warn(
"key getEncoded hex: " + uInt8ArrayToShowStr(encodedKey.data)
);
var promiseMacInit = globalHMAC.init(key);
return promiseMacInit;
})
.then(() => {
try {
var promiseMacUpdate = globalHMAC.update(inBlob);
console.log("promiseMacUpdate = " + promiseMacUpdate);
} catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
}
})
.then(() => {
var promiseMacdoFinal = globalHMAC.doFinal();
console.log("promiseMacdoFinal = " + promiseMacdoFinal);
return promiseMacdoFinal;
})
.catch((err) => {
console.error("[promise]catch err:" + err);
reject(err);
});
});
}
export { testMDDigestPromise, testHMACDigestPromise, testHMACDigestPromiseDatablobLong};
/*
* 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 testMDDigestPromise(MDAlgoName) {
var globalMd;
var globalText = "my test data";
return new Promise((resolve, reject) => {
globalMd = cryptoFramework.createMd(MDAlgoName);
expect(globalMd != null).assertTrue();
console.warn("md= " + globalMd);
console.warn("MD algName is: " + globalMd.algName);
let inBlob = {
data: stringTouInt8Array(globalText),
};
globalMd
.update(inBlob)
.then(() => {
console.warn("[Promise]: update finished");
let digestBlob = globalMd.digest();
return digestBlob;
})
.then((mdOutput) => {
console.warn(
"[Promise]: digest result1: " + uInt8ArrayToShowStr(mdOutput.data)
);
console.warn("[Promise]: digest result2: " + mdOutput.data);
let mdLen = globalMd.getMdLength();
console.warn("Md len: " + mdLen);
expect(mdOutput != null && mdLen != 0 && mdLen != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("[promise]catch err:" + err);
reject(err);
});
});
}
async function testHMACDigestPromise(HMACAlgoName, keyAlgoName) {
var globalHMAC;
var globalText = "my test data";
var globalsymKeyGenerator;
var inBlob = {
data: stringTouInt8Array(globalText),
};
return new Promise((resolve, reject) => {
globalHMAC = cryptoFramework.createMac(HMACAlgoName);
expect(globalHMAC != null).assertTrue();
console.warn("mac= " + globalHMAC);
console.warn("HMAC algName is: " + globalHMAC.algName);
console.log("start to call createSymKeyGenerator()");
globalsymKeyGenerator = cryptoFramework.createSymKeyGenerator(keyAlgoName);
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
expect(globalsymKeyGenerator != null).assertTrue();
console.log("createSymKeyGenerator ok");
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
globalsymKeyGenerator
.generateSymKey()
.then((key) => {
expect(key != null).assertTrue();
console.warn("generateSymKey ok");
console.warn("key algName:" + key.algName);
console.warn("key format:" + key.format);
var encodedKey = key.getEncoded();
console.warn(
"key getEncoded hex: " + uInt8ArrayToShowStr(encodedKey.data)
);
var promiseMacInit = globalHMAC.init(key);
return promiseMacInit;
})
.then(() => {
var promiseMacUpdate = globalHMAC.update(inBlob);
return promiseMacUpdate;
})
.then(() => {
var promiseMacdoFinal = globalHMAC.doFinal();
return promiseMacdoFinal;
})
.then((macOutput) => {
console.warn("HMAC result:" + macOutput.data);
let macLen = globalHMAC.getMacLength();
console.warn("MAC len:" + macLen);
expect(macOutput != null && macLen != 0 && macLen != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("[promise]catch err:" + err);
reject(err);
});
});
}
export { testMDDigestPromise, testHMACDigestPromise };
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册