update crypto
parent
070ccc8610
commit
95b658b4a0
@ -0,0 +1,60 @@
|
||||
import CryptoJS from 'crypto-js/crypto-js'
|
||||
|
||||
|
||||
const bufferChunkSize = 1024 * 1024 * 5;
|
||||
|
||||
export const fileHash = (file, config={}) => {
|
||||
config = {
|
||||
algo: 'MD5',
|
||||
enc: 'Hex',
|
||||
onProgress: (a,b)=>{console.debug(a,b)}
|
||||
,...config
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
if(file==null || file.size==0){
|
||||
resolve(null)
|
||||
return
|
||||
}
|
||||
const hash = CryptoJS.algo[config.algo].create()
|
||||
let count = 0;
|
||||
const reader = new FileReader()
|
||||
|
||||
// 以流的方式读取文件
|
||||
function readChunk(start, end) {
|
||||
const slice = file.slice(start, end)
|
||||
reader.readAsArrayBuffer(slice);
|
||||
}
|
||||
|
||||
// 递归读取文件的每个分块
|
||||
function processChunk(offset) {
|
||||
const start = offset;
|
||||
const end = Math.min(start + bufferChunkSize, file.size)
|
||||
count = end
|
||||
|
||||
readChunk(start, end)
|
||||
}
|
||||
|
||||
// 当读取完整个文件后,计算哈希值并返回
|
||||
reader.onloadend = function () {
|
||||
const arrayBuffer = reader.result
|
||||
const wordArray = CryptoJS.lib.WordArray.create(arrayBuffer)
|
||||
|
||||
// 更新哈希对象
|
||||
hash.update(wordArray)
|
||||
config.onProgress(count,file.size)
|
||||
if (count < file.size) {
|
||||
// 继续处理下一个分块
|
||||
processChunk(count)
|
||||
}
|
||||
else {
|
||||
// 计算哈希值并返回
|
||||
const ret = hash.finalize()
|
||||
resolve(ret.toString(CryptoJS.enc.Hex))
|
||||
}
|
||||
}
|
||||
config.onProgress(count,file.size)
|
||||
// 开始处理文件内容分块
|
||||
processChunk(0)
|
||||
|
||||
})
|
||||
}
|
||||
@ -0,0 +1,75 @@
|
||||
export { fileHash } from './fileHash'
|
||||
|
||||
import CryptoJS from 'crypto-js/crypto-js'
|
||||
|
||||
import { encrypt, decrypt } from '../jsencrypt'
|
||||
|
||||
|
||||
const AESKey = "d34xsfljkwSD}2cv32$#%dfdgvcJH{PIOPeopir1KSHD"
|
||||
|
||||
/**
|
||||
* Base64编码
|
||||
* @param {String} str
|
||||
* @returns
|
||||
*/
|
||||
const Base64Encode = (str) => CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(str))
|
||||
|
||||
/**
|
||||
* Base64解码
|
||||
* @param {String} str
|
||||
* @returns
|
||||
*/
|
||||
const Base64Decode = (str) => CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(str))
|
||||
|
||||
/**
|
||||
* 消息摘要md5加密
|
||||
* @param {String} str
|
||||
* @returns
|
||||
*/
|
||||
const md5 = (str) => CryptoJS.enc.Hex.stringify(CryptoJS.MD5(str))
|
||||
|
||||
/**
|
||||
* 消息摘要sha256加密
|
||||
* @param {String} str
|
||||
* @returns
|
||||
*/
|
||||
const sha256 = (str) => CryptoJS.enc.Hex.stringify(CryptoJS.SHA256(str))
|
||||
|
||||
|
||||
/**
|
||||
* 对称加密AES
|
||||
* @param {String} str
|
||||
* @returns
|
||||
*/
|
||||
const aesEncrypt = (str) => CryptoJS.AES.encrypt(str, AESKey).toString()
|
||||
/**
|
||||
* 对称解密AES
|
||||
* @param {String} str
|
||||
* @returns
|
||||
*/
|
||||
const aesDecrypt = (str) => CryptoJS.AES.decrypt(str, AESKey).toString(CryptoJS.enc.Utf8)
|
||||
|
||||
export { CryptoJS, Base64Encode, Base64Decode, md5, sha256, encrypt, decrypt, aesEncrypt, aesDecrypt }
|
||||
|
||||
|
||||
// let str = "https://evoai.cn"
|
||||
// let strEncode = Base64Encode(str)
|
||||
// console.debug("Base64Encode", strEncode)
|
||||
// console.debug("Base64Decode", Base64Decode(strEncode))
|
||||
|
||||
// console.debug("md5", md5(str))
|
||||
// console.debug("sha256", sha256(str))
|
||||
|
||||
// strEncode = encrypt(str);
|
||||
// console.debug("encrypt", strEncode)
|
||||
|
||||
// console.debug("decrypt", decrypt(strEncode))
|
||||
|
||||
// strEncode = aesEncrypt(str)
|
||||
// console.debug("aesEncrypt", strEncode)
|
||||
// console.debug("aesDecrypt", aesDecrypt(strEncode))
|
||||
|
||||
// console.debug("Hex.stringify",Hex.stringify(str));
|
||||
// console.debug("Hex.parse",Hex.parse(Hex.stringify(str)));
|
||||
// var words = CryptoJS.enc.Base64.parse("SGVsbG8sIFdvcmxkIQ==");
|
||||
// console.debug(words)
|
||||
Loading…
Reference in New Issue