Skip to content

Commit

Permalink
Merge pull request #52 from dojyorin/dev
Browse files Browse the repository at this point in the history
remove duplicate code.
  • Loading branch information
dojyorin authored May 23, 2023
2 parents d4ed314 + 9f0289f commit 2ba9332
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 80 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Release
"on":
name: release
on:
push:
tags: v[0-9]+.[0-9]+.[0-9]+
jobs:
Expand All @@ -14,7 +14,7 @@ jobs:
with:
deno-version: v1.x
- name: install esbuild
run: curl -L $(curl -L https://esbuild.github.io/getting-started | grep -o -E 'https:[^"]+linux-x64[^"]+\.tgz' | uniq) | tar x -z --strip-components 2
run: curl -L $(curl -L https://esbuild.github.io/getting-started | grep -oE -e 'https:[^"]+linux-x64[^"]+\.tgz' | uniq) | tar x -z --strip-components 2
- name: bundle universal module
run: deno bundle ./mod.universal.ts | ./esbuild --minify | head -c -1 > ./mod.universal.min.js
- name: dispatch release
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Test
"on":
name: test
on:
push:
branches:
- dev
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows_json/release.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "Release",
"name": "release",
"on": {
"push": {
"tags": "v[0-9]+.[0-9]+.[0-9]+"
Expand All @@ -20,7 +20,7 @@
}
}, {
"name": "install esbuild",
"run": "curl -L $(curl -L https://esbuild.github.io/getting-started | grep -o -E 'https:[^\"]+linux-x64[^\"]+\\.tgz' | uniq) | tar x -z --strip-components 2"
"run": "curl -L $(curl -L https://esbuild.github.io/getting-started | grep -oE -e 'https:[^\"]+linux-x64[^\"]+\\.tgz' | uniq) | tar x -z --strip-components 2"
}, {
"name": "bundle universal module",
"run": "deno bundle ./mod.universal.ts | ./esbuild --minify | head -c -1 > ./mod.universal.min.js"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows_json/test.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "Test",
"name": "test",
"on": {
"push": {
"branches": [
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows_json/to_yaml.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ set -eu

cd ${0%/*}

yq -P -I 4 ./${1}.json | head -c -1 > ../workflows/${1}.yaml
yq -o y -I 4 ./${1}.json | head -c -1 > ../workflows/${1}.yaml
4 changes: 1 addition & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ export * from "./src/time.ts";

export * from "./src/path.deno.ts";
export * from "./src/platform.deno.ts";
export * from "./src/process.deno.ts";

export type * from "./src/core.d.ts";
export * from "./src/process.deno.ts";
17 changes: 0 additions & 17 deletions src/core.d.ts

This file was deleted.

67 changes: 31 additions & 36 deletions src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
/**
* Serialized `CryptoKey`.
*/
export type PortableCryptoKey = Uint8Array;

/**
* Serialized `CryptoKeyPair`.
*/
export type PortableCryptoKeyPair = Record<keyof CryptoKeyPair, PortableCryptoKey>;
export type PortableCryptoKeyPair = Record<keyof CryptoKeyPair, Uint8Array>;

async function deriveSecretKey({publicKey, privateKey}:PortableCryptoKeyPair){
return await crypto.subtle.deriveKey({
Expand All @@ -28,21 +23,21 @@ async function deriveSecretKey({publicKey, privateKey}:PortableCryptoKeyPair){
* Generate UUIDv4 string.
* @example
* ```ts
* const uuid = cryptoUuid();
* const uuid = randomUuid();
* ```
*/
export function cryptoUuid():string{
export function randomUuid():string{
return crypto.randomUUID();
}

/**
* Generate random binary with any number of bytes.
* @example
* ```ts
* const random = cryptoRandom(16);
* const random = randomBin(16);
* ```
*/
export function cryptoRandom(n:number):Uint8Array{
export function randomBin(n:number):Uint8Array{
return crypto.getRandomValues(new Uint8Array(n));
}

Expand All @@ -51,10 +46,10 @@ export function cryptoRandom(n:number):Uint8Array{
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const hash = await cryptoHash(256, bin);
* const hash = await hashValue(256, bin);
* ```
*/
export async function cryptoHash(bit:256|384|512, data:Uint8Array):Promise<Uint8Array>{
export async function hashValue(bit:256 | 384 | 512, data:Uint8Array):Promise<Uint8Array>{
return new Uint8Array(await crypto.subtle.digest(`SHA-${bit}`, data));
}

Expand All @@ -64,15 +59,15 @@ export async function cryptoHash(bit:256|384|512, data:Uint8Array):Promise<Uint8
* Algorithm use is "NIST P-512".
* @example
* ```ts
* const keyForECDH = await cryptoGenerateKey(true);
* const keyForECDSA = await cryptoGenerateKey(false);
* const key1 = await pubkeyGen("ECDH");
* const key2 = await pubkeyGen("ECDSA");
* ```
*/
export async function cryptoGenerateKey(isECDH:boolean):Promise<PortableCryptoKeyPair>{
export async function pubkeyGen(usage:"ECDH" | "ECDSA"):Promise<PortableCryptoKeyPair>{
const {publicKey, privateKey} = await crypto.subtle.generateKey({
name: isECDH ? "ECDH" : "ECDSA",
name: usage,
namedCurve: "P-521"
}, true, isECDH ? ["deriveKey", "deriveBits"] : ["sign", "verify"]);
}, true, usage === "ECDH" ? ["deriveKey", "deriveBits"] : ["sign", "verify"]);

return {
publicKey: new Uint8Array(await crypto.subtle.exportKey("spki", publicKey)),
Expand All @@ -87,20 +82,20 @@ export async function cryptoGenerateKey(isECDH:boolean):Promise<PortableCryptoKe
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const key1 = await cryptoGenerateKey(true);
* const key2 = await cryptoGenerateKey(true);
* const converted = await cryptoEncrypt({
* const key1 = await pubkeyGen("ECDH");
* const key2 = await pubkeyGen("ECDH");
* const converted = await pubkeyEncrypt({
* publicKey: key1.publicKey,
* privateKey: key2.privateKey
* }, bin);
* const restored = await cryptoDecrypt({
* const restored = await pubkeyDecrypt({
* publicKey: key2.publicKey,
* privateKey: key1.privateKey
* }, converted);
* ```
*/
export async function cryptoEncrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array):Promise<Uint8Array>{
const iv = cryptoRandom(12);
export async function pubkeyEncrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array):Promise<Uint8Array>{
const iv = randomBin(12);
const enc = await crypto.subtle.encrypt({
name: "AES-GCM",
tagLength: 128,
Expand All @@ -121,19 +116,19 @@ export async function cryptoEncrypt({publicKey, privateKey}:PortableCryptoKeyPai
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const key1 = await cryptoGenerateKey(true);
* const key2 = await cryptoGenerateKey(true);
* const converted = await cryptoEncrypt({
* const key1 = await pubkeyGen("ECDH");
* const key2 = await pubkeyGen("ECDH");
* const converted = await pubkeyEncrypt({
* publicKey: key1.publicKey,
* privateKey: key2.privateKey
* }, bin);
* const restored = await cryptoDecrypt({
* const restored = await pubkeyDecrypt({
* publicKey: key2.publicKey,
* privateKey: key1.privateKey
* }, converted);
* ```
*/
export async function cryptoDecrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array):Promise<Uint8Array>{
export async function pubkeyDecrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array):Promise<Uint8Array>{
const iv = data.subarray(0, 12);
const dec = await crypto.subtle.decrypt({
name: "AES-GCM",
Expand All @@ -149,12 +144,12 @@ export async function cryptoDecrypt({publicKey, privateKey}:PortableCryptoKeyPai
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const key = await cryptoGenerateKey(false);
* const signature = await cryptoSign(key.privateKey, bin);
* const verified = await cryptoVerify(key.publicKey, signature, bin);
* const key = await pubkeyGen("ECDSA");
* const signature = await pubkeySign(key.privateKey, bin);
* const verified = await pubkeyVerify(key.publicKey, signature, bin);
* ```
*/
export async function cryptoSign(privateKey:PortableCryptoKey, data:Uint8Array):Promise<Uint8Array>{
export async function pubkeySign(privateKey:Uint8Array, data:Uint8Array):Promise<Uint8Array>{
const sign = await crypto.subtle.sign({
name: "ECDSA",
hash: "SHA-512"
Expand All @@ -171,12 +166,12 @@ export async function cryptoSign(privateKey:PortableCryptoKey, data:Uint8Array):
* @example
* ```ts
* const bin = await Deno.readFile("./file");
* const key = await cryptoGenerateKey(false);
* const signature = await cryptoSign(key.privateKey, bin);
* const verified = await cryptoVerify(key.publicKey, signature, bin);
* const key = await pubkeyGen("ECDSA");
* const signature = await pubkeySign(key.privateKey, bin);
* const verified = await pubkeyVerify(key.publicKey, signature, bin);
* ```
*/
export async function cryptoVerify(publicKey:PortableCryptoKey, signature:Uint8Array, data:Uint8Array):Promise<boolean>{
export async function pubkeyVerify(publicKey:Uint8Array, signature:Uint8Array, data:Uint8Array):Promise<boolean>{
return await crypto.subtle.verify({
name: "ECDSA",
hash: "SHA-512"
Expand Down
4 changes: 1 addition & 3 deletions src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {type JsonStruct} from "./core.d.ts";

/**
* Possible input for `URLSearchParams`.
*/
Expand All @@ -17,7 +15,7 @@ export interface FetchInit extends Omit<RequestInit, "window">{
*/
export interface ResponseType{
"text": string;
"json": JsonStruct;
"json": unknown;
"form": FormData;
"byte": Uint8Array;
"buffer": ArrayBuffer;
Expand Down
2 changes: 1 addition & 1 deletion src/process.deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Run command as subprocess.
* @example
* ```ts
* const success = executeCommand(["echo", "foobar"]);
* const success = await runCommand(["echo", "foobar"]);
* ```
*/
export async function runCommand(...commands:string[]):Promise<boolean>{
Expand Down
22 changes: 11 additions & 11 deletions test/crypto.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {assertEquals} from "../deps.test.ts";
import {cryptoUuid, cryptoRandom, cryptoHash, cryptoGenerateKey, cryptoEncrypt, cryptoDecrypt, cryptoSign, cryptoVerify} from "../src/crypto.ts";
import {randomUuid, randomBin, hashValue, pubkeyGen, pubkeyEncrypt, pubkeyDecrypt, pubkeySign, pubkeyVerify} from "../src/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: UUID",
fn(){
const uuid = cryptoUuid();
const uuid = randomUuid();

assertEquals(uuid.length, 36);
}
Expand All @@ -26,7 +26,7 @@ Deno.test({
Deno.test({
name: "Crypto: Random",
fn(){
const random = cryptoRandom(16);
const random = randomBin(16);

assertEquals(random.byteLength, 16);
}
Expand All @@ -35,7 +35,7 @@ Deno.test({
Deno.test({
name: "Crypto: Hash",
async fn(){
const hash = await cryptoHash(512, sample);
const hash = await hashValue(512, sample);

assertEquals(hash, hashResult);
}
Expand All @@ -45,15 +45,15 @@ Deno.test({
ignore: true,
name: "Crypto: Encrypt and Decrypt",
async fn(){
const key1 = await cryptoGenerateKey(true);
const key2 = await cryptoGenerateKey(true);
const key1 = await pubkeyGen("ECDH");
const key2 = await pubkeyGen("ECDH");

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

const decrypt = await cryptoDecrypt({
const decrypt = await pubkeyDecrypt({
publicKey: key2.publicKey,
privateKey: key1.privateKey
}, encrypt);
Expand All @@ -66,9 +66,9 @@ Deno.test({
ignore: true,
name: "Crypto: Sign and Verify",
async fn(){
const key = await cryptoGenerateKey(false);
const signature = await cryptoSign(key.privateKey, sample);
const result = await cryptoVerify(key.publicKey, signature, sample);
const key = await pubkeyGen("ECDSA");
const signature = await pubkeySign(key.privateKey, sample);
const result = await pubkeyVerify(key.publicKey, signature, sample);

assertEquals(result, true);
}
Expand Down

0 comments on commit 2ba9332

Please sign in to comment.