made url gen function a bit less shit

This commit is contained in:
uh wot 2021-07-17 00:46:25 +02:00
parent b357840761
commit 1efa8a0a55
Signed by: uhwot
GPG Key ID: CB2454984587B781
2 changed files with 20 additions and 26 deletions

6
dist/worker.js vendored

File diff suppressed because one or more lines are too long

View File

@ -8,35 +8,29 @@ addEventListener('fetch', event => {
const AESKEY = [106, 111, 54, 97, 101, 121, 54, 104, 97, 105, 100, 50, 84, 101, 105, 104] const AESKEY = [106, 111, 54, 97, 101, 121, 54, 104, 97, 105, 100, 50, 84, 101, 105, 104]
function str2bin(str) {
return new Uint8Array(Array.from(str).map(function (item) {
return item.charCodeAt(0);
}))
}
async function url_gen(md5_origin, format, id, media_version) { async function url_gen(md5_origin, format, id, media_version) {
const cipher = new aesjs.ModeOfOperation.ecb(AESKEY) const cipher = new aesjs.ModeOfOperation.ecb(AESKEY)
result = [md5_origin, format, id, media_version].join('\xa4') let result = [md5_origin, format, id, media_version].join('\xa4')
buf = new ArrayBuffer(result.length); let result_md5 = await crypto.subtle.digest('MD5', str2bin(result))
result_hash = new Uint8Array(buf);
for (i = 0; i < result.length; i++) { result_md5 = aesjs.utils.hex.fromBytes(new Uint8Array(result_md5))
result_hash[i] = result.charCodeAt(i);
result = result_md5 + '\xa4' + result + '\xa4'
// zero-padding
if (result.length % 16) {
result += '\x00'.repeat(16 - result.length % 16)
} }
result_hash = await crypto.subtle.digest('MD5', result_hash)
result_hash = Array.from(new Uint8Array(result_hash))
result_hash = result_hash.reduce(function(previous, current) {
return previous + '0'.concat(current.toString(16)).substr(-2, 2);
}, ''),
result = result_hash + '\xa4' + result + '\xa4' result = aesjs.utils.hex.fromBytes(cipher.encrypt(str2bin(result)))
// padding
result += '\x00'.repeat(result.length % 16 ? 16 - result.length % 16 : 0)
// converting to array for encryption / hex string
result = Array.from(result).map(function(item) {
return item.charCodeAt(0);
})
// encryption / hex string
result = (result = cipher.encrypt(result)).reduce(function(previous, current) {
return previous + '0'.concat(current.toString(16)).substr(-2, 2);
}, '')
// cdn template with first character of md5 string + hash // cdn template with first character of md5 string + hash
return `https://cdns-proxy-${md5_origin[0]}.dzcdn.net/api/1/${result}` return `https://cdns-proxy-${md5_origin[0]}.dzcdn.net/api/1/${result}`