wasm-aeslib
v0.1.7
Published
wasm aes128 crypto lib
Downloads
7
Readme
a wasm aes128 crypto lib created by rust
AES
USEAGE
//实例化对象
const aeslib = new wasm.Aeslib('1234567812345678','1234567812345678');
//字符串加密为base64
str2base64('a');
base642str('DKMTWehI9e81wINnnusn7w==');
//上传图片并加密
new Promise((resolve) => {
//监听文件变化
const myfile = document.getElementById('myfile');
myfile.onchange = resolve;
}).then((evt) => {
return new Promise((resolve) => {
const file = evt.target.files[0];
const reader = new FileReader();
//读取文件
reader.onload = function() {
resolve(this.result);
};
reader.readAsArrayBuffer(file);
});
}).then((result) => {
//加密
const arrayBuff = fileencode(result);
//解密
filedecode(arrayBuff);
});
//demo1 字符串加密为base64
function str2base64(str){
var blob = new Blob(['a'], {type: 'text/plain'});
var reader = new FileReader();
reader.onload = () => {
var u8Array = new Uint8Array(reader.result);
try{
const result = aeslib.base64encode(u8Array);
console.log(result); //DKMTWehI9e81wINnnusn7w==
}catch(err){
console.log(err);
}
};
reader.readAsArrayBuffer(blob);
}
//demo2 base64为字符串加密
function base642str(str){
try{
const result = aeslib.base64decode(str);
console.log(result); //a
}catch(err){
console.log(err);
}
}
//demo3 fileencode
function fileencode(arrayBuff){
const newResult = aeslib.fileencode(new Uint8Array(arrayBuff) );
return newResult;
}
//demo3 filedecode
function filedecode(arrayBuff){
const deResult = aeslib.filedecode(arrayBuff);
const ele = new Image();
const blob = new Blob([deResult.buffer]);
ele.setAttribute('src', URL.createObjectURL(blob));
document.body.appendChild(ele);
}
methods
- base64encode(arr: Uint8Array) => base64 string
- base64decode(str: String) => String
- fileencode(arr: Uint8Array) => Uint8Array
- filedecode(arr: Uint8Array) => Uint8Array