Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/secure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ describe("SecureStorage - Basic CRUD", () => {
expect(result).toEqual("mockData")
})

test("should properly set and get large data", async () => {
const storageMock = createStorageMock()

const secureStorage = new SecureStorage({ area: "sync" })
await secureStorage.setPassword("testPassword")

const mockData = new Array(200000).fill(65)
await secureStorage.set("testKey", mockData)

expect(storageMock.setTriggers).toHaveBeenCalledTimes(1)

const result = await secureStorage.get("testKey")

expect(storageMock.getTriggers).toHaveBeenCalledTimes(1)

// Assert that the decrypted data is returned
expect(result).toEqual(mockData)
})

test("should properly setMany and getMany data", async () => {
const storageMock = createStorageMock()

Expand Down
11 changes: 9 additions & 2 deletions src/secure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ const { crypto } = globalThis
const u8ToHex = (a: ArrayBufferLike) =>
Array.from(new Uint8Array(a), (v) => v.toString(16).padStart(2, "0")).join("")

const u8ToBase64 = (a: ArrayBufferLike) =>
globalThis.btoa(String.fromCharCode.apply(null, a))
const u8ToBase64 = (a: ArrayBufferLike) => {
const chunkSize = 10000;
const uint8Array = a instanceof ArrayBuffer ? new Uint8Array(a) : a;
let str = '';
for (let i = 0; i < uint8Array.byteLength; i += chunkSize) {
str += String.fromCharCode.apply(null, uint8Array.slice(i, i + chunkSize));
}
return globalThis.btoa(str)
}

const base64ToU8 = (base64: string) =>
Uint8Array.from(globalThis.atob(base64), (c) => c.charCodeAt(0))
Expand Down