-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
54 lines (39 loc) · 1.7 KB
/
test.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
// const a = "mLvaNoPcAVpC1aKoZLr8bxZOs8ApCEO5YS2EPPLqgUEDfihNIo3EbXtd"
// content-length <--> mLvaNoPcAVpC1aKoZLr8bxZOs8ApCEO5YS2EPPLqgUEDfihNIo3EbXtd
// 测试加密
const crypto = require('crypto')
const ALGORITHM = 'aes-128-gcm'
const PASSWORD = "1OKzP/qNsaMw1VJEoQfpW6lEd/AbREqyKzpBVxFC5iE=";
const IV = Buffer.from([152, 187, 218, 54, 131, 220, 1, 90, 66, 213, 162, 168]);
const SECURITY_KEY = createHashRandom(PASSWORD, 16)
const AAD = Buffer.from(PASSWORD)
function createHashRandom(seed, byteLength) {
let randomNums = crypto.createHash('sha1').update(seed).digest();
randomNums = crypto.createHash('sha1').update(randomNums).digest();
console.log(randomNums)
return randomNums.slice(0, byteLength)
}
function encrypt(plaintext) {
const cipher = crypto.createCipheriv(ALGORITHM, SECURITY_KEY, IV)
cipher.setAutoPadding(false)
cipher.setAAD(AAD)
let ciphertext = cipher.update(plaintext, 'utf8', 'base64')
ciphertext += cipher.final('base64')
const tag = cipher.getAuthTag();
return Buffer.concat([IV, Buffer.from(ciphertext, "base64"), tag]).toString("base64")
}
function decrypt(ciphertext) {
const ciphertextBuffer = Buffer.from(ciphertext, 'base64')
const bufferLength = ciphertextBuffer.length;
const realCiphertext = ciphertextBuffer.slice(IV.length, bufferLength - 16).toString('base64');
const tag = ciphertextBuffer.slice(bufferLength - 16)
console.log(tag, realCiphertext)
const decipher = crypto.createDecipheriv(ALGORITHM, SECURITY_KEY, IV)
decipher.setAutoPadding(false)
decipher.setAAD(AAD)
decipher.setAuthTag(tag)
let decrypted = decipher.update(realCiphertext, 'base64', 'utf8')
decrypted += decipher.final('utf8');
return decrypted;
}
console.log([[], 1])