mirror of
https://github.com/kmc7468/arkvault.git
synced 2026-11-04 08:10:28 +09:00
사소한 리팩토링
This commit is contained in:
1 parent
d986493431
commit
48bf9c1165
62 files changed
+31
-39
No files matched your search
@@ -0,0 +1,45 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
decodeFromBase64,
|
||||
decryptChunk,
|
||||
decryptString,
|
||||
encodeToBase64,
|
||||
encryptChunk,
|
||||
encryptString,
|
||||
generateDataKey,
|
||||
} from "#lib/shared/crypto/index.ts";
|
||||
|
||||
describe("base64", () => {
|
||||
it("round-trips large buffers", () => {
|
||||
const data = Uint8Array.from({ length: 1024 * 1024 }, (_, i) => (i * 31) % 256);
|
||||
const encoded = encodeToBase64(data);
|
||||
expect(new Uint8Array(decodeFromBase64(encoded))).toEqual(data);
|
||||
expect(encodeToBase64(data.buffer)).toBe(encoded);
|
||||
});
|
||||
});
|
||||
|
||||
describe("AES-GCM", () => {
|
||||
it("round-trips strings", async () => {
|
||||
const { dataKey } = await generateDataKey();
|
||||
const { ciphertext, iv } = await encryptString("안녕하세요.jpg", dataKey);
|
||||
expect(await decryptString(ciphertext, iv, dataKey)).toBe("안녕하세요.jpg");
|
||||
});
|
||||
|
||||
it("round-trips chunks", async () => {
|
||||
const { dataKey } = await generateDataKey();
|
||||
const chunk = crypto.getRandomValues(new Uint8Array(4096));
|
||||
|
||||
const encrypted = await encryptChunk(chunk.buffer, dataKey);
|
||||
|
||||
expect(encrypted.byteLength).toBe(4096 + 12 + 16);
|
||||
expect(new Uint8Array(await decryptChunk(encrypted, dataKey))).toEqual(chunk);
|
||||
});
|
||||
|
||||
it("rejects tampered chunks", async () => {
|
||||
const { dataKey } = await generateDataKey();
|
||||
const encrypted = new Uint8Array(await encryptChunk(new Uint8Array(32).buffer, dataKey));
|
||||
encrypted[20]! ^= 1;
|
||||
|
||||
await expect(decryptChunk(encrypted.buffer, dataKey)).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user