import { Sha256 } from '@aws-crypto/sha256-js' import CryptoJs from 'crypto-js' import encHex from 'crypto-js/enc-hex' onmessage = async function (event) { const data = event.data // 获取文件数据 // chunk方式读取文件 const chunkSize = 1024 * 1024 // 每次读取1MB const fileSize = data.size // 文件大小 let offset = 0 // 偏移量 const createSha256Uint8Array32 = new Sha256() // SHA256 Unit8Array(32) const createSha256 = CryptoJs.algo.SHA256.create() // SHA256 Hex const createMD5 = CryptoJs.algo.MD5.create() // MD5 while (offset < fileSize) { const chunk = new Uint8Array(await data.slice(offset, offset + chunkSize).arrayBuffer()) // 读取chunkSize大小的文件块 const wordArray = CryptoJs.lib.WordArray.create(chunk) createSha256.update(wordArray) createMD5.update(wordArray) createSha256Uint8Array32.update(chunk) offset += chunkSize // 计算进度 // const progress = Math.min(100, Math.round((offset / fileSize) * 100)) // console.log(progress) // 打印进度 // postMessage({ progress, offset, fileSize, }) // 将进度发送给主线程 } const [sha256Uint8Array32, sha256, md5] = await Promise.all([ createSha256Uint8Array32.digest(), // SHA256 Unit8Array(32) encHex.stringify(createSha256.finalize()), // SHA256 Hex encHex.stringify(createMD5.finalize()), // MD5 ]) const obj = { md5, // MD5 sha256, // SHA256 Hex sha256Int8Array32: Array.from(new Int8Array(sha256Uint8Array32)), // SHA256 Int8Array(32) } console.log(obj) // 打印结果对象 postMessage(obj) // 将结果对象发送给主线程 } 加功能计算文件sha1
时间: 2024-03-06 16:47:21 浏览: 142
您可以使用crypto-js中提供的SHA1算法来计算文件的SHA1值,代码如下:
```javascript
import { Sha256 } from '@aws-crypto/sha256-js'
import CryptoJs from 'crypto-js'
import encHex from 'crypto-js/enc-hex'
import encBase64 from 'crypto-js/enc-base64'
import encUtf8 from 'crypto-js/enc-utf8'
onmessage = async function (event) {
const data = event.data // 获取文件数据
// chunk方式读取文件
const chunkSize = 1024 * 1024 // 每次读取1MB
const fileSize = data.size // 文件大小
let offset = 0 // 偏移量
const createSha256Uint8Array32 = new Sha256() // SHA256 Unit8Array(32)
const createSha1 = CryptoJs.algo.SHA1.create() // SHA1 Hex
const createSha256 = CryptoJs.algo.SHA256.create() // SHA256 Hex
const createMD5 = CryptoJs.algo.MD5.create() // MD5
while (offset < fileSize) {
const chunk = new Uint8Array(await data.slice(offset, offset + chunkSize).arrayBuffer()) // 读取chunkSize大小的文件块
const wordArray = CryptoJs.lib.WordArray.create(chunk)
createSha256.update(wordArray)
createSha1.update(wordArray)
createMD5.update(wordArray)
createSha256Uint8Array32.update(chunk)
offset += chunkSize
// 计算进度
// const progress = Math.min(100, Math.round((offset / fileSize) * 100))
// console.log(progress) // 打印进度
// postMessage({ progress, offset, fileSize, }) // 将进度发送给主线程
}
const [sha256Uint8Array32, sha1, sha256, md5] = await Promise.all([
createSha256Uint8Array32.digest(), // SHA256 Unit8Array(32)
encHex.stringify(createSha1.finalize()), // SHA1 Hex
encHex.stringify(createSha256.finalize()), // SHA256 Hex
encBase64.stringify(createMD5.finalize()), // MD5
])
const obj = {
md5, // MD5
sha1, // SHA1 Hex
sha256, // SHA256 Hex
sha256Int8Array32: Array.from(new Int8Array(sha256Uint8Array32)), // SHA256 Int8Array(32)
}
console.log(obj) // 打印结果对象
postMessage(obj) // 将结果对象发送给主线程
}
```
需要注意的是,SHA1算法已经不再被推荐使用,因为其已经不再具备足够的安全性。建议您使用更为安全的算法,如SHA256。
阅读全文