feat(crypto): 新增加密工具类,封装 RSA 密钥生成、Base64 编解码并预留 AES 加解密接口
This commit is contained in:
@@ -0,0 +1,82 @@
|
|||||||
|
import { createRsaPair, base64Decode, base64Encode, } from "@/uni_modules/laoqianjunzi-crypto";
|
||||||
|
|
||||||
|
// #ifdef H5
|
||||||
|
type CryptoRsaPair = {
|
||||||
|
publicKey : string;
|
||||||
|
privateKey : string;
|
||||||
|
}
|
||||||
|
// #endif
|
||||||
|
// #ifdef APP-ANDROID
|
||||||
|
import { CryptoRsaPair } from "@/uni_modules/laoqianjunzi-crypto";
|
||||||
|
// #endif
|
||||||
|
|
||||||
|
export class Crypto {
|
||||||
|
/**
|
||||||
|
* 随机生成32位的字符串
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
static generateRandomString() : string {
|
||||||
|
const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
let result = ""
|
||||||
|
for (let i = 0; i < 32; i++) {
|
||||||
|
result += chars.charAt(
|
||||||
|
Math.floor(
|
||||||
|
Math.random() * chars.length
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static readonly rsaBitsText = ref('2048')
|
||||||
|
static currentRsaBits() : number {
|
||||||
|
const parsed = parseInt(Crypto.rsaBitsText.value)
|
||||||
|
if (parsed == 1024 || parsed == 2048 || parsed == 3072 || parsed == 4096) {
|
||||||
|
return parsed
|
||||||
|
}
|
||||||
|
return 2048
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 随机生成aes 密钥
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
static generateAesKey() : CryptoRsaPair {
|
||||||
|
return createRsaPair(Crypto.currentRsaBits());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加密base64
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
static encryptBase64(str : CryptoRsaPair) : string {
|
||||||
|
return base64Encode(str.privateKey + str.publicKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解密base64
|
||||||
|
*/
|
||||||
|
static decryptBase64(str : string) : string {
|
||||||
|
return base64Decode(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用密钥对数据进行加密
|
||||||
|
* @param message
|
||||||
|
* @param aesKey
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
static encryptWithAes() : string {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用密钥对数据进行解密
|
||||||
|
* @param message
|
||||||
|
* @param aesKey
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
static decryptWithAes() : string {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user