deemix-js/deemix/utils/crypto.js

57 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-05-16 19:40:14 +00:00
const crypto = require('crypto')
2021-07-25 11:22:17 +00:00
let Blowfish
try {
2021-07-25 14:55:45 +00:00
Blowfish = require('./blowfish.js')
2021-07-25 11:22:17 +00:00
} catch (e) { /* empty */ }
2021-05-16 19:40:14 +00:00
function _md5 (data, type = 'binary') {
let md5sum = crypto.createHash('md5')
md5sum.update(Buffer.from(data, type))
return md5sum.digest('hex')
}
function _ecbCrypt (key, data) {
let cipher = crypto.createCipheriv("aes-128-ecb", Buffer.from(key), Buffer.from(""));
2021-06-08 17:43:33 +00:00
cipher.setAutoPadding(false)
2021-05-16 19:40:14 +00:00
return Buffer.concat([cipher.update(data, 'binary'), cipher.final()]).toString("hex").toLowerCase();
}
function _ecbDecrypt (key, data) {
let cipher = crypto.createDecipheriv("aes-128-ecb", Buffer.from(key), Buffer.from(""));
2021-06-08 17:43:33 +00:00
cipher.setAutoPadding(false)
2021-05-16 19:40:14 +00:00
return Buffer.concat([cipher.update(data, 'binary'), cipher.final()]).toString("hex").toLowerCase();
}
function generateBlowfishKey(trackId) {
const SECRET = 'g4el58wc0zvf9na1';
const idMd5 = _md5(trackId.toString(), 'ascii')
let bfKey = ''
for (let i = 0; i < 16; i++) {
bfKey += String.fromCharCode(idMd5.charCodeAt(i) ^ idMd5.charCodeAt(i + 16) ^ SECRET.charCodeAt(i))
}
2021-07-20 12:44:19 +00:00
return String(bfKey);
2021-05-16 19:40:14 +00:00
}
function decryptChunk(chunk, blowFishKey){
2021-07-25 11:22:17 +00:00
let ciphers = crypto.getCiphers()
if (ciphers.includes('bf-cbc')){
let cipher = crypto.createDecipheriv('bf-cbc', blowFishKey, Buffer.from([0, 1, 2, 3, 4, 5, 6, 7]))
cipher.setAutoPadding(false)
return Buffer.concat([cipher.update(chunk), cipher.final()])
}
if (Blowfish){
let cipher = new Blowfish(blowFishKey, Blowfish.MODE.CBC, Blowfish.PADDING.NULL)
cipher.setIv(Buffer.from([0, 1, 2, 3, 4, 5, 6, 7]))
return Buffer.from(cipher.decode(chunk, Blowfish.TYPE.UINT8_ARRAY))
}
throw new Error("Can't find a way to decrypt chunks")
2021-05-16 19:40:14 +00:00
}
module.exports = {
_md5,
_ecbCrypt,
2021-07-20 12:44:19 +00:00
_ecbDecrypt,
generateBlowfishKey,
decryptChunk
2021-05-16 19:40:14 +00:00
}