未验证 提交 aa4f886e 编写于 作者: 胡啸天 提交者: Gitee

modify hmac & md

Signed-off-by: N胡啸天 <huxiaotian2@huawei.com>
上级 dd8a364c
...@@ -1130,83 +1130,71 @@ function signLongMessagePromise() { ...@@ -1130,83 +1130,71 @@ function signLongMessagePromise() {
```javascript ```javascript
import cryptoFramework from "@ohos.security.cryptoFramework" import cryptoFramework from "@ohos.security.cryptoFramework"
// turn string into uint8Arr // 可理解的字符串转成字节流
function stringToUint8Array(str) { function stringToUint8Array(str) {
var arr = []; let arr = [];
for (var i = 0, j = str.length; i < j; ++i) { for (let i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i)); arr.push(str.charCodeAt(i));
}
var tmpUint8Array = new Uint8Array(arr);
return tmpUint8Array;
}
// generate dataBlob with given length
function GenDataBlob(dataBlobLen) {
var dataBlob;
if (dataBlobLen == 12) {
dataBlob = {data: stringToUint8Array("my test data")};
} else {
console.error("GenDataBlob: dataBlobLen is invalid");
dataBlob = {data: stringToUint8Array("my test data")};
} }
return dataBlob; let tmpArray = new Uint8Array(arr);
return tmpArray;
} }
// md with promise async // 以Promise方式完成摘要
function doMdByPromise(algName) { function doMdByPromise() {
var md; let mdAlgName = "SHA256"; // 摘要算法名
let message = "mdTestMessgae"; // 待摘要数据
let md;
let mdOutput;
try { try {
md = cryptoFramework.createMd(algName); md = cryptoFramework.createMd(mdAlgName);
} 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);
return;
} }
console.error("[Promise]: Md algName is: " + md.algName); console.info("[Promise]: Md algName is: " + md.algName);
// 初次update // 数据量较少时,可以只做一次update,将数据全部传入,接口未对入参长度做限制
var promiseMdUpdate = md.update(GenDataBlob(12)); let promiseMdUpdate = md.update({ data: stringToUint8Array(message) });
promiseMdUpdate.then(() => { promiseMdUpdate.then(() => {
// 可根据情况进行多次update // 通过digest,返回摘要结果
promiseMdUpdate = md.update(GenDataBlob(12)); let PromiseMdDigest = md.digest();
return promiseMdUpdate;
}).then(mdOutput => {
var PromiseMdDigest = md.digest();
return PromiseMdDigest; return PromiseMdDigest;
}).then(mdOutput => { }).then(digestOutput => {
console.error("[Promise]: MD result: " + mdOutput.data); mdOutput = digestOutput;
var mdLen = md.getMdLength(); console.info("[Promise]: MD result: " + mdOutput.data);
console.error("[Promise]: MD len: " + mdLen); let mdLen = md.getMdLength();
console.info("[Promise]: MD len: " + mdLen);
}).catch(error => { }).catch(error => {
console.error("[Promise]: error: " + error.message); console.error("[Promise]: error: " + error.message);
}); });
} }
// md with callback async // 以Callback方式完成摘要
function doMdByCallback(algName) { function doMdByCallback() {
var md; let mdAlgName = "SHA256"; // 摘要算法名
let message = "mdTestMessgae"; // 待摘要数据
let md;
let mdOutput;
try { try {
md = cryptoFramework.createMd(algName); md = cryptoFramework.createMd(mdAlgName);
} catch (error) { } catch (error) {
console.error("[Callback]: error code: " + error.code + ", message is: " + error.message); console.error("[Callback]: error code: " + error.code + ", message is: " + error.message);
} }
console.error("[Callback]: Md algName is: " + md.algName); console.info("[Callback]: Md algName is: " + md.algName);
// 初次update // 数据量较少时,可以只做一次update,将数据全部传入,接口未对入参长度做限制
md.update(GenDataBlob(12), (err,) => { md.update({ data: stringToUint8Array(message) }, (err,) => {
if (err) { if (err) {
console.error("[Callback]: err: " + err.code); console.error("[Callback]: err: " + err.code);
} }
// 可根据情况进行多次update md.digest((err1, digestOutput) => {
md.update(GenDataBlob(12), (err1,) => {
if (err1) { if (err1) {
console.error("[Callback]: err: " + err1.code); console.error("[Callback]: err: " + err1.code);
} else {
mdOutput = digestOutput;
console.info("[Callback]: MD result: " + mdOutput.data);
let mdLen = md.getMdLength();
console.info("[Callback]: MD len: " + mdLen);
} }
md.digest((err2, mdOutput) => {
if (err2) {
console.error("[Callback]: err: " + err2.code);
} else {
console.error("[Callback]: MD result: " + mdOutput.data);
var mdLen = md.getMdLength();
console.error("[Callback]: MD len: " + mdLen);
}
});
}); });
}); });
} }
...@@ -1215,54 +1203,56 @@ function doMdByCallback(algName) { ...@@ -1215,54 +1203,56 @@ function doMdByCallback(algName) {
```javascript ```javascript
import cryptoFramework from "@ohos.security.cryptoFramework" import cryptoFramework from "@ohos.security.cryptoFramework"
async function updateData(index, obj, data) { // 可理解的字符串转成字节流
console.error("update " + (index + 1) + " MB data...");
return obj.update(data);
}
function stringToUint8Array(str) { function stringToUint8Array(str) {
var arr = []; let arr = [];
for (var i = 0, j = str.length; i < j; ++i) { for (let i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i)); arr.push(str.charCodeAt(i));
} }
var tmpUint8Array = new Uint8Array(arr); return new Uint8Array(arr);
return tmpUint8Array;
} }
function GenDataBlob(dataBlobLen) {
var dataBlob;
if (dataBlobLen == 12) {
dataBlob = {data: stringToUint8Array("my test data")};
} else {
console.error("GenDataBlob: dataBlobLen is invalid");
dataBlob = {data: stringToUint8Array("my test data")};
}
return dataBlob;
}
function LoopMdPromise(algName, loopSize) { // 使用Promise方式,完成分段摘要
var md; async function doLoopMdPromise() {
let mdAlgName = "SHA256"; // 摘要算法名
let md;
let mdOutput;
try { try {
md = cryptoFramework.createMd(algName); md = cryptoFramework.createMd(mdAlgName);
} 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);
return; return;
} }
console.error("[Promise]: Md algName is: " + md.algName); console.info("[Promise]: Md algName is: " + md.algName);
var promiseMdUpdate = md.update(GenDataBlob(12)); let messageText = "aaaaa.....bbbbb.....ccccc.....ddddd.....eee"; // 假设信息总共43字节
promiseMdUpdate.then(() => { let messageArr = [];
var PromiseMdDigest = md.digest(); let updateLength = 20; // 假设每20字节分段update一次,实际并无要求
return PromiseMdDigest;
}).then(async () => { for (let i = 0; i <= messageText.length; i++) {
for (var i = 0; i < loopSize; i++) { if ((i % updateLength == 0 || i == messageText.length) && messageArr.length != 0) {
await updateData(i, md, GenDataBlob(12)); let message = new Uint8Array(messageArr);
let messageBlob = { data : message };
// 使用await处理for循环里的update
try {
await md.update(messageBlob); // 分段update
} catch (error) {
console.error("await update error code: " + error.code + ", message is: " + error.message);
return;
}
messageArr = [];
} }
var PromiseMdDigest = md.digest(); // 按分割长度,填充messageArr
return PromiseMdDigest; if (i < messageText.length) {
}).then(mdOutput => { messageArr.push(messageText.charCodeAt(i));
console.error("[Promise]: MD result: " + mdOutput.data); }
var mdLen = md.getMdLength(); }
console.error("[Promise]: MD len: " + mdLen); let PromiseMdDigest = md.digest();
PromiseMdDigest.then(digestOutput => {
mdOutput = digestOutput;
console.info("[Promise]: MD result: " + mdOutput.data);
let mdLen = md.getMdLength();
console.info("[Promise]: MD len: " + mdLen);
}).catch(error => { }).catch(error => {
console.error("[Promise]: error: " + error.message); console.error("[Promise]: error: " + error.message);
}); });
...@@ -1374,77 +1364,73 @@ Mac(message authentication code)可以对消息进行完整性校验,通过使 ...@@ -1374,77 +1364,73 @@ Mac(message authentication code)可以对消息进行完整性校验,通过使
```javascript ```javascript
import cryptoFramework from "@ohos.security.cryptoFramework" import cryptoFramework from "@ohos.security.cryptoFramework"
// turn string into uint8Arr // 可理解的字符串转成字节流
function stringToUint8Array(str) { function stringToUint8Array(str) {
var arr = []; let arr = [];
for (var i = 0, j = str.length; i < j; ++i) { for (let i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i)); arr.push(str.charCodeAt(i));
}
var tmpUint8Array = new Uint8Array(arr);
return tmpUint8Array;
}
// generate blob with this func
function GenDataBlob(dataBlobLen) {
var dataBlob;
if (dataBlobLen == 12) {
dataBlob = {data: stringToUint8Array("my test data")};
} else {
console.error("GenDataBlob: dataBlobLen is invalid");
dataBlob = {data: stringToUint8Array("my test data")};
} }
return dataBlob; return new Uint8Array(arr);
} }
function doHmacByPromise(algName) { // 以Promise方式完成HMAC
var mac; function doHmacByPromise() {
let macAlgName = "SHA256"; // 摘要算法名
let message = "hmacTestMessgae"; // 待hmac数据
let macOutput;
let mac;
try { try {
mac = cryptoFramework.createMac(algName); mac = cryptoFramework.createMac(macAlgName);
} 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);
} }
console.error("[Promise]: Mac algName is: " + mac.algName); console.info("[Promise]: Mac algName is: " + mac.algName);
var KeyBlob = { let KeyBlob = {
// 128位密钥
data : stringToUint8Array("12345678abcdefgh") data : stringToUint8Array("12345678abcdefgh")
} }
var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128"); let symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128");
var promiseConvertKey = symKeyGenerator.convertKey(KeyBlob); // 将二进制密钥转换为算法库密钥
let promiseConvertKey = symKeyGenerator.convertKey(KeyBlob);
promiseConvertKey.then(symKey => { promiseConvertKey.then(symKey => {
var promiseMacInit = mac.init(symKey); let promiseMacInit = mac.init(symKey);
return promiseMacInit; return promiseMacInit;
}).then(() => { }).then(() => {
// 初次update // 数据量较少时,可以只做一次update,将数据全部传入,接口未对入参长度做限制
var promiseMacUpdate = mac.update(GenDataBlob(12)); let promiseMacUpdate = mac.update({ data: stringToUint8Array(message) });
return promiseMacUpdate;
}).then(() => {
// 可根据情况进行多次update
var promiseMacUpdate = mac.update(GenDataBlob(12));
return promiseMacUpdate; return promiseMacUpdate;
}).then(() => { }).then(() => {
var PromiseMacDoFinal = mac.doFinal(); let PromiseMacDoFinal = mac.doFinal();
return PromiseMacDoFinal; return PromiseMacDoFinal;
}).then(macOutput => { }).then(output => {
console.error("[Promise]: HMAC result: " + macOutput.data); macOutput = output;
var macLen = mac.getMacLength(); console.info("[Promise]: HMAC result: " + macOutput.data);
console.error("[Promise]: MAC len: " + macLen); let macLen = mac.getMacLength();
console.info("[Promise]: MAC len: " + macLen);
}).catch(error => { }).catch(error => {
console.error("[Promise]: error: " + error.message); console.error("[Promise]: error: " + error.message);
}); });
} }
// process by callback // 以Callback方式完成HMAC
function doHmacByCallback(algName) { function doHmacByCallback() {
var mac; let macAlgName = "SHA256"; // 摘要算法名
let message = "hmacTestMessgae"; // 待hmac数据
let macOutput;
let mac;
try { try {
mac = cryptoFramework.createMac(algName); mac = cryptoFramework.createMac(macAlgName);
} catch (error) { } catch (error) {
AlertDialog.show({message: "[Callback]: error code: " + error.code + ", message is: " + error.message}); AlertDialog.show({message: "[Callback]: error code: " + error.code + ", message is: " + error.message});
console.error("[Callback]: error code: " + error.code + ", message is: " + error.message); console.error("[Callback]: error code: " + error.code + ", message is: " + error.message);
} }
var KeyBlob = { console.info("[Promise]: Mac algName is: " + mac.algName);
let KeyBlob = {
// 128位密钥
data : stringToUint8Array("12345678abcdefgh") data : stringToUint8Array("12345678abcdefgh")
} }
var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128"); let symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128");
// 将二进制密钥转换为算法库密钥
symKeyGenerator.convertKey(KeyBlob, (err, symKey) => { symKeyGenerator.convertKey(KeyBlob, (err, symKey) => {
if (err) { if (err) {
console.error("[Callback]: err: " + err.code); console.error("[Callback]: err: " + err.code);
...@@ -1453,25 +1439,20 @@ function doHmacByCallback(algName) { ...@@ -1453,25 +1439,20 @@ function doHmacByCallback(algName) {
if (err1) { if (err1) {
console.error("[Callback]: err: " + err1.code); console.error("[Callback]: err: " + err1.code);
} }
// 初次update // 数据量较少时,可以只做一次update,将数据全部传入,接口未对入参长度做限制
mac.update(GenDataBlob(12), (err2, ) => { mac.update({ data: stringToUint8Array(message) }, (err2, ) => {
if (err2) { if (err2) {
console.error("[Callback]: err: " + err2.code); console.error("[Callback]: err: " + err2.code);
} }
// 可根据情况进行多次update mac.doFinal((err3, output) => {
mac.update(GenDataBlob(12), (err3, ) => {
if (err3) { if (err3) {
console.error("[Callback]: err: " + err3.code); console.error("[Callback]: err: " + err3.code);
} else {
macOutput = output;
console.error("[Callback]: HMAC result: " + macOutput.data);
let macLen = mac.getMacLength();
console.error("[Callback]: MAC len: " + macLen);
} }
mac.doFinal((err4, macOutput) => {
if (err4) {
console.error("[Callback]: err: " + err4.code);
} else {
console.error("[Callback]: HMAC result: " + macOutput.data);
var macLen = mac.getMacLength();
console.error("[Callback]: MAC len: " + macLen);
}
});
}); });
}); });
}); });
...@@ -1482,61 +1463,67 @@ function doHmacByCallback(algName) { ...@@ -1482,61 +1463,67 @@ function doHmacByCallback(algName) {
```javascript ```javascript
import cryptoFramework from "@ohos.security.cryptoFramework" import cryptoFramework from "@ohos.security.cryptoFramework"
async function updateData(index, obj, data) {
console.error("update " + (index + 1) + " MB data...");
return obj.update(data);
}
function stringToUint8Array(str) { function stringToUint8Array(str) {
var arr = []; let arr = [];
for (var i = 0, j = str.length; i < j; ++i) { for (let i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i)); arr.push(str.charCodeAt(i));
} }
var tmpUint8Array = new Uint8Array(arr); return new Uint8Array(arr);
return tmpUint8Array;
}
function GenDataBlob(dataBlobLen) {
var dataBlob;
if (dataBlobLen == 12) {
dataBlob = {data: stringToUint8Array("my test data")};
} else {
console.error("GenDataBlob: dataBlobLen is invalid");
dataBlob = {data: stringToUint8Array("my test data")};
}
return dataBlob;
} }
function LoopHmacPromise(algName, loopSize) { function doLoopHmacPromise() {
var mac; let macAlgName = "SHA256"; // 摘要算法名
let macOutput;
let mac;
try { try {
mac = cryptoFramework.createMac(algName); mac = cryptoFramework.createMac(macAlgName);
} 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);
return; return;
} }
console.error("[Promise]: Mac algName is: " + mac.algName); console.info("[Promise]: Mac algName is: " + mac.algName);
var KeyBlob = { let KeyBlob = {
// 128位密钥
data : stringToUint8Array("12345678abcdefgh") data : stringToUint8Array("12345678abcdefgh")
} }
var symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128"); let messageText = "aaaaa.....bbbbb.....ccccc.....ddddd.....eee"; // 假设信息总共43字节
var promiseConvertKey = symKeyGenerator.convertKey(KeyBlob); let updateLength = 20; // 假设每20字节分段update一次,实际并无要求
let symKeyGenerator = cryptoFramework.createSymKeyGenerator("AES128");
// 将二进制密钥转换为算法库密钥
let promiseConvertKey = symKeyGenerator.convertKey(KeyBlob);
promiseConvertKey.then(symKey => { promiseConvertKey.then(symKey => {
var promiseMacInit = mac.init(symKey); let promiseMacInit = mac.init(symKey);
return promiseMacInit; return promiseMacInit;
}).then(async () => { }).then(async () => {
for (var i = 0; i < loopSize; i++) { let promiseMacUpdate;
await updateData(i, mac, GenDataBlob(12)); let messageArr = [];
for (let i = 0; i <= messageText.length; i++) {
if ((i % updateLength == 0 || i == messageText.length) && messageArr.length != 0) {
let message = new Uint8Array(messageArr);
let messageBlob = { data : message };
// 使用await处理for循环里的update
try {
promiseMacUpdate = await mac.update(messageBlob); // 分段update
} catch (error) {
console.error("await update error code: " + error.code + ", message is: " + error.message);
return;
}
messageArr = [];
}
// 按分割长度,填充messageArr
if (i < messageText.length) {
messageArr.push(messageText.charCodeAt(i));
}
} }
var promiseMacUpdate = mac.update(GenDataBlob(12));
return promiseMacUpdate; return promiseMacUpdate;
}).then(() => { }).then(() => {
var PromiseMacDoFinal = mac.doFinal(); let PromiseMacDoFinal = mac.doFinal();
return PromiseMacDoFinal; return PromiseMacDoFinal;
}).then(macOutput => { }).then(output => {
console.error("[Promise]: HMAC result: " + macOutput.data); macOutput = output;
var macLen = mac.getMacLength(); console.log("[Promise]: HMAC result: " + macOutput.data);
console.error("[Promise]: MAC len: " + macLen); let macLen = mac.getMacLength();
console.log("[Promise]: MAC len: " + macLen);
}).catch(error => { }).catch(error => {
console.error("[Promise]: error: " + error.message); console.error("[Promise]: error: " + error.message);
}); });
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册