@bitblit/ratchet-common 5.1.657-alpha → 5.1.658-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class SimpleEncryptionRatchet {
|
|
2
|
+
private ivLength;
|
|
3
|
+
private sharedKey;
|
|
4
|
+
constructor(sharedRawKey: string | Promise<string>, ivLength?: number);
|
|
5
|
+
private strToBuf;
|
|
6
|
+
private bufToBase64;
|
|
7
|
+
private base64ToBuf;
|
|
8
|
+
encrypt(data: string): Promise<string>;
|
|
9
|
+
decrypt(encryptedValue: string): Promise<string>;
|
|
10
|
+
private createSharedKey;
|
|
11
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { RequireRatchet } from "@bitblit/ratchet-common/lang/require-ratchet";
|
|
2
|
+
import { NumberRatchet } from '@bitblit/ratchet-common/lang/number-ratchet';
|
|
3
|
+
export class SimpleEncryptionRatchet {
|
|
4
|
+
ivLength;
|
|
5
|
+
sharedKey;
|
|
6
|
+
constructor(sharedRawKey, ivLength = 12) {
|
|
7
|
+
this.ivLength = ivLength;
|
|
8
|
+
RequireRatchet.notNullOrUndefined(sharedRawKey);
|
|
9
|
+
RequireRatchet.true(ivLength >= 12, 'ivLength must be at least 12');
|
|
10
|
+
this.sharedKey = this.createSharedKey(sharedRawKey);
|
|
11
|
+
}
|
|
12
|
+
strToBuf(str) {
|
|
13
|
+
return new TextEncoder().encode(str);
|
|
14
|
+
}
|
|
15
|
+
bufToBase64(buf) {
|
|
16
|
+
return btoa(String.fromCharCode(...new Uint8Array(buf)));
|
|
17
|
+
}
|
|
18
|
+
base64ToBuf(base64) {
|
|
19
|
+
return new Uint8Array(atob(base64).split('').map(c => c.charCodeAt(0)));
|
|
20
|
+
}
|
|
21
|
+
async encrypt(data) {
|
|
22
|
+
const iv = crypto.getRandomValues(new Uint8Array(this.ivLength));
|
|
23
|
+
const encoded = this.strToBuf(data);
|
|
24
|
+
const key = await this.sharedKey;
|
|
25
|
+
const ciphertext = await crypto.subtle.encrypt({ name: "AES-GCM", iv }, key, encoded);
|
|
26
|
+
const ivMsg = this.bufToBase64(iv);
|
|
27
|
+
const dataMsg = this.bufToBase64(ciphertext);
|
|
28
|
+
return ivMsg.length + 'K' + ivMsg + dataMsg;
|
|
29
|
+
}
|
|
30
|
+
async decrypt(encryptedValue) {
|
|
31
|
+
const split = encryptedValue?.indexOf('K');
|
|
32
|
+
if (!split || split < 1) {
|
|
33
|
+
throw new Error('Invalid split : ' + split);
|
|
34
|
+
}
|
|
35
|
+
const ivLen = NumberRatchet.safeNumber(encryptedValue.substring(0, split));
|
|
36
|
+
const iv = encryptedValue.substring(split + 1, split + 1 + ivLen);
|
|
37
|
+
const data = encryptedValue.substring(split + 1 + ivLen);
|
|
38
|
+
const key = await this.sharedKey;
|
|
39
|
+
const decrypted = await crypto.subtle.decrypt({ name: "AES-GCM", iv: this.base64ToBuf(iv) }, key, this.base64ToBuf(data));
|
|
40
|
+
return new TextDecoder().decode(decrypted);
|
|
41
|
+
}
|
|
42
|
+
async createSharedKey(rawKeyIn) {
|
|
43
|
+
const rawKey = typeof rawKeyIn === 'string' ? rawKeyIn : await rawKeyIn;
|
|
44
|
+
const keyMaterial = this.strToBuf(rawKey.padEnd(32, '0').slice(0, 32));
|
|
45
|
+
return crypto.subtle.importKey("raw", keyMaterial, "AES-GCM", false, ["encrypt", "decrypt"]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=simple-encryption-ratchet.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simple-encryption-ratchet.js","sourceRoot":"","sources":["../../src/lang/simple-encryption-ratchet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,8CAA8C,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,6CAA6C,CAAC;AAO5E,MAAM,OAAO,uBAAuB;I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