Преглед на файлове

增加加密解密模块

zhusiqing преди 4 години
родител
ревизия
6a68d46c52
променени са 4 файла, в които са добавени 24 реда и са изтрити 7 реда
  1. 0 3
      src/app.ts
  2. 3 3
      src/controllers/document.ts
  3. 0 1
      src/middlewares/limit.ts
  4. 21 0
      src/utils/cipher.ts

+ 0 - 3
src/app.ts

@@ -58,9 +58,6 @@ app.use(koaProxy('/proxy', {
   rewrite: path => path.replace(/^\/proxy(\/|\/\w+)?$/, '/')
 }))
 
-
-
-
 app.on('error', (err, ctx: Context) => {
   ctx.$response(err, 'error', false);
   const errMsg: string = err.message || '服务出错';

+ 3 - 3
src/controllers/document.ts

@@ -33,9 +33,9 @@ export const putDocument = async (ctx: Context) => {
   ctx.body = ctx.$response(data.data, data.message, data.success, data.code)
 };
 
-export const getDocument = async (ctx:Context) => {
-  const { id } = ctx.query
-}
+// export const getDocument = async (ctx:Context) => {
+//   const { id } = ctx.query
+// }
 
 export default {
   putDocument

+ 0 - 1
src/middlewares/limit.ts

@@ -10,7 +10,6 @@ export default () => {
     }
     const ip: string = ctx.ip;
     const key: string = `client_ip_limit_${ip}`;
-    const a = await ctx.$redis.get(key)
     const limit: number = Number(await ctx.$redis.get(key)) || 0;
     if (limit > config.limit.times) {
       ctx.throw(403, '请求频次过高');

+ 21 - 0
src/utils/cipher.ts

@@ -0,0 +1,21 @@
+import { createCipheriv, createDecipheriv, scryptSync } from 'crypto';
+import config from '@config';
+
+const algorithm = 'aes-192-cbc';
+const salt = '123';
+const key = scryptSync(config.secrestKey, salt, 24);
+const iv = Buffer.alloc(16, '11')
+
+export const cipher = (data: string) => {
+  const cipher = createCipheriv(algorithm, key, iv);
+  let enc = cipher.update(data, 'utf8', 'hex');
+  enc += cipher.final('hex');
+  return enc;
+};
+
+export const decipher = (data: string) => {
+  const decipher = createDecipheriv(algorithm, key, iv);
+  let dec = decipher.update(data, 'hex', 'utf8');
+  dec += decipher.final('utf8');
+  return dec
+}