Skip to content

Commit

Permalink
Change export type name
Browse files Browse the repository at this point in the history
  • Loading branch information
mryhryki committed Aug 5, 2023
1 parent d93a94e commit 2296ad6
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 32 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SimpleEncryption } from "./types.d.ts";
import { SimpleEncryptionType } from "./types.d.ts";

export const DefaultAlg: Readonly<SimpleEncryption.SupportAlgorithm> =
export const DefaultAlg: Readonly<SimpleEncryptionType.SupportAlgorithm> =
"AES-GCM" as const;

export const getRandomBytes = (length: number, crypto: Crypto): Uint8Array => {
Expand Down
6 changes: 3 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { assertEquals } from "https://deno.land/std@0.193.0/testing/asserts.ts";
import { decrypt, encrypt } from "./index.ts";
import { SimpleEncryption } from "./types.d.ts";
import { SimpleEncryptionType } from "./types.d.ts";

const key = "314560f0574292fabeccc32a39de7f28";
const iv = "c3b21a40f02858c45853f369143d0b44";
const sampleData = "3b461ac2-05d1-406a-9214-d835e42c27cd";

const Algorithms: SimpleEncryption.SupportAlgorithm[] = ["AES-GCM", "AES-CBC"];
const Algorithms: SimpleEncryptionType.SupportAlgorithm[] = ["AES-GCM", "AES-CBC"];
await Promise.all(Algorithms.map((alg) => {
Deno.test(`Encrypt/Decrypt: ${alg}`, async (t) => {
let encryptResult: SimpleEncryption.EncryptedData = {
let encryptResult: SimpleEncryptionType.EncryptedData = {
alg,
data: "(DUMMY)",
iv: "(DUMMY)",
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { checkIvLength } from "./check.ts";
import { DefaultAlg, getKey, getRandomBytes } from "./common.ts";
import { Hex } from "./hex.ts";
import { SimpleEncryption } from "./types.d.ts";
import { SimpleEncryptionType } from "./types.d.ts";

export const encrypt = async (
args: SimpleEncryption.EncryptArgs,
): Promise<SimpleEncryption.EncryptedData> => {
args: SimpleEncryptionType.EncryptArgs,
): Promise<SimpleEncryptionType.EncryptedData> => {
const crypto: Crypto = args.crypto ?? globalThis.crypto;
const alg: SimpleEncryption.SupportAlgorithm = args.alg ?? DefaultAlg;
const alg: SimpleEncryptionType.SupportAlgorithm = args.alg ?? DefaultAlg;
const plainData: Uint8Array = args.plainData;
const key: CryptoKey = await getKey(Hex.toBin(args.key), alg, crypto);
const iv: Uint8Array = checkIvLength(
Expand All @@ -28,8 +28,8 @@ export const encrypt = async (
};

export const decrypt = async (
args: SimpleEncryption.DecryptArgs,
): Promise<SimpleEncryption.DecryptedData> => {
args: SimpleEncryptionType.DecryptArgs,
): Promise<SimpleEncryptionType.DecryptedData> => {
const crypto: Crypto = args.crypto ?? globalThis.crypto;
const { alg } = args;
const encryptedData: Uint8Array = Hex.toBin(args.data);
Expand Down
4 changes: 2 additions & 2 deletions src/test/01_flow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SimpleEncryption } from "../types.d.ts";
import { SimpleEncryptionType } from "../types.d.ts";
import { assert, DecryptFunc, EncryptFunc, Test } from "./common.ts";

const KEY = "2dc4104a50a08a41f53d3a6f10700f9660833ad2b369660ad24aa8cbf1657544";
Expand Down Expand Up @@ -30,7 +30,7 @@ export const test_01_4_FlowWithAesCbcWithoutIv: Test = {
};

interface FlowArgs {
alg: SimpleEncryption.SupportAlgorithm;
alg: SimpleEncryptionType.SupportAlgorithm;
decrypt: DecryptFunc;
encrypt: EncryptFunc;
iv?: string;
Expand Down
10 changes: 5 additions & 5 deletions src/test/02_encrypt.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { encrypt } from "../index.ts";
import { SimpleEncryption } from "../types.d.ts";
import { SimpleEncryptionType } from "../types.d.ts";
import { assert, Test } from "./common.ts";

const KEY = "b2880691b37d3e98417f59f6bbbc2704bc1f350e90f4c1c8d229a98e9086bac0";
const IV = "0e506606bf986f11f8582a736078725c";
const PlainData = "e8476638-38e4-47a8-9236-bf53d55aae13";

const EncryptedDataWithAesGcm: SimpleEncryption.EncryptedData = {
const EncryptedDataWithAesGcm: SimpleEncryptionType.EncryptedData = {
alg: "AES-GCM",
data:
"c2210c34ebc5259b2843409c74735ac59a1e2caf5940e83a0f6ca7d35d3711e18876eaf3bf997f8ad98638028b7e33bef6099da2",
iv: IV,
};

const EncryptedDataWithAesCbc: SimpleEncryption.EncryptedData = {
const EncryptedDataWithAesCbc: SimpleEncryptionType.EncryptedData = {
alg: "AES-CBC",
data:
"7bc4f92b6e0633f084023e4a1ead3c7cc95ec9d79fe34fe536e5c5c4d24c50eeecc34966a705157f2237fe202ddf6a70",
Expand All @@ -33,8 +33,8 @@ export const test_02_2_EncryptWithAesCbc: Test = {
};

const enc = async (
alg: SimpleEncryption.SupportAlgorithm,
expectData: SimpleEncryption.EncryptedData,
alg: SimpleEncryptionType.SupportAlgorithm,
expectData: SimpleEncryptionType.EncryptedData,
): Promise<void> => {
const encryptResult = await encrypt({
alg,
Expand Down
8 changes: 4 additions & 4 deletions src/test/03_decrypt.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { decrypt } from "../index.ts";
import { SimpleEncryption } from "../types.d.ts";
import { SimpleEncryptionType } from "../types.d.ts";
import { assert, Test } from "./common.ts";

const KEY = "b2880691b37d3e98417f59f6bbbc2704bc1f350e90f4c1c8d229a98e9086bac0";
const IV = "42b2ec4982b3e429f3d49159478c380f";
const PlainData = "f022bb62-d10a-480b-be8a-000584596690";

const EncryptedDataWithAesGcm: SimpleEncryption.DecryptArgs = {
const EncryptedDataWithAesGcm: SimpleEncryptionType.DecryptArgs = {
alg: "AES-GCM",
data:
"4fe66ad71d93c4b87f77fc8f234aa0f1a8a3abb78433438c3500832e83d2971f5c4f0b16d2b769cf7ef22bc120a3360ef0ad8025",
key: KEY,
iv: IV,
};

const EncryptedDataWithAesCbc: SimpleEncryption.DecryptArgs = {
const EncryptedDataWithAesCbc: SimpleEncryptionType.DecryptArgs = {
alg: "AES-CBC",
data:
"9cc48e3992dc10f6c7c19f9e95e8373ad0708b40ed6b867b0b6861775c0955a9aa2449edae5a4155e22cb8bbe1f94d0d",
Expand All @@ -35,7 +35,7 @@ export const test_03_2_DecryptWithAesCbc: Test = {
};

const dec = async (
decryptArgs: SimpleEncryption.DecryptArgs,
decryptArgs: SimpleEncryptionType.DecryptArgs,
): Promise<void> => {
const { plainData } = await decrypt(decryptArgs);
assert(new TextDecoder().decode(plainData), PlainData);
Expand Down
10 changes: 5 additions & 5 deletions src/test/common.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { SimpleEncryption } from "../types.d.ts";
import { SimpleEncryptionType } from "../types.d.ts";

export type EncryptFunc = (
args: SimpleEncryption.EncryptArgs,
args: SimpleEncryptionType.EncryptArgs,
crypto?: Crypto,
) => Promise<SimpleEncryption.EncryptedData>;
) => Promise<SimpleEncryptionType.EncryptedData>;

export type DecryptFunc = (
args: SimpleEncryption.DecryptArgs,
args: SimpleEncryptionType.DecryptArgs,
crypto?: Crypto,
) => Promise<SimpleEncryption.DecryptedData>;
) => Promise<SimpleEncryptionType.DecryptedData>;

export interface TestSubjects {
encrypt: EncryptFunc;
Expand Down
10 changes: 5 additions & 5 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export declare function encrypt(
args: SimpleEncryption.EncryptArgs,
): Promise<SimpleEncryption.EncryptedData>;
args: SimpleEncryptionType.EncryptArgs,
): Promise<SimpleEncryptionType.EncryptedData>;

export declare function decrypt(
args: SimpleEncryption.DecryptArgs,
): Promise<SimpleEncryption.DecryptedData>;
args: SimpleEncryptionType.DecryptArgs,
): Promise<SimpleEncryptionType.DecryptedData>;

export namespace SimpleEncryption {
export namespace SimpleEncryptionType {
type HexString = string;
type SupportAlgorithm = "AES-GCM" | "AES-CBC";

Expand Down

0 comments on commit 2296ad6

Please sign in to comment.