irc_dicom_service/app/middleware/encode.js

44 lines
1.0 KiB
JavaScript

"use strict";
const crypto = require('crypto');
const zlib = require('zlib');
module.exports = (option, app) => {
function getkey() {
let time = app.moment().format("YYYY-MM-DD");
return app.moment(time).valueOf();
}
return async function (ctx, next) {
let tmpData = '';
try {
await next();
let data = JSON.stringify(ctx.body);
tmpData = data;
ctx.body = aesDecrypt(data, 'rayplus_miyao_' + getkey());
} catch (err) {
ctx.body = JSON.parse(tmpData);
}
};
};
function aesEncrypt(data, key) {
const cipher = crypto.createCipher('aes192', key);
var crypted = cipher.update(data, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;
}
function aesDecrypt(encrypted, key) {
const decipher = crypto.createDecipher('aes192', key);
var decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
function gzip(data) {
return zlib.gzipSync(buf);
}
function gunzip(buf) {
return zlib.gunzipSync(buf);
}