mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-16 06:58:46 +00:00
암호 키 생성 후 내보내기 페이지로 이동하도록 구현
This commit is contained in:
56
src/routes/(fullscreen)/key/generate/service.ts
Normal file
56
src/routes/(fullscreen)/key/generate/service.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { storeKeyPairIntoIndexedDB } from "$lib/indexedDB";
|
||||
import { pubKey, privKey } from "$lib/stores/key";
|
||||
|
||||
type KeyType = "public" | "private";
|
||||
|
||||
const generateRSAKeyPair = async () => {
|
||||
const keyPair = await window.crypto.subtle.generateKey(
|
||||
{
|
||||
name: "RSA-OAEP",
|
||||
modulusLength: 4096,
|
||||
publicExponent: new Uint8Array([1, 0, 1]),
|
||||
hash: "SHA-256",
|
||||
} satisfies RsaHashedKeyGenParams,
|
||||
true,
|
||||
["encrypt", "decrypt"],
|
||||
);
|
||||
return keyPair;
|
||||
};
|
||||
|
||||
const makeRSAKeyNonextractable = async (key: CryptoKey, type: KeyType) => {
|
||||
const format = type === "public" ? "spki" : "pkcs8";
|
||||
const keyUsage = type === "public" ? "encrypt" : "decrypt";
|
||||
return await window.crypto.subtle.importKey(
|
||||
format,
|
||||
await window.crypto.subtle.exportKey(format, key),
|
||||
{
|
||||
name: "RSA-OAEP",
|
||||
hash: "SHA-256",
|
||||
} satisfies RsaHashedImportParams,
|
||||
false,
|
||||
[keyUsage],
|
||||
);
|
||||
};
|
||||
|
||||
const exportKeyToBase64 = async (key: CryptoKey, type: KeyType) => {
|
||||
const exportedKey = await window.crypto.subtle.exportKey(
|
||||
type === "public" ? "spki" : "pkcs8",
|
||||
key,
|
||||
);
|
||||
return btoa(String.fromCharCode(...new Uint8Array(exportedKey)));
|
||||
};
|
||||
|
||||
export const generateKeyPair = async () => {
|
||||
const keyPair = await generateRSAKeyPair();
|
||||
const privKeySecure = await makeRSAKeyNonextractable(keyPair.privateKey, "private");
|
||||
|
||||
pubKey.set(keyPair.publicKey);
|
||||
privKey.set(privKeySecure);
|
||||
|
||||
await storeKeyPairIntoIndexedDB(keyPair.publicKey, privKeySecure);
|
||||
|
||||
return {
|
||||
pubKeyBase64: await exportKeyToBase64(keyPair.publicKey, "public"),
|
||||
privKeyBase64: await exportKeyToBase64(keyPair.privateKey, "private"),
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user