forked from nondanee/UnblockNeteaseMusic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.js
executable file
·99 lines (94 loc) · 3.79 KB
/
crypto.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict'
const crypto = require('crypto')
const parse = require('url').parse
const bodyify = require('querystring').stringify
const eapiKey = 'e82ckenh8dichen8'
const linuxapiKey = 'rFgB&h#%2?^eDg:Q'
const decrypt = (buffer, key) => {
let decipher = crypto.createDecipheriv('aes-128-ecb', key, '')
return Buffer.concat([decipher.update(buffer), decipher.final()])
}
const encrypt = (buffer, key) => {
let cipher = crypto.createCipheriv('aes-128-ecb', key, '')
return Buffer.concat([cipher.update(buffer), cipher.final()])
}
module.exports = {
eapi: {
encrypt: buffer => encrypt(buffer, eapiKey),
decrypt: buffer => decrypt(buffer, eapiKey),
encryptRequest: (url, object) => {
url = parse(url)
let text = JSON.stringify(object)
let message = `nobody${url.path}use${text}md5forencrypt`
let digest = crypto.createHash('md5').update(message).digest('hex')
let data = `${url.path}-36cd479b6b5-${text}-36cd479b6b5-${digest}`
return {
url: url.href.replace(/\w*api/, 'eapi'),
body: bodyify({
params: encrypt(Buffer.from(data), eapiKey).toString('hex').toUpperCase()
})
}
}
},
linuxapi: {
encrypt: buffer => encrypt(buffer, linuxapiKey),
decrypt: buffer => decrypt(buffer, linuxapiKey),
encryptRequest: (url, object) => {
url = parse(url)
let text = JSON.stringify({method: 'POST', url: url.href, params: object})
return {
url: url.resolve('/api/linux/forward'),
body: bodyify({
eparams: encrypt(Buffer.from(text), linuxapiKey).toString('hex').toUpperCase()
})
}
}
},
miguapi: {
encrypt: object => {
let text = JSON.stringify(object)
const derive = (password, salt, keyLength, ivSize) => { // EVP_BytesToKey
salt = salt || Buffer.alloc(0)
let keySize = keyLength / 8
let repeat = Math.ceil((keySize + ivSize * 8) / 32)
let buffer = Buffer.concat(Array(repeat).fill(null).reduce(result =>
result.concat(crypto.createHash('md5').update(Buffer.concat([result.slice(-1)[0], password, salt])).digest())
, [Buffer.alloc(0)]))
return {
key: buffer.slice(0, keySize),
iv: buffer.slice(keySize, keySize + ivSize)
}
}
let password = Buffer.from(crypto.randomBytes(32).toString('hex')), salt = crypto.randomBytes(8),
key = '-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8asrfSaoOb4je+DSmKdriQJKWVJ2oDZrs3wi5W67m3LwTB9QVR+cE3XWU21Nx+YBxS0yun8wDcjgQvYt625ZCcgin2ro/eOkNyUOTBIbuj9CvMnhUYiR61lC1f1IGbrSYYimqBVSjpifVufxtx/I3exReZosTByYp4Xwpb1+WAQIDAQAB\n-----END PUBLIC KEY-----'
let secret = derive(password, salt, 256, 16)
let cipher = crypto.createCipheriv('aes-256-cbc', secret.key, secret.iv)
return bodyify({
data: Buffer.concat([Buffer.from('Salted__'), salt, cipher.update(Buffer.from(text)), cipher.final()]).toString('base64'),
secKey: crypto.publicEncrypt({key, padding: crypto.constants.RSA_PKCS1_PADDING}, password).toString('base64')
})
}
},
base64: {
encode: text => Buffer.from(text).toString('base64').replace(/\+/g, '-').replace(/\//g, '_'),
decode: text => Buffer.from(text.replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString('ascii')
},
uri: {
retrieve: id => {
id = id.toString().trim()
let key = '3go8&$8*3*3h0k(2)2'
let string = Array.from(Array(id.length).keys()).map(index => String.fromCharCode(id.charCodeAt(index) ^ key.charCodeAt(index % key.length))).join('')
let result = crypto.createHash('md5').update(string).digest('base64').replace(/\//g, '_').replace(/\+/g, '-')
return `http://p1.music.126.net/${result}/${id}`
}
},
md5: {
digest: value => crypto.createHash('md5').update(value).digest('hex'),
pipe: source => new Promise((resolve, reject) => {
let digest = crypto.createHash('md5').setEncoding('hex')
source.pipe(digest)
.on('error', error => reject(error))
.once('finish', () => resolve(digest.read()))
})
}
}