-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(connect): HAWNG-441 encrypt session storage
- Loading branch information
Showing
10 changed files
with
123 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { decrypt, encrypt, generateKey } from './crypto' | ||
|
||
jest.mock('@thumbmarkjs/thumbmarkjs', () => ({ | ||
getFingerprint: jest.fn(() => '123abc'), | ||
})) | ||
|
||
describe('crypto', () => { | ||
test('generateKey, encrypt, and decrypt', async () => { | ||
const salt = window.crypto.getRandomValues(new Uint8Array(16)) | ||
const key = await generateKey(salt) | ||
expect(key).not.toBeNull() | ||
expect(key.algorithm).toEqual({ name: 'AES-GCM', length: 256 }) | ||
const text = 'test' | ||
const encrypted = await encrypt(key, text) | ||
expect(encrypted).not.toEqual(text) | ||
const decrypted = await decrypt(key, encrypted) | ||
expect(decrypted).toEqual(text) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { getFingerprint } from '@thumbmarkjs/thumbmarkjs' | ||
|
||
export async function generateKey(salt: ArrayBufferView): Promise<CryptoKey> { | ||
const fingerprint = await getFingerprint() | ||
const data = new TextEncoder().encode(fingerprint) | ||
const key = await window.crypto.subtle.importKey('raw', data, { name: 'PBKDF2' }, false, ['deriveKey']) | ||
const algorithm = { | ||
name: 'PBKDF2', | ||
salt, | ||
iterations: 100000, | ||
hash: 'SHA-256', | ||
} | ||
const keyType = { | ||
name: 'AES-GCM', | ||
length: 256, | ||
} | ||
return window.crypto.subtle.deriveKey(algorithm, key, keyType, true, ['encrypt', 'decrypt']) | ||
} | ||
|
||
export function toBase64(data: Uint8Array): string { | ||
return window.btoa(String.fromCharCode(...Array.from(data))) | ||
} | ||
|
||
export function toByteArray(data: string): Uint8Array { | ||
return new Uint8Array(Array.from(window.atob(data)).map(c => c.charCodeAt(0))) | ||
} | ||
|
||
export async function encrypt(key: CryptoKey, data: string): Promise<string> { | ||
const iv = window.crypto.getRandomValues(new Uint8Array(12)) | ||
const encodedData = new TextEncoder().encode(data) | ||
const encrypted = await window.crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, encodedData) | ||
return toBase64(iv) + '.' + toBase64(new Uint8Array(encrypted)) | ||
} | ||
|
||
export async function decrypt(key: CryptoKey, data: string): Promise<string> { | ||
const iv = toByteArray(data.split('.')[0] ?? '') | ||
const encrypted = toByteArray(data.split('.')[1] ?? '') | ||
const decrypted = await window.crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, encrypted) | ||
return new TextDecoder('utf-8').decode(new Uint8Array(decrypted)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters