forked from nondanee/UnblockNeteaseMusic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.js
executable file
·68 lines (64 loc) · 2.37 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
'use strict'
const crypto = require('crypto')
const parse = require('url').parse
const uriKey = '3go8&$8*3*3h0k(2)2'
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: '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: 'eparams=' + encrypt(Buffer.from(text), linuxapiKey).toString('hex').toUpperCase()
}
}
},
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 string = Array.from(Array(id.length).keys()).map(index => String.fromCharCode(id.charCodeAt(index) ^ uriKey.charCodeAt(index % uriKey.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()))
})
}
}