44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
// src/utils/encryption.js
|
||
import { sm2, sm3, sm4 } from 'sm-crypto'
|
||
// 配置项,例如盐值、SM2 公私钥、SM4 密钥
|
||
import { SM_CONFIG } from './configure'
|
||
|
||
|
||
// SM3 哈希
|
||
export function hashSM3(text) {
|
||
// 对数据进行哈希计算
|
||
return sm3(text)
|
||
}
|
||
|
||
// 使用 SM3 进行哈希并加入盐值
|
||
export function hashWithSM3AndSalt(text) {
|
||
// 将文本和盐值拼接在一起
|
||
const textWithSalt = SM_CONFIG.SALT + text
|
||
// 使用 SM3 进行哈希
|
||
return hashSM3(textWithSalt)
|
||
}
|
||
|
||
// SM2 加密
|
||
export function encryptWithSM2(text) {
|
||
// SM2 公钥加密
|
||
return sm2.doEncrypt(text, SM_CONFIG.SM2_PUBLIC_KEY)
|
||
}
|
||
|
||
// SM2 解密
|
||
export function decryptWithSM2(encryptedText) {
|
||
// SM2 私钥解密
|
||
return sm2.doDecrypt(encryptedText, SM_CONFIG.SM2_PRIVATE_KEY)
|
||
}
|
||
|
||
// SM4 加密
|
||
export function encryptWithSM4(text) {
|
||
// SM4 对称加密,ECB 模式
|
||
return sm4.encrypt(text, SM_CONFIG.SM4_KEY)
|
||
}
|
||
|
||
// SM4 解密
|
||
export function decryptWithSM4(encryptedText) {
|
||
// SM4 对称解密,ECB 模式
|
||
return sm4.decrypt(encryptedText, SM_CONFIG.SM4_KEY)
|
||
}
|