Skip to content

Commit

Permalink
crypto prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
dojyorin committed Apr 24, 2024
1 parent 44b15f5 commit 4953324
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
56 changes: 28 additions & 28 deletions src/pure/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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<Uint8Array>{
export async function cryptoHash(data:Uint8Array, sha?:`SHA-${256 | 384 | 512}`):Promise<Uint8Array>{
return new Uint8Array(await crypto.subtle.digest(sha ?? "SHA-256", data));
}

Expand All @@ -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<PortableCryptoKeyPair>{
export async function cryptoGenerateEncryptKey():Promise<PortableCryptoKeyPair>{
return await generateKey(CURVE_ECDH, ["deriveKey"]);
}

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

Expand All @@ -100,22 +100,22 @@ export async function generateSignKey():Promise<PortableCryptoKeyPair>{
* @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<Uint8Array>{
export async function cryptoEncrypt(data:Uint8Array, {publicKey, privateKey}:PortableCryptoKeyPair):Promise<Uint8Array>{
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));
Expand All @@ -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<Uint8Array>{
export async function cryptoDecrypt(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 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<Uint8Array>{
export async function cryptoSign(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 deriveSign(data:Uint8Array, key:Uint8Array):Promise<Uint8A
* @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 verifySign(data:Uint8Array, key:Uint8Array, sign:Uint8Array):Promise<boolean>{
export async function cryptoVerify(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);
}
20 changes: 10 additions & 10 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, 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]);

Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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
});
Expand All @@ -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);
}
Expand Down

0 comments on commit 4953324

Please sign in to comment.