function stringToUint8Array(str: string) {
return new Uint8Array(buffer.from(str,'utf-8').buffer);
}
function uint8ArrayToString(array:Uint8Array) {
let out: string = '';
let index: number = 0;
let len: number = array.length;
while (index < len) {
let character = array[index++];
switch(character >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
out += String.fromCharCode(character);
break;
case 12:
case 13:
out += String.fromCharCode(((character & 0x1F) << 6) | (array[index++] & 0x3F));
break;
case 14:
out += String.fromCharCode(((character & 0x0F) << 12) | ((array[index++] & 0x3F) << 6) | ((array[index++] & 0x3F) << 0));
break;
default:
break;
}
}
return out;
}
async function doMd5() {
let TAG='Test-';
let mdAlgName = 'MD5';
// let message = “ahdjashdjahduqwyieqwijkdals45451234545155aadasdadawdsa”;
let md = cryptoFramework.createMd(mdAlgName);
console.info('mdAlgName:' + md.algName);
let promiseMdUpdate = md.update({ data: stringToUint8Array(bodyStr) });
promiseMdUpdate.then(() => {
let PromiseMdDigest = md.digest();
//console.log(“MD5:”+PromiseMdDigest);
return PromiseMdDigest;
}).then(digestOutput => {
let mdOutput = digestOutput;
console.info(TAG+'Result:' + mdOutput.data);
//转成16进制
let buf=buffer.from(mdOutput.data);
console.log(TAG+'Hex222:'+buf.toString('hex'));
let mdLen = md.getMdLength();
console.info(TAG+"MDlength: " + mdLen);
//转成base64
let base64=new util.Base64Helper;
let resBase64=base64.encodeToStringSync(mdOutput.data)
console.log(TAG+'base64:'+resBase64)
}).catch((error: BusinessError) => {
console.error('error:' + error.message);
});
}