-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
138 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { SecureData } from '$types'; | ||
|
||
export async function hashPassword(password: string): Promise<string> { | ||
const encoder = new TextEncoder(); | ||
const data = encoder.encode(password); | ||
const algo: { name: string } = { name: 'SHA-256' }; | ||
const hashBuffer = await crypto.subtle.digest(algo, data); | ||
const hashArray = Array.from(new Uint8Array(hashBuffer)); | ||
const hashHex = hashArray.map((byte) => byte.toString(16).padStart(2, '0')).join(''); | ||
return hashHex; | ||
} | ||
|
||
export async function encryptData( | ||
secretData: Uint8Array, | ||
passwordHash: string | ||
): Promise<SecureData> { | ||
const iv = crypto.getRandomValues(new Uint8Array(12)); | ||
const algo: AesGcmParams = { name: 'AES-GCM', iv }; | ||
const key = await crypto.subtle.importKey( | ||
'raw', | ||
new TextEncoder().encode(passwordHash), | ||
algo, | ||
false, | ||
['encrypt'] | ||
); | ||
|
||
const encrypted = await crypto.subtle.encrypt(algo, key, secretData); | ||
return { | ||
encryptedData: new Uint8Array(encrypted), | ||
iv | ||
}; | ||
} | ||
|
||
export async function decryptData( | ||
encryptedData: SecureData, | ||
passwordHash: string | ||
): Promise<Uint8Array> { | ||
const algo: AesGcmParams = { name: 'AES-GCM', iv: encryptedData.iv }; | ||
const key = await crypto.subtle.importKey( | ||
'raw', | ||
new TextEncoder().encode(passwordHash), | ||
algo, | ||
false, | ||
['decrypt'] | ||
); | ||
|
||
const decrypted = await crypto.subtle.decrypt(algo, key, encryptedData.encryptedData); | ||
return new Uint8Array(decrypted); | ||
} | ||
|
||
export function arrayBufferToBase64(buffer: ArrayBuffer): string { | ||
return btoa(String.fromCharCode(...new Uint8Array(buffer))); | ||
} | ||
|
||
export function base64ToArrayBuffer(base64: string): ArrayBuffer { | ||
const binary_string = atob(base64); | ||
const len = binary_string.length; | ||
const bytes = new Uint8Array(len); | ||
for (let i = 0; i < len; i++) { | ||
bytes[i] = binary_string.charCodeAt(i); | ||
} | ||
return bytes.buffer; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './navigation'; | ||
export * from './other'; | ||
export * from './password'; | ||
export * from './encryption'; | ||
export * from './auth'; |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export const isChromeDefined = () => typeof chrome !== 'undefined'; | ||
|
||
export const isChromeStorageSafe = () => | ||
typeof chrome !== 'undefined' && chrome.storage && chrome.storage.session; | ||
isChromeDefined() && chrome.storage && chrome.storage.session; |
This file was deleted.
Oops, something went wrong.
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