From 4953324b9c657d305d9b174807f9a549e5d2624b Mon Sep 17 00:00:00 2001 From: dojyorin Date: Thu, 25 Apr 2024 03:57:15 +0900 Subject: [PATCH] crypto prefix --- src/pure/crypto.ts | 56 ++++++++++++++++++++-------------------- test/pure/crypto.test.ts | 20 +++++++------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/pure/crypto.ts b/src/pure/crypto.ts index 0d387ce..653fb2c 100644 --- a/src/pure/crypto.ts +++ b/src/pure/crypto.ts @@ -48,10 +48,10 @@ async function deriveKey({publicKey, privateKey}:PortableCryptoKeyPair){ * Generate random binary with any number of bytes. * @example * ```ts -* const random = generateRandom(16); +* const random = cryptoRandom(16); * ``` */ -export function generateRandom(n:number):Uint8Array{ +export function cryptoRandom(n:number):Uint8Array{ return crypto.getRandomValues(new Uint8Array(n)); } @@ -61,10 +61,10 @@ export function generateRandom(n:number):Uint8Array{ * @example * ```ts * const bin = await Deno.readFile("./file"); -* const hash = await deriveHash(bin); +* const hash = await cryptoHash(bin); * ``` */ -export async function deriveHash(data:Uint8Array, sha?:`SHA-${256 | 384 | 512}`):Promise{ +export async function cryptoHash(data:Uint8Array, sha?:`SHA-${256 | 384 | 512}`):Promise{ return new Uint8Array(await crypto.subtle.digest(sha ?? "SHA-256", data)); } @@ -73,11 +73,11 @@ export async function deriveHash(data:Uint8Array, sha?:`SHA-${256 | 384 | 512}`) * Curve algorithm is "NIST P-256". * @example * ```ts -* const k1 = await generateEncryptKey(); -* const k2 = await generateEncryptKey(); +* const k1 = await cryptoGenerateEncryptKey(); +* const k2 = await cryptoGenerateEncryptKey(); * ``` */ -export async function generateEncryptKey():Promise{ +export async function cryptoGenerateEncryptKey():Promise{ return await generateKey(CURVE_ECDH, ["deriveKey"]); } @@ -86,10 +86,10 @@ export async function generateEncryptKey():Promise{ * Curve algorithm is "NIST P-256". * @example * ```ts -* const {publicKey, privateKey} = await generateSignKey(); +* const {publicKey, privateKey} = await cryptoGenerateSignKey(); * ``` */ -export async function generateSignKey():Promise{ +export async function cryptoGenerateSignKey():Promise{ return await generateKey(CURVE_ECDSA, ["sign", "verify"]); } @@ -100,22 +100,22 @@ export async function generateSignKey():Promise{ * @example * ```ts * const bin = await Deno.readFile("./file"); -* const k1 = await generateEncryptKey(); -* const k2 = await generateEncryptKey(); -* const cipher = await encryptData(bin, { +* const k1 = await cryptoGenerateEncryptKey(); +* const k2 = await cryptoGenerateEncryptKey(); +* const cipher = await cryptoEncrypt(bin, { * publicKey: k1.publicKey, * privateKey: k2.privateKey * }); -* const decrypt = await decryptData(cipher, { +* const decrypt = await cryptoDecrypt(cipher, { * publicKey: k2.publicKey, * privateKey: k1.privateKey * }); * ``` */ -export async function encryptData(data:Uint8Array, {publicKey, privateKey}:PortableCryptoKeyPair):Promise{ +export async function cryptoEncrypt(data:Uint8Array, {publicKey, privateKey}:PortableCryptoKeyPair):Promise{ const aes = { name: AES_MODE, - iv: generateRandom(12) + iv: cryptoRandom(12) }; return byteConcat(aes.iv, await crypto.subtle.encrypt(aes, await deriveKey({publicKey, privateKey}), data)); @@ -128,19 +128,19 @@ export async function encryptData(data:Uint8Array, {publicKey, privateKey}:Porta * @example * ```ts * const bin = await Deno.readFile("./file"); -* const k1 = await generateEncryptKey(); -* const k2 = await generateEncryptKey(); -* const cipher = await encryptData(bin, { +* const k1 = await cryptoGenerateEncryptKey(); +* const k2 = await cryptoGenerateEncryptKey(); +* const cipher = await cryptoEncrypt(bin, { * publicKey: k1.publicKey, * privateKey: k2.privateKey * }); -* const decrypt = await decryptData(cipher, { +* const decrypt = await cryptoDecrypt(cipher, { * publicKey: k2.publicKey, * privateKey: k1.privateKey * }); * ``` */ -export async function decryptData(data:Uint8Array, {publicKey, privateKey}:PortableCryptoKeyPair):Promise{ +export async function cryptoDecrypt(data:Uint8Array, {publicKey, privateKey}:PortableCryptoKeyPair):Promise{ const aes = { name: AES_MODE, iv: data.subarray(0, 12) @@ -154,12 +154,12 @@ export async function decryptData(data:Uint8Array, {publicKey, privateKey}:Porta * @example * ```ts * const bin = await Deno.readFile("./file"); -* const {publicKey, privateKey} = await generateSignKey(); -* const sign = await deriveSign(bin, privateKey); -* const verify = await verifySign(bin, publicKey, sign); +* const {publicKey, privateKey} = await cryptoGenerateSignKey(); +* const sign = await cryptoSign(bin, privateKey); +* const verify = await cryptoVerify(bin, publicKey, sign); * ``` */ -export async function deriveSign(data:Uint8Array, key:Uint8Array):Promise{ +export async function cryptoSign(data:Uint8Array, key:Uint8Array):Promise{ return new Uint8Array(await crypto.subtle.sign(MAC_ECDSA, await crypto.subtle.importKey(FORMAT_PRI, key, CURVE_ECDSA, false, ["sign"]), data)); } @@ -168,11 +168,11 @@ export async function deriveSign(data:Uint8Array, key:Uint8Array):Promise{ +export async function cryptoVerify(data:Uint8Array, key:Uint8Array, sign:Uint8Array):Promise{ return await crypto.subtle.verify(MAC_ECDSA, await crypto.subtle.importKey(FORMAT_PUB, key, CURVE_ECDSA, false, ["verify"]), sign, data); } \ No newline at end of file diff --git a/test/pure/crypto.test.ts b/test/pure/crypto.test.ts index 087c30f..b66db05 100644 --- a/test/pure/crypto.test.ts +++ b/test/pure/crypto.test.ts @@ -1,5 +1,5 @@ import {assertEquals} from "../../deps.test.ts"; -import {generateRandom, deriveHash, generateEncryptKey, generateSignKey, encryptData, decryptData, deriveSign, verifySign} from "../../src/pure/crypto.ts"; +import {cryptoRandom, cryptoHash, cryptoGenerateEncryptKey, cryptoGenerateSignKey, cryptoEncrypt, cryptoDecrypt, cryptoSign, cryptoVerify} from "../../src/pure/crypto.ts"; const sample = new Uint8Array([0x02, 0xF2, 0x5D, 0x1F, 0x1C, 0x34, 0xB9, 0x2F]); @@ -17,7 +17,7 @@ const hashResult = new Uint8Array([ Deno.test({ name: "Crypto: Random", fn(){ - const {byteLength} = generateRandom(16); + const {byteLength} = cryptoRandom(16); assertEquals(byteLength, 16); } @@ -26,7 +26,7 @@ Deno.test({ Deno.test({ name: "Crypto: Hash", async fn(){ - const hash = await deriveHash(sample, "SHA-512"); + const hash = await cryptoHash(sample, "SHA-512"); assertEquals(hash, hashResult); } @@ -35,15 +35,15 @@ Deno.test({ Deno.test({ name: "Crypto: Encrypt and Decrypt", async fn(){ - const key1 = await generateEncryptKey(); - const key2 = await generateEncryptKey(); + const key1 = await cryptoGenerateEncryptKey(); + const key2 = await cryptoGenerateEncryptKey(); - const encrypt = await encryptData(sample, { + const encrypt = await cryptoEncrypt(sample, { publicKey: key1.publicKey, privateKey: key2.privateKey }); - const decrypt = await decryptData(encrypt, { + const decrypt = await cryptoDecrypt(encrypt, { publicKey: key2.publicKey, privateKey: key1.privateKey }); @@ -55,9 +55,9 @@ Deno.test({ Deno.test({ name: "Crypto: Sign and Verify", async fn(){ - const key = await generateSignKey(); - const signature = await deriveSign(sample, key.privateKey); - const verify = await verifySign(sample, key.publicKey, signature); + const key = await cryptoGenerateSignKey(); + const signature = await cryptoSign(sample, key.privateKey); + const verify = await cryptoVerify(sample, key.publicKey, signature); assertEquals(verify, true); }