diff --git a/.github/workflows_json/release.json b/.github/workflows/release.json similarity index 100% rename from .github/workflows_json/release.json rename to .github/workflows/release.json diff --git a/.github/workflows_json/test.json b/.github/workflows/test.json similarity index 100% rename from .github/workflows_json/test.json rename to .github/workflows/test.json diff --git a/.github/workflows/to_yaml.sh b/.github/workflows/to_yaml.sh new file mode 100755 index 0000000..d70cb17 --- /dev/null +++ b/.github/workflows/to_yaml.sh @@ -0,0 +1,10 @@ +#!/bin/sh +set -eu + +while [[ ${#} -ge 1 ]]; do + if [[ -f ./${1}.json ]]; then + yq -I 4 -o y ./${1}.json | head -c -1 > ./${1}.yaml + fi + + shift +done \ No newline at end of file diff --git a/.github/workflows_json/to_yaml.sh b/.github/workflows_json/to_yaml.sh deleted file mode 100644 index 4878624..0000000 --- a/.github/workflows_json/to_yaml.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh -set -eu - -cd ${0%/*} - -yq -o y -I 4 ./${1}.json | head -c -1 > ../workflows/${1}.yaml \ No newline at end of file diff --git a/deps.test.ts b/deps.test.ts index a8da676..a188725 100644 --- a/deps.test.ts +++ b/deps.test.ts @@ -1,3 +1,3 @@ -export {assertEquals} from "https://deno.land/std@0.198.0/assert/mod.ts"; -export {dirname, fromFileUrl} from "https://deno.land/std@0.198.0/path/mod.ts"; -export {exists} from "https://deno.land/std@0.198.0/fs/mod.ts"; \ No newline at end of file +export {assertEquals} from "https://deno.land/std@0.203.0/assert/mod.ts"; +export {dirname, fromFileUrl} from "https://deno.land/std@0.203.0/path/mod.ts"; +export {exists} from "https://deno.land/std@0.203.0/fs/mod.ts"; \ No newline at end of file diff --git a/deps.ts b/deps.ts index ab83b64..b9a2051 100644 --- a/deps.ts +++ b/deps.ts @@ -1,3 +1,3 @@ -export {dirname, fromFileUrl} from "https://deno.land/std@0.198.0/path/mod.ts"; -export {Logger, handlers} from "https://deno.land/std@0.198.0/log/mod.ts"; -export {format} from "https://deno.land/std@0.198.0/datetime/mod.ts"; \ No newline at end of file +export {dirname, fromFileUrl} from "https://deno.land/std@0.203.0/path/mod.ts"; +export {Logger, handlers} from "https://deno.land/std@0.203.0/log/mod.ts"; +export {format} from "https://deno.land/std@0.203.0/datetime/mod.ts"; \ No newline at end of file diff --git a/mod.test.ts b/mod.test.ts index 73858fe..c93243a 100644 --- a/mod.test.ts +++ b/mod.test.ts @@ -1,5 +1,5 @@ import "./test/base64.test.ts"; -import "./test/blob.test.ts"; +import "./test/byte.test.ts"; import "./test/crypto.test.ts"; import "./test/deep.test.ts"; import "./test/deflate.test.ts"; diff --git a/mod.ts b/mod.ts index 26257f3..2630c07 100644 --- a/mod.ts +++ b/mod.ts @@ -1,5 +1,5 @@ export * from "./src/base64.ts"; -export * from "./src/blob.ts"; +export * from "./src/byte.ts"; export * from "./src/crypto.ts"; export * from "./src/deep.ts"; export * from "./src/deflate.ts"; diff --git a/mod.universal.ts b/mod.universal.ts index d5f9659..bf2552d 100644 --- a/mod.universal.ts +++ b/mod.universal.ts @@ -1,5 +1,5 @@ export * from "./src/base64.ts"; -export * from "./src/blob.ts"; +export * from "./src/byte.ts"; export * from "./src/crypto.ts"; export * from "./src/deep.ts"; export * from "./src/deflate.ts"; diff --git a/src/base64.ts b/src/base64.ts index 08c9793..5d1d2f8 100644 --- a/src/base64.ts +++ b/src/base64.ts @@ -3,11 +3,11 @@ * @example * ```ts * const bin = await Deno.readFile("./file"); -* const converted = base64Encode(bin); -* const restored = base64Decode(converted); +* const encode = b64Encode(bin); +* const decode = b64Decode(encode); * ``` */ -export function base64Encode(data:Uint8Array):string{ +export function b64Encode(data:Uint8Array):string{ return btoa([...data].map(v => String.fromCharCode(v)).join("")); } @@ -16,11 +16,11 @@ export function base64Encode(data:Uint8Array):string{ * @example * ```ts * const bin = await Deno.readFile("./file"); -* const converted = base64Encode(bin); -* const restored = base64Decode(converted); +* const encode = b64Encode(bin); +* const decode = b64Decode(encode); * ``` */ -export function base64Decode(data:string):Uint8Array{ +export function b64Decode(data:string):Uint8Array{ return new Uint8Array([...atob(data)].map(v => v.charCodeAt(0))); } @@ -30,9 +30,9 @@ export function base64Decode(data:string):Uint8Array{ * @example * ```ts * const bin = await Deno.readFile("./file"); -* const url = base64DataURL(bin); +* const data = b64DataURL(bin); * ``` */ -export function base64DataURL(data:Uint8Array, mime?:string):string{ - return `data:${mime ?? "application/octet-stream"};base64,${base64Encode(data)}`; +export function b64DataURL(data:Uint8Array, mime?:string):string{ + return `data:${mime ?? "application/octet-stream"};base64,${b64Encode(data)}`; } \ No newline at end of file diff --git a/src/blob.ts b/src/blob.ts deleted file mode 100644 index 7b30f9a..0000000 --- a/src/blob.ts +++ /dev/null @@ -1,29 +0,0 @@ -import {base64Encode} from "./base64.ts"; - -/** -* Assignment of types convertible from blob. -*/ -export interface DataType{ - "text": string; - "base64": string; - "byte": Uint8Array; - "buffer": ArrayBuffer; -} - -/** -* Convert from blob to specified data type. -* @example -* ```ts -* const file = new File(["my-text"], "example.txt"); -* const data = await blobConvert(file, "text"); -* ``` -*/ -export async function blobConvert(blob:Blob, type:T):Promise{ - switch(type){ - case "text": return await blob.text(); - case "base64": return base64Encode(new Uint8Array(await blob.arrayBuffer())); - case "byte": return new Uint8Array(await blob.arrayBuffer()); - case "buffer": return await blob.arrayBuffer(); - default: throw new Error(); - } -} \ No newline at end of file diff --git a/src/byte.ts b/src/byte.ts new file mode 100644 index 0000000..16fdd5b --- /dev/null +++ b/src/byte.ts @@ -0,0 +1,48 @@ +import {b64Encode} from "./base64.ts"; + +/** +* Assignment of types convertible from blob. +*/ +export interface BlobType{ + "text": string; + "base64": string; + "byte": Uint8Array; + "buffer": ArrayBuffer; +} + +/** +* Convert from blob to specified data type. +* @example +* ```ts +* const file = new File(["my-text"], "example.txt"); +* const data = await blobConvert(file, "text"); +* ``` +*/ +export async function blobConvert(blob:Blob, type:T):Promise{ + switch(type){ + case "text": return await blob.text(); + case "base64": return b64Encode(new Uint8Array(await blob.arrayBuffer())); + case "byte": return new Uint8Array(await blob.arrayBuffer()); + case "buffer": return await blob.arrayBuffer(); + default: throw new Error(); + } +} + +/** +* Concat multiple buffer sources into single Uint8Array. +* @example +* ```ts +* const byte = byteConcat(new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])); +* ``` +*/ +export function byteConcat(...parts:BufferSource[]):Uint8Array{ + const output = new Uint8Array(parts.reduce((n, {byteLength}) => n + byteLength , 0)); + let i = 0; + + for(const part of parts){ + output.set(new Uint8Array(part instanceof ArrayBuffer ? part : part.buffer), i); + i += part.byteLength; + } + + return output; +} \ No newline at end of file diff --git a/src/crypto.ts b/src/crypto.ts index 914d579..f7d1d8d 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -3,19 +3,42 @@ */ export type PortableCryptoKeyPair = Record; -async function deriveSecretKey({publicKey, privateKey}:PortableCryptoKeyPair){ +const AES_MODE = "AES-GCM"; +const AES_BIT = 128; +const FORMAT_PUB = "spki"; +const FORMAT_PRI = "pkcs8"; + +const CURVE_ECDH = Object.freeze({ + name: "ECDH", + namedCurve: "P-256" +}); + +const CURVE_ECDSA = Object.freeze({ + name: "ECDSA", + namedCurve: "P-256" +}); + +const MAC_ECDSA = Object.freeze({ + name: "ECDSA", + hash: "SHA-256" +}); + +async function generateKey(alg:EcKeyAlgorithm, usage:KeyUsage[]){ + const {publicKey, privateKey} = await crypto.subtle.generateKey(alg, true, usage); + + return { + publicKey: new Uint8Array(await crypto.subtle.exportKey(FORMAT_PUB, publicKey)), + privateKey: new Uint8Array(await crypto.subtle.exportKey(FORMAT_PRI, privateKey)) + }; +} + +async function deriveKey({publicKey, privateKey}:PortableCryptoKeyPair){ return await crypto.subtle.deriveKey({ - name: "ECDH", - public: await crypto.subtle.importKey("spki", publicKey, { - name: "ECDH", - namedCurve: "P-521" - }, false, []) - }, await crypto.subtle.importKey("pkcs8", privateKey, { - name: "ECDH", - namedCurve: "P-521" - }, false, ["deriveKey", "deriveBits"]), { - name: "AES-GCM", - length: 256 + name: CURVE_ECDH.name, + public: await crypto.subtle.importKey(FORMAT_PUB, publicKey, CURVE_ECDH, false, []) + }, await crypto.subtle.importKey(FORMAT_PRI, privateKey, CURVE_ECDH, false, ["deriveKey"]), { + name: AES_MODE, + length: AES_BIT }, false, ["encrypt", "decrypt"]); } @@ -23,10 +46,10 @@ async function deriveSecretKey({publicKey, privateKey}:PortableCryptoKeyPair){ * Generate random binary with any number of bytes. * @example * ```ts -* const random = randomBin(16); +* const random = generateRandom(16); * ``` */ -export function randomBin(n:number):Uint8Array{ +export function generateRandom(n:number):Uint8Array{ return crypto.getRandomValues(new Uint8Array(n)); } @@ -35,97 +58,96 @@ export function randomBin(n:number):Uint8Array{ * @example * ```ts * const bin = await Deno.readFile("./file"); -* const hash = await hashValue(256, bin); +* const hash = await deriveHash(256, bin); * ``` */ -export async function hashValue(bit:256 | 384 | 512, data:Uint8Array):Promise{ +export async function deriveHash(bit:256 | 384 | 512, data:Uint8Array):Promise{ return new Uint8Array(await crypto.subtle.digest(`SHA-${bit}`, data)); } /** -* Generate exportable public-key pair. -* You can generate keys for ECDH or ECDSA. -* Algorithm use is "NIST P-512". +* Generate exportable public-key pair for ECDH. +* Curve algorithm is "NIST P-256". * @example * ```ts -* const key1 = await pubkeyGen("ECDH"); -* const key2 = await pubkeyGen("ECDSA"); +* const k1 = await pkGenerateECDH(); +* const k2 = await pkGenerateECDH(); * ``` */ -export async function pubkeyGen(usage:"ECDH" | "ECDSA"):Promise{ - const {publicKey, privateKey} = await crypto.subtle.generateKey({ - name: usage, - namedCurve: "P-521" - }, true, usage === "ECDH" ? ["deriveKey", "deriveBits"] : ["sign", "verify"]); +export async function pkGenerateECDH():Promise{ + return await generateKey(CURVE_ECDH, ["deriveKey"]); +} - return { - publicKey: new Uint8Array(await crypto.subtle.exportKey("spki", publicKey)), - privateKey: new Uint8Array(await crypto.subtle.exportKey("pkcs8", privateKey)) - }; +/** +* Generate exportable public-key pair for ECDSA. +* Curve algorithm is "NIST P-256". +* @example +* ```ts +* const {publicKey, privateKey} = await pkGenerateECDSA(); +* ``` +*/ +export async function pkGenerateECDSA():Promise{ + return await generateKey(CURVE_ECDSA, ["sign", "verify"]); } /** * Encrypt binary. -* Algorithm use is "AES-GCM" with 256 bits key, 128 bits tag and 96 bits IV. +* Algorithm is AES-GCM with 128 bits key, 128 bits tag and 96 bits IV. * IV is prepended to cipher. * @example * ```ts * const bin = await Deno.readFile("./file"); -* const key1 = await pubkeyGen("ECDH"); -* const key2 = await pubkeyGen("ECDH"); -* const converted = await pubkeyEncrypt({ -* publicKey: key1.publicKey, -* privateKey: key2.privateKey +* const k1 = await pkGenerateECDH(); +* const k2 = await pkGenerateECDH(); +* const cipher = await pkEncrypt({ +* publicKey: k1.publicKey, +* privateKey: k2.privateKey * }, bin); -* const restored = await pubkeyDecrypt({ -* publicKey: key2.publicKey, -* privateKey: key1.privateKey -* }, converted); +* const decrypt = await pkDecrypt({ +* publicKey: k2.publicKey, +* privateKey: k1.privateKey +* }, cipher); * ``` */ -export async function pubkeyEncrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array):Promise{ - const iv = randomBin(12); - const enc = await crypto.subtle.encrypt({ - name: "AES-GCM", - tagLength: 128, - iv: iv - }, await deriveSecretKey({publicKey, privateKey}), data); - - const output = new Uint8Array(iv.byteLength + enc.byteLength); - output.set(iv, 0); - output.set(new Uint8Array(enc), iv.byteLength); +export async function pkEncrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array):Promise{ + const aes = { + name: AES_MODE, + iv: generateRandom(12) + }; + + const output = new Uint8Array(aes.iv.byteLength + data.byteLength + 16); + output.set(aes.iv, 0); + output.set(new Uint8Array(await crypto.subtle.encrypt(aes, await deriveKey({publicKey, privateKey}), data)), aes.iv.byteLength); return output; } /** * Decrypt binary. -* Algorithm use is "AES-GCM" with 256 bits key, 128 bits tag and 96 bits IV. +* Algorithm is AES-GCM with 128 bits key, 128 bits tag and 96 bits IV. * IV is read from head of cipher. * @example * ```ts * const bin = await Deno.readFile("./file"); -* const key1 = await pubkeyGen("ECDH"); -* const key2 = await pubkeyGen("ECDH"); -* const converted = await pubkeyEncrypt({ -* publicKey: key1.publicKey, -* privateKey: key2.privateKey +* const k1 = await pkGenerateECDH(); +* const k2 = await pkGenerateECDH(); +* const cipher = await pkEncrypt({ +* publicKey: k1.publicKey, +* privateKey: k2.privateKey * }, bin); -* const restored = await pubkeyDecrypt({ -* publicKey: key2.publicKey, -* privateKey: key1.privateKey -* }, converted); +* const decrypt = await pkDecrypt({ +* publicKey: k2.publicKey, +* privateKey: k1.privateKey +* }, cipher); * ``` */ -export async function pubkeyDecrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array):Promise{ - const iv = data.subarray(0, 12); - const dec = await crypto.subtle.decrypt({ - name: "AES-GCM", - tagLength: 128, - iv: iv - }, await deriveSecretKey({publicKey, privateKey}), data.subarray(iv.byteLength)); - - return new Uint8Array(dec); +export async function pkDecrypt({publicKey, privateKey}:PortableCryptoKeyPair, data:Uint8Array):Promise{ + const aes = { + name: AES_MODE, + iv: data.subarray(0, 12) + }; + + return new Uint8Array(await crypto.subtle.decrypt(aes, await deriveKey({publicKey, privateKey}), data.subarray(aes.iv.byteLength))); } /** @@ -133,21 +155,13 @@ export async function pubkeyDecrypt({publicKey, privateKey}:PortableCryptoKeyPai * @example * ```ts * const bin = await Deno.readFile("./file"); -* const key = await pubkeyGen("ECDSA"); -* const signature = await pubkeySign(key.privateKey, bin); -* const verified = await pubkeyVerify(key.publicKey, signature, bin); +* const {publicKey, privateKey} = await pkGenerateECDSA(); +* const sign = await pkSign(privateKey, bin); +* const verify = await pkVerify(publicKey, sign, bin); * ``` */ -export async function pubkeySign(privateKey:Uint8Array, data:Uint8Array):Promise{ - const sign = await crypto.subtle.sign({ - name: "ECDSA", - hash: "SHA-512" - }, await crypto.subtle.importKey("pkcs8", privateKey, { - name: "ECDSA", - namedCurve: "P-521" - }, false, ["sign"]), data); - - return new Uint8Array(sign); +export async function pkSign(key:Uint8Array, data:Uint8Array):Promise{ + return new Uint8Array(await crypto.subtle.sign(MAC_ECDSA, await crypto.subtle.importKey(FORMAT_PRI, key, CURVE_ECDSA, false, ["sign"]), data)); } /** @@ -155,17 +169,11 @@ export async function pubkeySign(privateKey:Uint8Array, data:Uint8Array):Promise * @example * ```ts * const bin = await Deno.readFile("./file"); -* const key = await pubkeyGen("ECDSA"); -* const signature = await pubkeySign(key.privateKey, bin); -* const verified = await pubkeyVerify(key.publicKey, signature, bin); +* const {publicKey, privateKey} = await pkGenerateECDSA(); +* const sign = await pkSign(privateKey, bin); +* const verify = await pkVerify(publicKey, sign, bin); * ``` */ -export async function pubkeyVerify(publicKey:Uint8Array, signature:Uint8Array, data:Uint8Array):Promise{ - return await crypto.subtle.verify({ - name: "ECDSA", - hash: "SHA-512" - }, await crypto.subtle.importKey("spki", publicKey, { - name: "ECDSA", - namedCurve: "P-521" - }, false, ["verify"]), signature, data); +export async function pkVerify(key:Uint8Array, sign:Uint8Array, data: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/src/deflate.ts b/src/deflate.ts index 75f7894..e3cfb83 100644 --- a/src/deflate.ts +++ b/src/deflate.ts @@ -8,8 +8,8 @@ async function streamConvert(data:Uint8Array, ts:TransformStream{ @@ -22,8 +22,8 @@ export async function deflateEncode(data:Uint8Array):Promise{ * @example * ```ts * const bin = await Deno.readFile("./file"); -* const converted = await deflateEncode(bin); -* const restored = await deflateDecode(converted); +* const encode = await deflateEncode(bin); +* const decode = await deflateDecode(encode); * ``` */ export async function deflateDecode(data:Uint8Array):Promise{ diff --git a/src/fetch.ts b/src/fetch.ts index 31ab523..c9876f5 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -1,4 +1,4 @@ -import {base64Encode} from "./base64.ts"; +import {b64Encode} from "./base64.ts"; /** * Possible input for `URLSearchParams`. @@ -56,7 +56,7 @@ export async function fetchExtend(path:string, typ switch(type){ case "text": return await response.text(); - case "base64": return base64Encode(new Uint8Array(await response.arrayBuffer())); + case "base64": return b64Encode(new Uint8Array(await response.arrayBuffer())); case "json": return await response.json(); case "form": return await response.formData(); case "byte": return new Uint8Array(await response.arrayBuffer()); diff --git a/src/json.deno.ts b/src/json.deno.ts index 63b5d36..120f52a 100644 --- a/src/json.deno.ts +++ b/src/json.deno.ts @@ -5,7 +5,7 @@ import {mainPath} from "./path.deno.ts"; * Read JSON file and convert to object. * @example * ```ts -* const object = await jsonRead("./resource.json"); +* const json = await jsonRead("./resource.json"); * ``` */ export async function jsonRead>(path:string):Promise{ diff --git a/src/minipack.ts b/src/minipack.ts index fb4cc46..762619f 100644 --- a/src/minipack.ts +++ b/src/minipack.ts @@ -1,4 +1,4 @@ -import {utfEncode, utfDecode} from "./text.ts"; +import {u8Encode, u8Decode} from "./text.ts"; const sizeName = 1; const sizeBody = 4; @@ -17,17 +17,16 @@ export type FileInit = [string, Uint8Array]; * ["file1", await Deno.readFile("./file1")], * ["file2", await Deno.readFile("./file2")] * ]; -* const converted = minipackEncode(files); -* const restored = minipackDecode(converted); +* const encode = mpEncode(files); +* const decode = mpDecode(encode); * ``` */ -export function minipackEncode(files:FileInit[]):Uint8Array{ - const archive = new Uint8Array(files.reduce((size, [k, v]) => size + sizeName + sizeBody + utfEncode(k).byteLength + v.byteLength, 0)); - +export function mpEncode(files:FileInit[]):Uint8Array{ + const archive = new Uint8Array(files.reduce((size, [k, v]) => size + sizeName + sizeBody + u8Encode(k).byteLength + v.byteLength, 0)); let i = 0; for(const [k, v] of files){ - const name = utfEncode(k); + const name = u8Encode(k); const body = v; new DataView(archive.buffer, i).setUint8(0, name.byteLength); @@ -55,21 +54,21 @@ export function minipackEncode(files:FileInit[]):Uint8Array{ * ["file1", await Deno.readFile("./file1")], * ["file2", await Deno.readFile("./file2")] * ]; -* const converted = minipackEncode(files); -* const restored = minipackDecode(converted); +* const encode = mpEncode(files); +* const decode = mpDecode(encode); * ``` */ -export function minipackDecode(archive:Uint8Array):FileInit[]{ +export function mpDecode(archive:Uint8Array):FileInit[]{ const files:FileInit[] = []; - for(let i = 0; i < archive.byteLength; false){ + for(let i = 0; i < archive.byteLength;){ const ns = new DataView(archive.buffer, i).getUint8(0); i += sizeName; const bs = new DataView(archive.buffer, i).getUint32(0); i += sizeBody; - const name = utfDecode(archive.subarray(i, i += ns)); + const name = u8Decode(archive.subarray(i, i += ns)); const body = archive.subarray(i, i += bs); files.push([name, body]); diff --git a/src/platform.deno.ts b/src/platform.deno.ts index 90e12d2..2c37309 100644 --- a/src/platform.deno.ts +++ b/src/platform.deno.ts @@ -2,7 +2,7 @@ * Are you running on Windows? * @example * ```ts -* const runOnWin = isWin(); +* const win = isWin(); * ``` */ export function isWin():boolean{ diff --git a/src/text.ts b/src/text.ts index a0448d5..4327693 100644 --- a/src/text.ts +++ b/src/text.ts @@ -3,11 +3,11 @@ * @example * ```ts * const text = "HelloWorld!"; -* const converted = utfEncode(text); -* const restored = utfDecode(converted); +* const encode = u8Encode(text); +* const decode = u8Decode(encode); * ``` */ -export function utfEncode(data:string):Uint8Array{ +export function u8Encode(data:string):Uint8Array{ return new TextEncoder().encode(data); } @@ -16,11 +16,11 @@ export function utfEncode(data:string):Uint8Array{ * @example * ```ts * const text = "HelloWorld!"; -* const converted = utfEncode(text); -* const restored = utfDecode(converted); +* const encode = u8Encode(text); +* const decode = u8Decode(encode); * ``` */ -export function utfDecode(data:Uint8Array):string{ +export function u8Decode(data:Uint8Array):string{ return new TextDecoder().decode(data); } @@ -29,8 +29,8 @@ export function utfDecode(data:Uint8Array):string{ * @example * ```ts * const bin = await Deno.readFile("./file"); -* const converted = hexEncode(bin); -* const restored = hexDecode(converted); +* const encode = hexEncode(bin); +* const decode = hexDecode(encode); * ``` */ export function hexEncode(data:Uint8Array):string{ @@ -42,8 +42,8 @@ export function hexEncode(data:Uint8Array):string{ * @example * ```ts * const bin = await Deno.readFile("./file"); -* const converted = hexEncode(bin); -* const restored = hexDecode(converted); +* const encode = hexEncode(bin); +* const decode = hexDecode(encode); * ``` */ export function hexDecode(data:string):Uint8Array{ @@ -55,7 +55,7 @@ export function hexDecode(data:string):Uint8Array{ * @example * ```ts * const text = " Lorem ipsum\r dolor sit \r\r amet. "; -* const formated = trimExtend(text); +* const format = trimExtend(text); * ``` */ export function trimExtend(data:string):string{ @@ -67,7 +67,7 @@ export function trimExtend(data:string):string{ * @example * ```ts * const text = "1+1=2"; -* const formated = fixWidth(text); +* const format = fixWidth(text); * ``` */ export function fixWidth(data:string):string{ @@ -107,7 +107,7 @@ export function fixWidth(data:string):string{ * @example * ```ts * const text = "1 + 1 = 2 "; -* const formated = cleanText(text); +* const format = cleanText(text); * ``` */ export function cleanText(data:string):string{ @@ -133,7 +133,7 @@ export function accurateSegment(data:string):string[]{ * @example * ```ts * const num = 8; -* const padding = pad0(num); +* const pad = pad0(num); * ``` */ export function pad0(data:number, digit?:number, radix?:number):string{ diff --git a/src/time.ts b/src/time.ts index 47d1330..883f158 100644 --- a/src/time.ts +++ b/src/time.ts @@ -3,11 +3,11 @@ * If no argument will be calculate at current time. * @example * ```ts -* const time = unixtimeEncode(); -* const date = unixtimeDecode(time); +* const time = utEncode(); +* const date = utDecode(time); * ``` */ -export function unixtimeEncode(date?:Date):number{ +export function utEncode(date?:Date):number{ return Math.floor((date ?? new Date()).getTime() / 1000); } @@ -16,11 +16,11 @@ export function unixtimeEncode(date?:Date):number{ * Note that in seconds not milliseconds. * @example * ```ts -* const time = unixtimeEncode(); -* const date = unixtimeDecode(time); +* const time = utEncode(); +* const date = utDecode(time); * ``` */ -export function unixtimeDecode(time:number):Date{ +export function utDecode(time:number):Date{ return new Date(time * 1000); } @@ -28,11 +28,11 @@ export function unixtimeDecode(time:number):Date{ * Convert from formatted datetime string such as ISO8601 to UNIX time in seconds. * @example * ```ts -* const time = unixtimeParse("2023-05-18T08:31:32.292Z"); +* const time = utParse("2023-05-18T08:31:32.292Z"); * ``` */ -export function unixtimeParse(ds:string):number{ +export function utParse(ds:string):number{ const [y, m, d, h, mi, s] = ds.split(/[/ :TZ_.-]/i).map(v => Number(v)); - return unixtimeEncode(new Date(y, (m ?? 1) - 1, d ?? 1, h ?? 0, mi ?? 0, s ?? 0)); + return utEncode(new Date(y, (m ?? 1) - 1, d ?? 1, h ?? 0, mi ?? 0, s ?? 0)); } \ No newline at end of file diff --git a/test/base64.test.ts b/test/base64.test.ts index 5c76b16..27a22df 100644 --- a/test/base64.test.ts +++ b/test/base64.test.ts @@ -1,5 +1,5 @@ import {assertEquals} from "../deps.test.ts"; -import {base64Encode, base64Decode, base64DataURL} from "../src/base64.ts"; +import {b64Encode, b64Decode, b64DataURL} from "../src/base64.ts"; const sample1 = new Uint8Array([ 0x58, 0x0D, 0xC7, 0x64, 0x21, 0x42, 0x27, 0x76, @@ -13,7 +13,7 @@ const sample2 = "WA3HZCFCJ3Yupv6eWKOTmpoHV65OWy/Heu/Xr/UfKjo="; Deno.test({ name: "Base64: Encode", fn(){ - const encode = base64Encode(sample1); + const encode = b64Encode(sample1); assertEquals(encode, sample2); } @@ -22,7 +22,7 @@ Deno.test({ Deno.test({ name: "Base64: Decode", fn(){ - const decode = base64Decode(sample2); + const decode = b64Decode(sample2); assertEquals(decode, sample1); } @@ -31,7 +31,7 @@ Deno.test({ Deno.test({ name: "Base64: DataURL", fn(){ - const encode = base64DataURL(sample1); + const encode = b64DataURL(sample1); assertEquals(encode, `data:application/octet-stream;base64,${sample2}`); } diff --git a/test/blob.test.ts b/test/blob.test.ts deleted file mode 100644 index dad8f81..0000000 --- a/test/blob.test.ts +++ /dev/null @@ -1,13 +0,0 @@ -import {assertEquals} from "../deps.test.ts"; -import {blobConvert} from "../src/blob.ts"; - -const sample = "hello!"; - -Deno.test({ - name: "Blob: Convert", - async fn(){ - const data = await blobConvert(new Blob([sample]), "text"); - - assertEquals(data, sample); - } -}); \ No newline at end of file diff --git a/test/byte.test.ts b/test/byte.test.ts new file mode 100644 index 0000000..c402741 --- /dev/null +++ b/test/byte.test.ts @@ -0,0 +1,22 @@ +import {assertEquals} from "../deps.test.ts"; +import {blobConvert, byteConcat} from "../src/byte.ts"; + +const sample = "hello!"; + +Deno.test({ + name: "Byte: Blob Convert", + async fn(){ + const data = await blobConvert(new Blob([sample]), "text"); + + assertEquals(data, sample); + } +}); + +Deno.test({ + name: "Byte: Concat", + fn(){ + const data = byteConcat(new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])); + + assertEquals(data, new Uint8Array([1, 2, 3, 4, 5, 6])); + } +}); \ No newline at end of file diff --git a/test/crypto.test.ts b/test/crypto.test.ts index 4075bda..da954ed 100644 --- a/test/crypto.test.ts +++ b/test/crypto.test.ts @@ -1,5 +1,5 @@ import {assertEquals} from "../deps.test.ts"; -import {randomBin, hashValue, pubkeyGen, pubkeyEncrypt, pubkeyDecrypt, pubkeySign, pubkeyVerify} from "../src/crypto.ts"; +import {generateRandom, deriveHash, pkGenerateECDH, pkGenerateECDSA, pkEncrypt, pkDecrypt, pkSign, pkVerify} from "../src/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} = randomBin(16); + const {byteLength} = generateRandom(16); assertEquals(byteLength, 16); } @@ -26,25 +26,24 @@ Deno.test({ Deno.test({ name: "Crypto: Hash", async fn(){ - const hash = await hashValue(512, sample); + const hash = await deriveHash(512, sample); assertEquals(hash, hashResult); } }); Deno.test({ - ignore: true, name: "Crypto: Encrypt and Decrypt", async fn(){ - const key1 = await pubkeyGen("ECDH"); - const key2 = await pubkeyGen("ECDH"); + const key1 = await pkGenerateECDH(); + const key2 = await pkGenerateECDH(); - const encrypt = await pubkeyEncrypt({ + const encrypt = await pkEncrypt({ publicKey: key1.publicKey, privateKey: key2.privateKey }, sample); - const decrypt = await pubkeyDecrypt({ + const decrypt = await pkDecrypt({ publicKey: key2.publicKey, privateKey: key1.privateKey }, encrypt); @@ -54,12 +53,11 @@ Deno.test({ }); Deno.test({ - ignore: true, name: "Crypto: Sign and Verify", async fn(){ - const key = await pubkeyGen("ECDSA"); - const signature = await pubkeySign(key.privateKey, sample); - const verify = await pubkeyVerify(key.publicKey, signature, sample); + const key = await pkGenerateECDSA(); + const signature = await pkSign(key.privateKey, sample); + const verify = await pkVerify(key.publicKey, signature, sample); assertEquals(verify, true); } diff --git a/test/deep.test.ts b/test/deep.test.ts index a5e3ae2..8d64b40 100644 --- a/test/deep.test.ts +++ b/test/deep.test.ts @@ -10,7 +10,7 @@ const sample = { }; Deno.test({ - name: "Freeze: DeepFreeze", + name: "Deep: Freeze", fn(){ const freeze = deepFreeze(deepClone(sample)); @@ -21,7 +21,7 @@ Deno.test({ }); Deno.test({ - name: "Freeze: DeepSeal", + name: "Deep: Seal", fn(){ const seal = deepSeal(deepClone(sample)); diff --git a/test/minipack.test.ts b/test/minipack.test.ts index 912c5e3..dfcd35c 100644 --- a/test/minipack.test.ts +++ b/test/minipack.test.ts @@ -1,5 +1,5 @@ import {assertEquals} from "../deps.test.ts"; -import {minipackEncode, minipackDecode} from "../src/minipack.ts"; +import {mpEncode, mpDecode} from "../src/minipack.ts"; const sampleBin = new Uint8Array([0x02, 0xF2, 0x5D, 0x1F, 0x1C, 0x34, 0xB9, 0x2F]); const sampleName = "random.bin"; @@ -7,8 +7,8 @@ const sampleName = "random.bin"; Deno.test({ name: "Minipack: Encode and Decode", fn(){ - const encode = minipackEncode([[sampleName, sampleBin]]); - const [[name, body]] = minipackDecode(encode); + const encode = mpEncode([[sampleName, sampleBin]]); + const [[name, body]] = mpDecode(encode); assertEquals(name, sampleName); assertEquals(body, sampleBin); diff --git a/test/text.test.ts b/test/text.test.ts index 9fa0c9a..21d7fd7 100644 --- a/test/text.test.ts +++ b/test/text.test.ts @@ -1,5 +1,5 @@ import {assertEquals} from "../deps.test.ts"; -import {utfEncode, utfDecode, hexEncode, hexDecode, trimExtend, fixWidth, cleanText, accurateSegment, pad0} from "../src/text.ts"; +import {u8Encode, u8Decode, hexEncode, hexDecode, trimExtend, fixWidth, cleanText, accurateSegment, pad0} from "../src/text.ts"; const sampleText = " Lorem ipsum\r dolor sit \r\r amet. "; const sampleBin = new Uint8Array([ @@ -13,8 +13,8 @@ const sampleBin = new Uint8Array([ Deno.test({ name: "Text: UTF8 Encode and Decode", fn(){ - const encode = utfEncode(sampleText); - const decode = utfDecode(encode); + const encode = u8Encode(sampleText); + const decode = u8Decode(encode); assertEquals(decode, sampleText); } diff --git a/test/time.test.ts b/test/time.test.ts index be73f90..83ecccc 100644 --- a/test/time.test.ts +++ b/test/time.test.ts @@ -1,22 +1,22 @@ import {assertEquals} from "../deps.test.ts"; -import {unixtimeEncode, unixtimeDecode, unixtimeParse} from "../src/time.ts"; +import {utEncode, utDecode, utParse} from "../src/time.ts"; const sample = new Date(2000, 0, 1, 0, 0, 0, 0); Deno.test({ - name: "Date: Encode and Decode", + name: "Time: Encode and Decode", fn(){ - const encode = unixtimeEncode(sample); - const decode = unixtimeDecode(encode); + const encode = utEncode(sample); + const decode = utDecode(encode); assertEquals(decode, sample); } }); Deno.test({ - name: "Date: Parse", + name: "Time: Parse", fn(){ - const result = unixtimeParse(sample.toISOString()); + const result = utParse(sample.toISOString()); assertEquals(result, 946684800); }