refactor: cipher
parent
7015287a6f
commit
75a7c91bb1
|
@ -1,6 +1,6 @@
|
||||||
import { cacheCipher } from '@/settings/encryptionSetting'
|
import { cacheCipher } from '@/settings/encryptionSetting'
|
||||||
import type { EncryptionParams } from '@/utils/cipher'
|
import type { EncryptionParams } from '@/utils/cipherOld'
|
||||||
import { AesEncryption } from '@/utils/cipher'
|
import { AesEncryption } from '@/utils/cipherOld'
|
||||||
import { isNullOrUnDef } from '@/utils/is'
|
import { isNullOrUnDef } from '@/utils/is'
|
||||||
|
|
||||||
export interface CreateStorageParams extends EncryptionParams {
|
export interface CreateStorageParams extends EncryptionParams {
|
||||||
|
|
|
@ -1,53 +1,159 @@
|
||||||
import { decrypt, encrypt } from 'crypto-js/aes'
|
import { decrypt as aesDecrypt, encrypt as aesEncrypt } from 'crypto-js/aes'
|
||||||
import UTF8, { parse } from 'crypto-js/enc-utf8'
|
import UTF8, { parse } from 'crypto-js/enc-utf8'
|
||||||
import pkcs7 from 'crypto-js/pad-pkcs7'
|
import pkcs7 from 'crypto-js/pad-pkcs7'
|
||||||
import ECB from 'crypto-js/mode-ecb'
|
import CTR from 'crypto-js/mode-ctr'
|
||||||
import md5 from 'crypto-js/md5'
|
|
||||||
import Base64 from 'crypto-js/enc-base64'
|
import Base64 from 'crypto-js/enc-base64'
|
||||||
|
import MD5 from 'crypto-js/md5'
|
||||||
|
import SHA256 from 'crypto-js/sha256'
|
||||||
|
import SHA512 from 'crypto-js/sha512'
|
||||||
|
|
||||||
|
// Define an interface for encryption
|
||||||
|
// 定义一个加密器的接口
|
||||||
|
export interface Encryption {
|
||||||
|
encrypt(plainText: string): string
|
||||||
|
decrypt(cipherText: string): string
|
||||||
|
}
|
||||||
|
// Define an interface for Hashing
|
||||||
|
// 定义一个哈希算法的接口
|
||||||
|
export interface Hashing {
|
||||||
|
hash(data: string): string
|
||||||
|
}
|
||||||
|
|
||||||
export interface EncryptionParams {
|
export interface EncryptionParams {
|
||||||
key: string
|
key: string
|
||||||
iv: string
|
iv: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export class AesEncryption {
|
class AesEncryption implements Encryption {
|
||||||
private key
|
private readonly key
|
||||||
private iv
|
private readonly iv
|
||||||
|
|
||||||
constructor(opt: Partial<EncryptionParams> = {}) {
|
constructor({ key, iv }: EncryptionParams) {
|
||||||
const { key, iv } = opt
|
this.key = parse(key)
|
||||||
if (key)
|
this.iv = parse(iv)
|
||||||
this.key = parse(key)
|
|
||||||
|
|
||||||
if (iv)
|
|
||||||
this.iv = parse(iv)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get getOptions() {
|
get getOptions() {
|
||||||
return {
|
return {
|
||||||
mode: ECB,
|
mode: CTR,
|
||||||
padding: pkcs7,
|
padding: pkcs7,
|
||||||
iv: this.iv,
|
iv: this.iv,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
encryptByAES(cipherText: string) {
|
encrypt(plainText: string) {
|
||||||
return encrypt(cipherText, this.key, this.getOptions).toString()
|
return aesEncrypt(plainText, this.key, this.getOptions).toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
decryptByAES(cipherText: string) {
|
decrypt(cipherText: string) {
|
||||||
return decrypt(cipherText, this.key, this.getOptions).toString(UTF8)
|
return aesDecrypt(cipherText, this.key, this.getOptions).toString(UTF8)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function encryptByBase64(cipherText: string) {
|
// Define a singleton class for Base64 encryption
|
||||||
return UTF8.parse(cipherText).toString(Base64)
|
class Base64Encryption implements Encryption {
|
||||||
|
private static instance: Base64Encryption
|
||||||
|
|
||||||
|
private constructor() {}
|
||||||
|
|
||||||
|
// Get the singleton instance
|
||||||
|
// 获取单例实例
|
||||||
|
public static getInstance(): Base64Encryption {
|
||||||
|
if (!Base64Encryption.instance)
|
||||||
|
Base64Encryption.instance = new Base64Encryption()
|
||||||
|
|
||||||
|
return Base64Encryption.instance
|
||||||
|
}
|
||||||
|
|
||||||
|
encrypt(plainText: string) {
|
||||||
|
return UTF8.parse(plainText).toString(Base64)
|
||||||
|
}
|
||||||
|
|
||||||
|
decrypt(cipherText: string) {
|
||||||
|
return Base64.parse(cipherText).toString(UTF8)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function decodeByBase64(cipherText: string) {
|
// Define a singleton class for MD5 Hashing
|
||||||
return Base64.parse(cipherText).toString(UTF8)
|
class MD5Hashing implements Hashing {
|
||||||
|
private static instance: MD5Hashing
|
||||||
|
|
||||||
|
private constructor() {}
|
||||||
|
|
||||||
|
// Get the singleton instance
|
||||||
|
// 获取单例实例
|
||||||
|
public static getInstance(): MD5Hashing {
|
||||||
|
if (!MD5Hashing.instance)
|
||||||
|
MD5Hashing.instance = new MD5Hashing()
|
||||||
|
|
||||||
|
return MD5Hashing.instance
|
||||||
|
}
|
||||||
|
|
||||||
|
hash(plainText: string) {
|
||||||
|
return MD5(plainText).toString()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function encryptByMd5(password: string) {
|
// Define a singleton class for SHA256 Hashing
|
||||||
return md5(password).toString()
|
class SHA256Hashing implements Hashing {
|
||||||
|
private static instance: SHA256Hashing
|
||||||
|
|
||||||
|
private constructor() {}
|
||||||
|
|
||||||
|
// Get the singleton instance
|
||||||
|
// 获取单例实例
|
||||||
|
public static getInstance(): SHA256Hashing {
|
||||||
|
if (!SHA256Hashing.instance)
|
||||||
|
SHA256Hashing.instance = new SHA256Hashing()
|
||||||
|
|
||||||
|
return SHA256Hashing.instance
|
||||||
|
}
|
||||||
|
|
||||||
|
hash(plainText: string) {
|
||||||
|
return SHA256(plainText).toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define a singleton class for SHA512 Hashing
|
||||||
|
class SHA512Hashing implements Hashing {
|
||||||
|
private static instance: SHA512Hashing
|
||||||
|
|
||||||
|
private constructor() {}
|
||||||
|
|
||||||
|
// Get the singleton instance
|
||||||
|
// 获取单例实例
|
||||||
|
public static getInstance(): SHA256Hashing {
|
||||||
|
if (!SHA512Hashing.instance)
|
||||||
|
SHA512Hashing.instance = new SHA512Hashing()
|
||||||
|
|
||||||
|
return SHA512Hashing.instance
|
||||||
|
}
|
||||||
|
|
||||||
|
hash(plainText: string) {
|
||||||
|
return SHA512(plainText).toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class EncryptionFactory {
|
||||||
|
public static createAesEncryption(params: EncryptionParams): Encryption {
|
||||||
|
return new AesEncryption(params)
|
||||||
|
}
|
||||||
|
|
||||||
|
public static createBase64Encryption(): Encryption {
|
||||||
|
return Base64Encryption.getInstance()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class HashingFactory {
|
||||||
|
public static createMD5Hashing(): Hashing {
|
||||||
|
return MD5Hashing.getInstance()
|
||||||
|
}
|
||||||
|
|
||||||
|
public static createSHA256Hashing(): Hashing {
|
||||||
|
return SHA256Hashing.getInstance()
|
||||||
|
}
|
||||||
|
|
||||||
|
public static createSHA512Hashing(): Hashing {
|
||||||
|
return SHA512Hashing.getInstance()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
import { decrypt, encrypt } from 'crypto-js/aes'
|
||||||
|
import UTF8, { parse } from 'crypto-js/enc-utf8'
|
||||||
|
import pkcs7 from 'crypto-js/pad-pkcs7'
|
||||||
|
import ECB from 'crypto-js/mode-ecb'
|
||||||
|
import md5 from 'crypto-js/md5'
|
||||||
|
import Base64 from 'crypto-js/enc-base64'
|
||||||
|
|
||||||
|
export interface EncryptionParams {
|
||||||
|
key: string
|
||||||
|
iv: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export class AesEncryption {
|
||||||
|
private key
|
||||||
|
private iv
|
||||||
|
|
||||||
|
constructor(opt: Partial<EncryptionParams> = {}) {
|
||||||
|
const { key, iv } = opt
|
||||||
|
if (key)
|
||||||
|
this.key = parse(key)
|
||||||
|
|
||||||
|
if (iv)
|
||||||
|
this.iv = parse(iv)
|
||||||
|
}
|
||||||
|
|
||||||
|
get getOptions() {
|
||||||
|
return {
|
||||||
|
mode: ECB,
|
||||||
|
padding: pkcs7,
|
||||||
|
iv: this.iv,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
encryptByAES(cipherText: string) {
|
||||||
|
return encrypt(cipherText, this.key, this.getOptions).toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
decryptByAES(cipherText: string) {
|
||||||
|
return decrypt(cipherText, this.key, this.getOptions).toString(UTF8)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function encryptByBase64(cipherText: string) {
|
||||||
|
return UTF8.parse(cipherText).toString(Base64)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function decodeByBase64(cipherText: string) {
|
||||||
|
return Base64.parse(cipherText).toString(UTF8)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function encryptByMd5(password: string) {
|
||||||
|
return md5(password).toString()
|
||||||
|
}
|
Loading…
Reference in New Issue