Skip to content

Commit

Permalink
fix method name
Browse files Browse the repository at this point in the history
  • Loading branch information
dojyorin committed Apr 18, 2024
1 parent 7275ce9 commit c7199c0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
46 changes: 23 additions & 23 deletions src/pure/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 pkGenerateECDH();
* const k2 = await pkGenerateECDH();
* const k1 = await generateEncryptKey();
* const k2 = await generateEncryptKey();
* ```
*/
export async function pkGenerateECDH():Promise<PortableCryptoKeyPair>{
export async function generateEncryptKey():Promise<PortableCryptoKeyPair>{
return await generateKey(CURVE_ECDH, ["deriveKey"]);
}

Expand All @@ -86,10 +86,10 @@ export async function pkGenerateECDH():Promise<PortableCryptoKeyPair>{
* Curve algorithm is "NIST P-256".
* @example
* ```ts
* const {publicKey, privateKey} = await pkGenerateECDSA();
* const {publicKey, privateKey} = await generateSignKey();
* ```
*/
export async function pkGenerateECDSA():Promise<PortableCryptoKeyPair>{
export async function generateSignKey():Promise<PortableCryptoKeyPair>{
return await generateKey(CURVE_ECDSA, ["sign", "verify"]);
}

Expand All @@ -100,19 +100,19 @@ export async function pkGenerateECDSA():Promise<PortableCryptoKeyPair>{
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const k1 = await pkGenerateECDH();
* const k2 = await pkGenerateECDH();
* const cipher = await pkEncrypt(bin, {
* const k1 = await generateEncryptKey();
* const k2 = await generateEncryptKey();
* const cipher = await encryptData(bin, {
* publicKey: k1.publicKey,
* privateKey: k2.privateKey
* });
* const decrypt = await pkDecrypt(cipher, {
* const decrypt = await decryptData(cipher, {
* publicKey: k2.publicKey,
* privateKey: k1.privateKey
* });
* ```
*/
export async function pkEncrypt(data:Uint8Array, {publicKey, privateKey}:PortableCryptoKeyPair):Promise<Uint8Array>{
export async function encryptData(data:Uint8Array, {publicKey, privateKey}:PortableCryptoKeyPair):Promise<Uint8Array>{
const aes = {
name: AES_MODE,
iv: generateRandom(12)
Expand All @@ -128,19 +128,19 @@ export async function pkEncrypt(data:Uint8Array, {publicKey, privateKey}:Portabl
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const k1 = await pkGenerateECDH();
* const k2 = await pkGenerateECDH();
* const cipher = await pkEncrypt(bin, {
* const k1 = await generateEncryptKey();
* const k2 = await generateEncryptKey();
* const cipher = await encryptData(bin, {
* publicKey: k1.publicKey,
* privateKey: k2.privateKey
* });
* const decrypt = await pkDecrypt(cipher, {
* const decrypt = await decryptData(cipher, {
* publicKey: k2.publicKey,
* privateKey: k1.privateKey
* });
* ```
*/
export async function pkDecrypt(data:Uint8Array, {publicKey, privateKey}:PortableCryptoKeyPair):Promise<Uint8Array>{
export async function decryptData(data:Uint8Array, {publicKey, privateKey}:PortableCryptoKeyPair):Promise<Uint8Array>{
const aes = {
name: AES_MODE,
iv: data.subarray(0, 12)
Expand All @@ -154,12 +154,12 @@ export async function pkDecrypt(data:Uint8Array, {publicKey, privateKey}:Portabl
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const {publicKey, privateKey} = await pkGenerateECDSA();
* const sign = await pkSign(bin, privateKey);
* const verify = await pkVerify(bin, publicKey, sign);
* const {publicKey, privateKey} = await generateSignKey();
* const sign = await deriveSign(bin, privateKey);
* const verify = await verifySign(bin, publicKey, sign);
* ```
*/
export async function pkSign(data:Uint8Array, key:Uint8Array):Promise<Uint8Array>{
export async function deriveSign(data:Uint8Array, key:Uint8Array):Promise<Uint8Array>{
return new Uint8Array(await crypto.subtle.sign(MAC_ECDSA, await crypto.subtle.importKey(FORMAT_PRI, key, CURVE_ECDSA, false, ["sign"]), data));
}

Expand All @@ -168,11 +168,11 @@ export async function pkSign(data:Uint8Array, key:Uint8Array):Promise<Uint8Array
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const {publicKey, privateKey} = await pkGenerateECDSA();
* const sign = await pkSign(bin, privateKey);
* const verify = await pkVerify(bin, publicKey, sign);
* const {publicKey, privateKey} = await generateSignKey();
* const sign = await deriveSign(bin, privateKey);
* const verify = await verifySign(bin, publicKey, sign);
* ```
*/
export async function pkVerify(data:Uint8Array, key:Uint8Array, sign:Uint8Array):Promise<boolean>{
export async function verifySign(data:Uint8Array, key:Uint8Array, sign:Uint8Array):Promise<boolean>{
return await crypto.subtle.verify(MAC_ECDSA, await crypto.subtle.importKey(FORMAT_PUB, key, CURVE_ECDSA, false, ["verify"]), sign, data);
}
16 changes: 8 additions & 8 deletions test/pure/crypto.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {assertEquals} from "../../deps.test.ts";
import {generateRandom, deriveHash, pkGenerateECDH, pkGenerateECDSA, pkEncrypt, pkDecrypt, pkSign, pkVerify} from "../../src/pure/crypto.ts";
import {generateRandom, deriveHash, generateEncryptKey, generateSignKey, encryptData, decryptData, deriveSign, verifySign} from "../../src/pure/crypto.ts";

const sample = new Uint8Array([0x02, 0xF2, 0x5D, 0x1F, 0x1C, 0x34, 0xB9, 0x2F]);

Expand Down Expand Up @@ -35,15 +35,15 @@ Deno.test({
Deno.test({
name: "Crypto: Encrypt and Decrypt",
async fn(){
const key1 = await pkGenerateECDH();
const key2 = await pkGenerateECDH();
const key1 = await generateEncryptKey();
const key2 = await generateEncryptKey();

const encrypt = await pkEncrypt(sample, {
const encrypt = await encryptData(sample, {
publicKey: key1.publicKey,
privateKey: key2.privateKey
});

const decrypt = await pkDecrypt(encrypt, {
const decrypt = await decryptData(encrypt, {
publicKey: key2.publicKey,
privateKey: key1.privateKey
});
Expand All @@ -55,9 +55,9 @@ Deno.test({
Deno.test({
name: "Crypto: Sign and Verify",
async fn(){
const key = await pkGenerateECDSA();
const signature = await pkSign(sample, key.privateKey);
const verify = await pkVerify(sample, key.publicKey, signature);
const key = await generateSignKey();
const signature = await deriveSign(sample, key.privateKey);
const verify = await verifySign(sample, key.publicKey, signature);

assertEquals(verify, true);
}
Expand Down

0 comments on commit c7199c0

Please sign in to comment.