Sanakey
5 天以前 2af71bcf522c485ea005184c977986374a7dcc4a
提交 | 用户 | age
6f7d84 1 import { decrypt as aesDecrypt, encrypt as aesEncrypt } from 'crypto-js/aes';
8e5a6b 2 import UTF8, { parse } from 'crypto-js/enc-utf8';
fcee7d 3 import pkcs7 from 'crypto-js/pad-pkcs7';
6f7d84 4 import CTR from 'crypto-js/mode-ctr';
fcee7d 5 import Base64 from 'crypto-js/enc-base64';
6f7d84 6 import MD5 from 'crypto-js/md5';
KL 7 import SHA256 from 'crypto-js/sha256';
8 import SHA512 from 'crypto-js/sha512';
9
10 // Define an interface for encryption
11 // 定义一个加密器的接口
12 export interface Encryption {
13   encrypt(plainText: string): string;
14   decrypt(cipherText: string): string;
15 }
16 // Define an interface for Hashing
17 // 定义一个哈希算法的接口
18 export interface Hashing {
19   hash(data: string): string;
20 }
fcee7d 21
V 22 export interface EncryptionParams {
23   key: string;
24   iv: string;
25 }
26
6f7d84 27 class AesEncryption implements Encryption {
KL 28   private readonly key;
29   private readonly iv;
fcee7d 30
6f7d84 31   constructor({ key, iv }: EncryptionParams) {
KL 32     this.key = parse(key);
33     this.iv = parse(iv);
fcee7d 34   }
V 35
36   get getOptions() {
37     return {
6f7d84 38       mode: CTR,
fcee7d 39       padding: pkcs7,
V 40       iv: this.iv,
41     };
42   }
43
6f7d84 44   encrypt(plainText: string) {
KL 45     return aesEncrypt(plainText, this.key, this.getOptions).toString();
fcee7d 46   }
V 47
6f7d84 48   decrypt(cipherText: string) {
KL 49     return aesDecrypt(cipherText, this.key, this.getOptions).toString(UTF8);
fcee7d 50   }
V 51 }
52
6f7d84 53 // Define a singleton class for Base64 encryption
KL 54 class Base64Encryption implements Encryption {
55   private static instance: Base64Encryption;
56
57   private constructor() {}
58
59   // Get the singleton instance
60   // 获取单例实例
61   public static getInstance(): Base64Encryption {
62     if (!Base64Encryption.instance) {
63       Base64Encryption.instance = new Base64Encryption();
64     }
65     return Base64Encryption.instance;
66   }
67
68   encrypt(plainText: string) {
69     return UTF8.parse(plainText).toString(Base64);
70   }
71
72   decrypt(cipherText: string) {
73     return Base64.parse(cipherText).toString(UTF8);
74   }
fcee7d 75 }
V 76
6f7d84 77 // Define a singleton class for MD5 Hashing
KL 78 class MD5Hashing implements Hashing {
79   private static instance: MD5Hashing;
80
81   private constructor() {}
82
83   // Get the singleton instance
84   // 获取单例实例
85   public static getInstance(): MD5Hashing {
86     if (!MD5Hashing.instance) {
87       MD5Hashing.instance = new MD5Hashing();
88     }
89     return MD5Hashing.instance;
90   }
91
92   hash(plainText: string) {
93     return MD5(plainText).toString();
94   }
fcee7d 95 }
V 96
6f7d84 97 // Define a singleton class for SHA256 Hashing
KL 98 class SHA256Hashing implements Hashing {
99   private static instance: SHA256Hashing;
100
101   private constructor() {}
102
103   // Get the singleton instance
104   // 获取单例实例
105   public static getInstance(): SHA256Hashing {
106     if (!SHA256Hashing.instance) {
107       SHA256Hashing.instance = new SHA256Hashing();
108     }
109     return SHA256Hashing.instance;
110   }
111
112   hash(plainText: string) {
113     return SHA256(plainText).toString();
114   }
115 }
116
117 // Define a singleton class for SHA512 Hashing
118 class SHA512Hashing implements Hashing {
119   private static instance: SHA512Hashing;
120
121   private constructor() {}
122
123   // Get the singleton instance
124   // 获取单例实例
125   public static getInstance(): SHA256Hashing {
126     if (!SHA512Hashing.instance) {
127       SHA512Hashing.instance = new SHA512Hashing();
128     }
129     return SHA512Hashing.instance;
130   }
131
132   hash(plainText: string) {
133     return SHA512(plainText).toString();
134   }
135 }
136
137 export class EncryptionFactory {
138   public static createAesEncryption(params: EncryptionParams): Encryption {
139     return new AesEncryption(params);
140   }
141
142   public static createBase64Encryption(): Encryption {
143     return Base64Encryption.getInstance();
144   }
145 }
146
147 export class HashingFactory {
148   public static createMD5Hashing(): Hashing {
149     return MD5Hashing.getInstance();
150   }
151
152   public static createSHA256Hashing(): Hashing {
153     return SHA256Hashing.getInstance();
154   }
155
156   public static createSHA512Hashing(): Hashing {
157     return SHA512Hashing.getInstance();
158   }
fcee7d 159 }