From 4919f028c884a041da7ff098abb02389b4eac598 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Tue, 18 Apr 2023 03:02:17 +0800 Subject: ✨add envshare docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/encryption.ts | 51 --------------------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 pkg/encryption.ts (limited to 'pkg/encryption.ts') diff --git a/pkg/encryption.ts b/pkg/encryption.ts deleted file mode 100644 index c9f0e9d..0000000 --- a/pkg/encryption.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { fromBase58 } from "../util/base58"; - -export async function generateKey() { - return await crypto.subtle.generateKey( - { - name: "AES-GCM", - length: 128, - }, - true, - ["encrypt", "decrypt"], - ); -} - -export async function encrypt(text: string): Promise<{ encrypted: Uint8Array; iv: Uint8Array; key: Uint8Array }> { - const key = await generateKey(); - - const iv = crypto.getRandomValues(new Uint8Array(16)); - - const encryptedBuffer = await crypto.subtle.encrypt( - { - name: "AES-GCM", - iv, - }, - key, - new TextEncoder().encode(text), - ); - - const exportedKey = await crypto.subtle.exportKey("raw", key); - return { - encrypted: new Uint8Array(encryptedBuffer), - key: new Uint8Array(exportedKey), - iv, - }; -} - -export async function decrypt(encrypted: string, keyData: Uint8Array, iv: string, keyVersion: number): Promise { - const algorithm = keyVersion === 1 ? "AES-CBC" : "AES-GCM"; - - const key = await crypto.subtle.importKey("raw", keyData, { name: algorithm, length: 128 }, false, ["decrypt"]); - - const decrypted = await crypto.subtle.decrypt( - { - name: algorithm, - iv: fromBase58(iv), - }, - key, - fromBase58(encrypted), - ); - - return new TextDecoder().decode(decrypted); -} -- cgit v1.2.3-70-g09d2