Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F32167703
D6995.1765053662.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
D6995.1765053662.diff
View Options
diff --git a/web/database/utils/worker-crypto-utils.js b/web/database/utils/worker-crypto-utils.js
new file mode 100644
--- /dev/null
+++ b/web/database/utils/worker-crypto-utils.js
@@ -0,0 +1,60 @@
+// @flow
+
+import type { AesGcmParams, Crypto, CryptoKey } from './crypto-types.js';
+
+declare var crypto: Crypto;
+
+const ENCRYPTION_ALGORITHM = 'AES-GCM';
+
+type EncryptionResult = {
+ iv: BufferSource,
+ cipher: ArrayBuffer,
+};
+
+function generateCryptoKey(): Promise<CryptoKey> {
+ return crypto.subtle.generateKey(
+ {
+ name: ENCRYPTION_ALGORITHM,
+ length: 256,
+ },
+ false,
+ ['encrypt', 'decrypt'],
+ );
+}
+
+function generateIv(): BufferSource {
+ return crypto.getRandomValues(new Uint8Array(12));
+}
+
+async function encrypt(
+ data: ArrayBuffer,
+ key: CryptoKey,
+): Promise<EncryptionResult> {
+ const iv = generateIv();
+ const params: AesGcmParams = {
+ name: ENCRYPTION_ALGORITHM,
+ iv: iv,
+ };
+ const cipher = await crypto.subtle.encrypt(params, key, data);
+ return {
+ cipher,
+ iv,
+ };
+}
+
+async function decrypt(
+ cipher: ArrayBuffer,
+ key: CryptoKey,
+ iv: BufferSource,
+): Promise<ArrayBuffer> {
+ return crypto.subtle.decrypt(
+ {
+ name: ENCRYPTION_ALGORITHM,
+ iv,
+ },
+ key,
+ cipher,
+ );
+}
+
+export { generateCryptoKey, encrypt, decrypt };
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Dec 6, 8:41 PM (1 h, 33 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5840699
Default Alt Text
D6995.1765053662.diff (1 KB)
Attached To
Mode
D6995: [web-db] implement crypto functions
Attached
Detach File
Event Timeline
Log In to Comment