Skip to content

Commit

Permalink
regenerate key if previous key is corrupted
Browse files Browse the repository at this point in the history
  • Loading branch information
kamranasad7 committed Apr 2, 2024
1 parent dc3f9c4 commit 93f8b73
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions lib/ReactiveStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class Crypto {
#key: CryptoKey | null;
enc = new TextEncoder();
dec = new TextDecoder();
static #iv = new Uint8Array([-26,100,-118,-90,-29,56,24,-66,-18,-107,-24,109,-100,-34,-102,-72,-32]);

static #iv = new Uint8Array([-26, 100, -118, -90, -29, 56, 24, -66, -18, -107, -24, 109, -100, -34, -102, -72, -32]);
static #keyType = { name: 'AES-GCM', length: 256 };
static #algorithm = { name: 'AES-GCM', iv: this.#iv };
static #wrappingKeyBuffer = new Uint8Array([81,-21,31,-34,109,47,-19,122,-31,-45,127,-83,10,-15,126,-68,99,-6,-87,54,-24,107,-44,111,124,-36,-102,109,-108,120,63,57]);
static #wrappingKeyBuffer = new Uint8Array([81, -21, 31, -34, 109, 47, -19, 122, -31, -45, 127, -83, 10, -15, 126, -68, 99, -6, -87, 54, -24, 107, -44, 111, 124, -36, -102, 109, -108, 120, 63, 57]);
static #wrappingKey: CryptoKey | null = null;

private constructor(key: CryptoKey | null) {
Expand All @@ -39,33 +39,43 @@ class Crypto {
const dc = await crypto.subtle.decrypt(Crypto.#algorithm, this.#key, parsedBuffer.buffer);
return this.dec.decode(dc);
}
catch(e) {
catch (e) {
return 'null';
}
}
else return str;
}

static async generateKey(wrappingKey: CryptoKey) {
const cryptoKey = await crypto.subtle.generateKey(this.#keyType, true, ['encrypt', 'decrypt']);
const wrappedKey = await crypto.subtle.wrapKey('raw', cryptoKey, wrappingKey, this.#algorithm);
localStorage.setItem('secure_reactive_storage', JSON.stringify(Array.from(new Int8Array(wrappedKey))));
return cryptoKey;
}

static async createInstance() {
if (crypto) {
let cryptoKey: CryptoKey | null = null;
this.#wrappingKey = await crypto.subtle.importKey('raw', this.#wrappingKeyBuffer, this.#algorithm.name, false, ['wrapKey', 'unwrapKey'])

const currentKey = localStorage.getItem('secure_reactive_storage');
if (currentKey) {
const keyBuffer = new Int8Array(JSON.parse(currentKey))
cryptoKey = await crypto.subtle.unwrapKey('raw', keyBuffer, this.#wrappingKey, this.#algorithm , this.#algorithm.name, true, ['encrypt', 'decrypt']);
try {
const keyBuffer = new Int8Array(JSON.parse(currentKey))
cryptoKey = await crypto.subtle.unwrapKey('raw', keyBuffer, this.#wrappingKey, this.#algorithm, this.#algorithm.name, true, ['encrypt', 'decrypt']);
} catch {
cryptoKey = await this.generateKey(this.#wrappingKey);
}
}
else {
cryptoKey = await crypto.subtle.generateKey(this.#keyType, true, ['encrypt', 'decrypt']);
const wrappedKey = await crypto.subtle.wrapKey('raw', cryptoKey, this.#wrappingKey, this.#algorithm);
localStorage.setItem('secure_reactive_storage', JSON.stringify(Array.from(new Int8Array(wrappedKey))));
cryptoKey = await this.generateKey(this.#wrappingKey);
}

return new Crypto(cryptoKey as CryptoKey);
}
else return new Crypto(null);
}
}

const encryption = await Crypto.createInstance();
export { encryption };
export { encryption };

0 comments on commit 93f8b73

Please sign in to comment.