2017-11-15 10:54:12 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-11-16 07:23:19 +08:00
|
|
|
const crypto = require('crypto');
|
2017-12-31 09:14:05 +08:00
|
|
|
const log = require('./log');
|
2017-11-11 11:55:19 +08:00
|
|
|
|
2017-11-16 11:13:45 +08:00
|
|
|
function arraysIdentical(a, b) {
|
|
|
|
let i = a.length;
|
|
|
|
if (i !== b.length) return false;
|
|
|
|
while (i--) {
|
|
|
|
if (a[i] !== b[i]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function shaArray(content) {
|
|
|
|
// we use this as simple checksum and don't rely on its security so SHA-1 is good enough
|
2017-11-16 12:39:50 +08:00
|
|
|
return crypto.createHash('sha1').update(content).digest();
|
2017-11-16 11:13:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function pad(data) {
|
2019-01-12 06:04:51 +08:00
|
|
|
if (data.length > 16) {
|
|
|
|
data = data.slice(0, 16);
|
|
|
|
}
|
|
|
|
else if (data.length < 16) {
|
|
|
|
const zeros = Array(16 - data.length).fill(0);
|
2017-11-16 11:13:45 +08:00
|
|
|
|
2019-01-12 06:04:51 +08:00
|
|
|
data = Buffer.concat([data, Buffer.from(zeros)]);
|
2017-11-16 07:23:19 +08:00
|
|
|
}
|
2017-11-16 11:13:45 +08:00
|
|
|
|
2019-01-13 07:24:51 +08:00
|
|
|
return Buffer.from(data);
|
2017-11-16 11:13:45 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 06:04:51 +08:00
|
|
|
function encrypt(key, plainText, ivLength = 13) {
|
2017-11-16 13:22:00 +08:00
|
|
|
if (!key) {
|
2017-11-16 11:13:45 +08:00
|
|
|
throw new Error("No data key!");
|
|
|
|
}
|
|
|
|
|
2017-11-16 12:39:50 +08:00
|
|
|
const plainTextBuffer = Buffer.from(plainText);
|
|
|
|
|
2019-01-12 06:04:51 +08:00
|
|
|
const iv = crypto.randomBytes(ivLength);
|
2017-11-16 13:22:00 +08:00
|
|
|
const cipher = crypto.createCipheriv('aes-128-cbc', pad(key), pad(iv));
|
2017-11-16 11:13:45 +08:00
|
|
|
|
2017-11-16 12:39:50 +08:00
|
|
|
const digest = shaArray(plainTextBuffer).slice(0, 4);
|
2017-11-16 11:13:45 +08:00
|
|
|
|
2017-11-16 12:39:50 +08:00
|
|
|
const digestWithPayload = Buffer.concat([digest, plainTextBuffer]);
|
2017-11-16 11:13:45 +08:00
|
|
|
|
2017-11-16 12:39:50 +08:00
|
|
|
const encryptedData = Buffer.concat([cipher.update(digestWithPayload), cipher.final()]);
|
2017-11-16 11:13:45 +08:00
|
|
|
|
2019-01-12 06:04:51 +08:00
|
|
|
const encryptedDataWithIv = Buffer.concat([iv, encryptedData]);
|
|
|
|
|
|
|
|
return encryptedDataWithIv.toString('base64');
|
2017-11-16 11:13:45 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 06:04:51 +08:00
|
|
|
function decrypt(key, cipherText, ivLength = 13) {
|
2017-11-16 13:22:00 +08:00
|
|
|
if (!key) {
|
2017-11-16 11:13:45 +08:00
|
|
|
return "[protected]";
|
|
|
|
}
|
|
|
|
|
2019-01-12 06:04:51 +08:00
|
|
|
const cipherTextBufferWithIv = Buffer.from(cipherText, 'base64');
|
|
|
|
const iv = cipherTextBufferWithIv.slice(0, ivLength);
|
|
|
|
|
|
|
|
const cipherTextBuffer = cipherTextBufferWithIv.slice(ivLength);
|
|
|
|
|
2017-11-16 13:22:00 +08:00
|
|
|
const decipher = crypto.createDecipheriv('aes-128-cbc', pad(key), pad(iv));
|
2017-11-16 12:39:50 +08:00
|
|
|
|
|
|
|
const decryptedBytes = Buffer.concat([decipher.update(cipherTextBuffer), decipher.final()]);
|
|
|
|
|
2017-11-16 11:13:45 +08:00
|
|
|
const digest = decryptedBytes.slice(0, 4);
|
|
|
|
const payload = decryptedBytes.slice(4);
|
|
|
|
|
|
|
|
const computedDigest = shaArray(payload).slice(0, 4);
|
|
|
|
|
|
|
|
if (!arraysIdentical(digest, computedDigest)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload;
|
|
|
|
}
|
|
|
|
|
2019-01-12 06:04:51 +08:00
|
|
|
function decryptString(dataKey, cipherText) {
|
|
|
|
const buffer = decrypt(dataKey, cipherText);
|
2017-11-16 12:39:50 +08:00
|
|
|
|
2017-12-31 09:14:05 +08:00
|
|
|
const str = buffer.toString('utf-8');
|
|
|
|
|
|
|
|
if (str === 'false') {
|
|
|
|
log.error("Could not decrypt string. Buffer: " + buffer);
|
|
|
|
|
|
|
|
throw new Error("Could not decrypt string.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
2017-11-16 12:39:50 +08:00
|
|
|
}
|
|
|
|
|
2017-11-11 11:55:19 +08:00
|
|
|
module.exports = {
|
2017-11-19 01:53:17 +08:00
|
|
|
encrypt,
|
|
|
|
decrypt,
|
2019-01-12 06:04:51 +08:00
|
|
|
decryptString
|
2017-11-11 11:55:19 +08:00
|
|
|
};
|