UNPKG

@hazae41/glacier

Version:

Yet another React data (re)fetching library

45 lines (42 loc) 1.82 kB
import { __addDisposableResource, __disposeResources } from '../../../../../node_modules/tslib/tslib.es6.mjs'; import { Base64 } from '@hazae41/base64'; import { Bytes } from '@hazae41/bytes'; class AesGcmBicoder { key; constructor(key) { this.key = key; } async encryptOrThrow(plain, iv) { return new Uint8Array(await crypto.subtle.encrypt({ name: "AES-GCM", iv }, this.key, plain)); } async encodeOrThrow(input) { const ivBytes = Bytes.random(12); const ivBase64 = Base64.get().getOrThrow().encodePaddedOrThrow(ivBytes); const plainBytes = Bytes.fromUtf8(input); const cipherBytes = await this.encryptOrThrow(plainBytes, ivBytes); const cipherBase64 = Base64.get().getOrThrow().encodePaddedOrThrow(cipherBytes); return `${ivBase64}.${cipherBase64}`; } async decryptOrThrow(cipher, iv) { return new Uint8Array(await crypto.subtle.decrypt({ name: "AES-GCM", iv }, this.key, cipher)); } async decodeOrThrow(output) { const env_1 = { stack: [], error: void 0, hasError: false }; try { const [ivBase64, cipherBase64] = output.split("."); const ivMemory = __addDisposableResource(env_1, Base64.get().getOrThrow().decodePaddedOrThrow(ivBase64), false); const cipherMemory = __addDisposableResource(env_1, Base64.get().getOrThrow().decodePaddedOrThrow(cipherBase64), false); const plainBytes = await this.decryptOrThrow(cipherMemory.bytes, ivMemory.bytes); return Bytes.toUtf8(plainBytes); } catch (e_1) { env_1.error = e_1; env_1.hasError = true; } finally { __disposeResources(env_1); } } } export { AesGcmBicoder }; //# sourceMappingURL=gcm.mjs.map