-
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
23 changed files
with
170 additions
and
118 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,2 @@ | ||
export * from './secure-store'; | ||
export * from './query-keys'; |
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 @@ | ||
export const GENERATED_KEYS = 'generatedKeys'; |
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,5 @@ | ||
export const SESSION = 'session'; | ||
export const LOCAL = 'local'; | ||
|
||
export const SESSION_DATA = 'sessionData'; | ||
export const PASSWORD_WITH_DEVICE_KEY = 'passwordWithDeviceKey'; |
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,63 +1,77 @@ | ||
import type { SecureData } from '$types'; | ||
|
||
function hexStringToArrayBuffer(hexString: string) { | ||
if (hexString.length % 2 !== 0) { | ||
throw new Error('Invalid hexString length. It must be even.'); | ||
} | ||
const bytes = new Uint8Array(hexString.length / 2); | ||
for (let i = 0; i < bytes.length; i++) { | ||
bytes[i] = parseInt(hexString.slice(i * 2, i * 2 + 2), 16); | ||
} | ||
return bytes.buffer; | ||
} | ||
|
||
function arrayBufferToBase64(buffer: ArrayBuffer): string { | ||
return btoa(String.fromCharCode(...new Uint8Array(buffer))); | ||
} | ||
|
||
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; | ||
} | ||
|
||
function arrayBufferToHexString(buffer: ArrayBuffer) { | ||
const byteArray = new Uint8Array(buffer); | ||
let hexString = ''; | ||
byteArray.forEach(function (byte) { | ||
hexString += ('0' + byte.toString(16)).slice(-2); | ||
}); | ||
return hexString; | ||
} | ||
|
||
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 algo = { 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; | ||
return arrayBufferToHexString(hashBuffer); // Convert ArrayBuffer to hex string | ||
} | ||
|
||
export async function encryptData( | ||
secretData: Uint8Array, | ||
passwordHash: string | ||
passwordHashHex: string // Accept hex 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 passwordHash = hexStringToArrayBuffer(passwordHashHex); | ||
console.log('Imported key length (bytes):', passwordHash.byteLength); | ||
const key = await crypto.subtle.importKey('raw', passwordHash, algo, false, ['encrypt']); | ||
|
||
const encrypted = await crypto.subtle.encrypt(algo, key, secretData); | ||
return { | ||
encryptedData: new Uint8Array(encrypted), | ||
iv | ||
encryptedData: arrayBufferToBase64(encrypted), | ||
iv: arrayBufferToBase64(iv) | ||
}; | ||
} | ||
|
||
export async function decryptData( | ||
encryptedData: SecureData, | ||
passwordHash: string | ||
passwordHashHex: string // Accept hex string | ||
): Promise<Uint8Array> { | ||
const algo: AesGcmParams = { name: 'AES-GCM', iv: encryptedData.iv }; | ||
const key = await crypto.subtle.importKey( | ||
'raw', | ||
new TextEncoder().encode(passwordHash), | ||
const iv = base64ToArrayBuffer(encryptedData.iv); | ||
const algo = { name: 'AES-GCM', iv }; | ||
const passwordHash = hexStringToArrayBuffer(passwordHashHex); // Convert hex string to ArrayBuffer | ||
const key = await crypto.subtle.importKey('raw', passwordHash, algo, false, ['decrypt']); | ||
|
||
const decrypted = await crypto.subtle.decrypt( | ||
algo, | ||
false, | ||
['decrypt'] | ||
key, | ||
base64ToArrayBuffer(encryptedData.encryptedData) | ||
); | ||
|
||
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,2 +1 @@ | ||
export * from './app-queries'; | ||
export * from './query-keys'; |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
export * from './passphrase'; | ||
export * from './password-exist'; | ||
export * from './password'; | ||
export * from './const'; | ||
export * from './keys-store'; | ||
export * from './session'; |
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 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
Oops, something went wrong.