feat: switch upload hash to md5

This commit is contained in:
2026-01-08 15:40:48 +08:00
parent 6ffe58ffb4
commit 7f162b7c45
8 changed files with 348 additions and 8 deletions

View File

@@ -0,0 +1,144 @@
const hexChars = '0123456789abcdef';
function add(x, y) {
return (x + y) >>> 0;
}
function rol(x, n) {
return (x << n) | (x >>> (32 - n));
}
function cmn(q, a, b, x, s, t) {
return add(rol(add(add(a, q), add(x, t)), s), b);
}
function ff(a, b, c, d, x, s, t) {
return cmn((b & c) | (~b & d), a, b, x, s, t);
}
function gg(a, b, c, d, x, s, t) {
return cmn((b & d) | (c & ~d), a, b, x, s, t);
}
function hh(a, b, c, d, x, s, t) {
return cmn(b ^ c ^ d, a, b, x, s, t);
}
function ii(a, b, c, d, x, s, t) {
return cmn(c ^ (b | ~d), a, b, x, s, t);
}
function toHex(n) {
let out = '';
for (let i = 0; i < 4; i++) {
const byte = (n >>> (i * 8)) & 0xff;
out += hexChars.charAt((byte >>> 4) & 0x0f) + hexChars.charAt(byte & 0x0f);
}
return out;
}
export function md5ArrayBuffer(buffer) {
const bytes = new Uint8Array(buffer);
const words = [];
for (let i = 0; i < bytes.length; i++) {
words[i >> 2] |= bytes[i] << ((i % 4) * 8);
}
const bitLen = bytes.length * 8;
words[bitLen >> 5] |= 0x80 << (bitLen % 32);
words[(((bitLen + 64) >>> 9) << 4) + 14] = bitLen;
const totalLen = (((bitLen + 64) >>> 9) << 4) + 16;
for (let i = 0; i < totalLen; i++) {
if (words[i] === undefined) {
words[i] = 0;
}
}
let a = 0x67452301;
let b = 0xefcdab89;
let c = 0x98badcfe;
let d = 0x10325476;
for (let i = 0; i < words.length; i += 16) {
const oa = a;
const ob = b;
const oc = c;
const od = d;
a = ff(a, b, c, d, words[i + 0], 7, 0xd76aa478);
d = ff(d, a, b, c, words[i + 1], 12, 0xe8c7b756);
c = ff(c, d, a, b, words[i + 2], 17, 0x242070db);
b = ff(b, c, d, a, words[i + 3], 22, 0xc1bdceee);
a = ff(a, b, c, d, words[i + 4], 7, 0xf57c0faf);
d = ff(d, a, b, c, words[i + 5], 12, 0x4787c62a);
c = ff(c, d, a, b, words[i + 6], 17, 0xa8304613);
b = ff(b, c, d, a, words[i + 7], 22, 0xfd469501);
a = ff(a, b, c, d, words[i + 8], 7, 0x698098d8);
d = ff(d, a, b, c, words[i + 9], 12, 0x8b44f7af);
c = ff(c, d, a, b, words[i + 10], 17, 0xffff5bb1);
b = ff(b, c, d, a, words[i + 11], 22, 0x895cd7be);
a = ff(a, b, c, d, words[i + 12], 7, 0x6b901122);
d = ff(d, a, b, c, words[i + 13], 12, 0xfd987193);
c = ff(c, d, a, b, words[i + 14], 17, 0xa679438e);
b = ff(b, c, d, a, words[i + 15], 22, 0x49b40821);
a = gg(a, b, c, d, words[i + 1], 5, 0xf61e2562);
d = gg(d, a, b, c, words[i + 6], 9, 0xc040b340);
c = gg(c, d, a, b, words[i + 11], 14, 0x265e5a51);
b = gg(b, c, d, a, words[i + 0], 20, 0xe9b6c7aa);
a = gg(a, b, c, d, words[i + 5], 5, 0xd62f105d);
d = gg(d, a, b, c, words[i + 10], 9, 0x02441453);
c = gg(c, d, a, b, words[i + 15], 14, 0xd8a1e681);
b = gg(b, c, d, a, words[i + 4], 20, 0xe7d3fbc8);
a = gg(a, b, c, d, words[i + 9], 5, 0x21e1cde6);
d = gg(d, a, b, c, words[i + 14], 9, 0xc33707d6);
c = gg(c, d, a, b, words[i + 3], 14, 0xf4d50d87);
b = gg(b, c, d, a, words[i + 8], 20, 0x455a14ed);
a = gg(a, b, c, d, words[i + 13], 5, 0xa9e3e905);
d = gg(d, a, b, c, words[i + 2], 9, 0xfcefa3f8);
c = gg(c, d, a, b, words[i + 7], 14, 0x676f02d9);
b = gg(b, c, d, a, words[i + 12], 20, 0x8d2a4c8a);
a = hh(a, b, c, d, words[i + 5], 4, 0xfffa3942);
d = hh(d, a, b, c, words[i + 8], 11, 0x8771f681);
c = hh(c, d, a, b, words[i + 11], 16, 0x6d9d6122);
b = hh(b, c, d, a, words[i + 14], 23, 0xfde5380c);
a = hh(a, b, c, d, words[i + 1], 4, 0xa4beea44);
d = hh(d, a, b, c, words[i + 4], 11, 0x4bdecfa9);
c = hh(c, d, a, b, words[i + 7], 16, 0xf6bb4b60);
b = hh(b, c, d, a, words[i + 10], 23, 0xbebfbc70);
a = hh(a, b, c, d, words[i + 13], 4, 0x289b7ec6);
d = hh(d, a, b, c, words[i + 0], 11, 0xeaa127fa);
c = hh(c, d, a, b, words[i + 3], 16, 0xd4ef3085);
b = hh(b, c, d, a, words[i + 6], 23, 0x04881d05);
a = hh(a, b, c, d, words[i + 9], 4, 0xd9d4d039);
d = hh(d, a, b, c, words[i + 12], 11, 0xe6db99e5);
c = hh(c, d, a, b, words[i + 15], 16, 0x1fa27cf8);
b = hh(b, c, d, a, words[i + 2], 23, 0xc4ac5665);
a = ii(a, b, c, d, words[i + 0], 6, 0xf4292244);
d = ii(d, a, b, c, words[i + 7], 10, 0x432aff97);
c = ii(c, d, a, b, words[i + 14], 15, 0xab9423a7);
b = ii(b, c, d, a, words[i + 5], 21, 0xfc93a039);
a = ii(a, b, c, d, words[i + 12], 6, 0x655b59c3);
d = ii(d, a, b, c, words[i + 3], 10, 0x8f0ccc92);
c = ii(c, d, a, b, words[i + 10], 15, 0xffeff47d);
b = ii(b, c, d, a, words[i + 1], 21, 0x85845dd1);
a = ii(a, b, c, d, words[i + 8], 6, 0x6fa87e4f);
d = ii(d, a, b, c, words[i + 15], 10, 0xfe2ce6e0);
c = ii(c, d, a, b, words[i + 6], 15, 0xa3014314);
b = ii(b, c, d, a, words[i + 13], 21, 0x4e0811a1);
a = ii(a, b, c, d, words[i + 4], 6, 0xf7537e82);
d = ii(d, a, b, c, words[i + 11], 10, 0xbd3af235);
c = ii(c, d, a, b, words[i + 2], 15, 0x2ad7d2bb);
b = ii(b, c, d, a, words[i + 9], 21, 0xeb86d391);
a = add(a, oa);
b = add(b, ob);
c = add(c, oc);
d = add(d, od);
}
return toHex(a) + toHex(b) + toHex(c) + toHex(d);
}

View File

@@ -222,7 +222,7 @@ import ProgressBar from 'primevue/progressbar';
import Image from 'primevue/image';
import Toast from 'primevue/toast';
import draggable from 'vuedraggable';
import { sha256 } from 'js-sha256';
import { md5ArrayBuffer } from '../../utils/md5';
import { useToast } from 'primevue/usetoast';
import { computed, reactive, ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
@@ -339,7 +339,7 @@ const triggerUpload = (type) => {
const calculateHash = async (file) => {
const buffer = await file.arrayBuffer();
return sha256(buffer);
return md5ArrayBuffer(buffer);
};
const handleFileChange = async (event) => {
@@ -518,4 +518,4 @@ const saveContent = async (targetStatus) => {
:deep(.p-select:not(.p-disabled).p-focus) {
@apply border-primary-500 ring-2 ring-primary-100 shadow-none;
}
</style>
</style>