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