Skip to content

Commit 29cf11e

Browse files
committed
fix(mobile): replace all arrayBufferToBase64 usage with node-forge
1 parent 588df9c commit 29cf11e

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

apps/mobile/v1/src/lib/encryption/EncryptionService.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
*/
55

66
import * as Crypto from 'expo-crypto';
7+
import forge from 'node-forge';
78
import { EncryptedNote, DecryptedData, PotentiallyEncrypted } from './types';
89
import { ENCRYPTION_CONFIG } from './config';
910
import { encryptWithAESGCM, decryptWithAESGCM } from './core/aes';
1011
import { deriveEncryptionKey } from './core/keyDerivation';
11-
import { arrayBufferToBase64 } from './core/crypto';
1212
import { getUserSecret, getMasterKey, clearUserStorageData } from './storage/secureStorage';
1313
import { DecryptionCache } from './storage/cache';
1414

@@ -61,9 +61,11 @@ export class MobileEncryptionService {
6161
const saltBytes = await Crypto.getRandomBytesAsync(ENCRYPTION_CONFIG.SALT_LENGTH);
6262
const ivBytes = await Crypto.getRandomBytesAsync(ENCRYPTION_CONFIG.IV_LENGTH);
6363

64-
// Convert to base64
65-
const saltBase64 = arrayBufferToBase64(saltBytes);
66-
const ivBase64 = arrayBufferToBase64(ivBytes);
64+
// Convert Uint8Array to base64 using node-forge
65+
const saltString = String.fromCharCode.apply(null, Array.from(saltBytes));
66+
const ivString = String.fromCharCode.apply(null, Array.from(ivBytes));
67+
const saltBase64 = forge.util.encode64(saltString);
68+
const ivBase64 = forge.util.encode64(ivString);
6769

6870
// Derive encryption key
6971
const key = await this.deriveKey(userId, saltBase64);

apps/mobile/v1/src/lib/encryption/storage/cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { CACHE_CONFIG } from '../config';
88

99
export class DecryptionCache {
1010
private cache = new Map<string, CacheEntry>();
11-
private cleanupInterval: NodeJS.Timeout;
11+
private cleanupInterval: ReturnType<typeof setInterval>;
1212

1313
constructor() {
1414
// Clean expired cache every 5 minutes

apps/mobile/v1/src/lib/encryption/storage/secureStorage.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import * as SecureStore from 'expo-secure-store';
77
import * as Crypto from 'expo-crypto';
88
import { STORAGE_KEYS } from './storageKeys';
9-
import { arrayBufferToBase64 } from '../core/crypto';
9+
import forge from 'node-forge';
1010

1111
/**
1212
* User secret management - in-memory cache
@@ -28,8 +28,9 @@ export async function getUserSecret(userId: string): Promise<string> {
2828
}
2929

3030
// Check memory cache
31-
if (userSecretsCache.has(userId)) {
32-
return userSecretsCache.get(userId)!;
31+
const cachedSecret = userSecretsCache.get(userId);
32+
if (cachedSecret) {
33+
return cachedSecret;
3334
}
3435

3536
// Use secure storage for user secrets
@@ -40,7 +41,9 @@ export async function getUserSecret(userId: string): Promise<string> {
4041
if (!secret) {
4142
// Generate new random secret and store it securely
4243
const randomBytes = await Crypto.getRandomBytesAsync(64);
43-
secret = arrayBufferToBase64(randomBytes);
44+
// Convert Uint8Array to base64 using node-forge
45+
const randomString = String.fromCharCode.apply(null, Array.from(randomBytes));
46+
secret = forge.util.encode64(randomString);
4447
await SecureStore.setItemAsync(storageKey, secret);
4548
}
4649

0 commit comments

Comments
 (0)