From c87a3b9f9f7280cab3edae153049c5e7e9c49bff Mon Sep 17 00:00:00 2001 From: xiaozhi <1822691309@qq.com> Date: Sun, 7 Jun 2026 11:11:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(crypto):=20=E6=96=B0=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=E5=B7=A5=E5=85=B7=E7=B1=BB=EF=BC=8C=E5=B0=81=E8=A3=85?= =?UTF-8?q?=20RSA=20=E5=AF=86=E9=92=A5=E7=94=9F=E6=88=90=E3=80=81Base64=20?= =?UTF-8?q?=E7=BC=96=E8=A7=A3=E7=A0=81=E5=B9=B6=E9=A2=84=E7=95=99=20AES=20?= =?UTF-8?q?=E5=8A=A0=E8=A7=A3=E5=AF=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/crypto.uts | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 utils/crypto.uts diff --git a/utils/crypto.uts b/utils/crypto.uts new file mode 100644 index 0000000..e8b6018 --- /dev/null +++ b/utils/crypto.uts @@ -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 ''; + } + +} \ No newline at end of file