60%
This commit is contained in:
@@ -19,6 +19,14 @@ const toWords = (value) => {
|
||||
return words
|
||||
}
|
||||
|
||||
const toWordsFromBytes = (bytes) => {
|
||||
const words = []
|
||||
for (let index = 0; index < bytes.length; index += 1) {
|
||||
words[index >> 2] |= bytes[index] << ((index % 4) * 8)
|
||||
}
|
||||
return words
|
||||
}
|
||||
|
||||
const toHex = (words) => {
|
||||
const alphabet = '0123456789abcdef'
|
||||
let output = ''
|
||||
@@ -70,3 +78,16 @@ export const calcMD5 = (value) => {
|
||||
const utf8Value = unescape(encodeURIComponent(String(value)))
|
||||
return toHex(coreMd5(toWords(utf8Value), utf8Value.length * 8))
|
||||
}
|
||||
|
||||
export const calcMD5Bytes = (value) => {
|
||||
const bytes = value instanceof ArrayBuffer
|
||||
? new Uint8Array(value)
|
||||
: ArrayBuffer.isView(value)
|
||||
? new Uint8Array(value.buffer, value.byteOffset, value.byteLength)
|
||||
: null
|
||||
if (!bytes) throw new TypeError('MD5 字节输入必须是 ArrayBuffer 或 TypedArray')
|
||||
if (bytes.length > 0x1fffffff) {
|
||||
throw new RangeError('当前 MD5 实现不支持超过 512MB 的单文件')
|
||||
}
|
||||
return toHex(coreMd5(toWordsFromBytes(bytes), bytes.length * 8))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user