Skip to content

Commit

Permalink
Merge pull request #22 from dojyorin/dev
Browse files Browse the repository at this point in the history
feat and improved.
  • Loading branch information
dojyorin authored Nov 28, 2022
2 parents 109e9ba + 7fda735 commit de09796
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const decoded = base64Decode(encoded); // Restored.
```ts
const file = await Deno.readFile("/path/to/binary.bin");

const random = await cryptoRandom(16); // random byte array.
const hash = await cryptoHash(true, file); // byte array of SHA2 512 bits hash value.
const keyEcdh = await cryptoGenerateKey(true); // public/private key pair for ECDH, each in byte array.
const keyEcdsa = await cryptoGenerateKey(false); // public/private key pair for ECDSA, each in byte array.
Expand Down Expand Up @@ -120,6 +121,19 @@ The actual binary structure looks like this:

This is for one file and repeats for the number of files.

# Browser Compatible
Some methods and classes in this module don't use `globalThis.Deno` internally and are browser compatible.

I have prepared browser compatible code only export as [mod.compatible.ts](./mod.compatible.ts).

By bundling this, you can easily create universal utility scripts.

```sh
deno bundle https://deno.land/x/simple_utility@(version)/mod.compatible.ts > ./simple_utility.esm.js
```

This section may eventually be automated with GitHub Actions, in which case the bundled scripts will be merged into GitHub Releases, making this step unnecessary.

# Tips
This section is not directly related to this module, but provides a few line snippets to help you implement your application.

Expand All @@ -138,18 +152,5 @@ const {default: data} = await import("./data.json", {assert: {type: "json"}});
</details>
</p>

# Browser Compatible
Some methods and classes in this module don't use `globalThis.Deno` internally and are browser compatible.

I have prepared browser compatible code only export as [mod.compatible.ts](./mod.compatible.ts).

By bundling this, you can easily create universal utility scripts.

```sh
deno bundle https://deno.land/x/simple_utility@(version)/mod.compatible.ts > ./simple_utility.esm.js
```

This section may eventually be automated with GitHub Actions, in which case the bundled scripts will be merged into GitHub Releases, making this step unnecessary.

# API
See [Deno Document](https://deno.land/x/simple_utility/mod.ts) for details.
7 changes: 6 additions & 1 deletion src/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ export type SyncFunction<T extends unknown = void> = () => T;
/**
* Function returning arbitrary async types.
*/
export type AsyncFunction<T extends unknown = void> = SyncFunction<Promise<T>>;
export type AsyncFunction<T extends unknown = void> = SyncFunction<Promise<T>>;

/**
* The file name and byte array pairs that make up the basic file.
*/
export type FileInit = [string, Uint8Array];
15 changes: 12 additions & 3 deletions src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ async function deriveSecretKey(kp:PortableCryptoKeyPair){
return await crypto.subtle.deriveKey(dh, privateKey, aes, false, ["encrypt", "decrypt"]);
}

/**
* Returns random byte array with the specified number of bytes.
* @param size number of bytes.
* @return random byte array.
*/
export async function cryptoRandom(size:number){
return await new Promise<Uint8Array>(done => done(crypto.getRandomValues(new Uint8Array(size))));
}

/**
* Derive SHA2 hash value from byte array.
* @param is512 Use the hash length 512 bits if `true`, 256 bits if `false`.
Expand Down Expand Up @@ -81,7 +90,7 @@ export async function cryptoEncrypt(kp:PortableCryptoKeyPair, data:Uint8Array){
const gcm:AesGcmParams = {
name: "AES-GCM",
tagLength: sizeTag * 8,
iv: crypto.getRandomValues(new Uint8Array(sizeIv))
iv: await cryptoRandom(sizeIv)
};

const secretKey = await deriveSecretKey(kp);
Expand All @@ -107,9 +116,9 @@ export async function cryptoDecrypt(kp:PortableCryptoKeyPair, data:Uint8Array){
iv: data.subarray(0, sizeIv)
};

const commonKey = await deriveSecretKey(kp);
const secretKey = await deriveSecretKey(kp);

return new Uint8Array(await crypto.subtle.decrypt(gcm, commonKey, data.subarray(sizeIv)));
return new Uint8Array(await crypto.subtle.decrypt(gcm, secretKey, data.subarray(sizeIv)));
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/minipack.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import {type FileInit} from "./core.d.ts";
import {cryptoHash} from "./crypto.ts";
import {ucEncode, ucDecode, hexEncode} from "./text.ts";

/**
* The file name and byte array pairs that make up the basic file.
*/
export type FileInit = [string, Uint8Array];

const sizeHash = 32;
const sizeName = 1;
const sizeBody = 4;
Expand Down
11 changes: 10 additions & 1 deletion 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 {cryptoHash, cryptoGenerateKey, cryptoEncrypt, cryptoDecrypt, cryptoSign, cryptoVerify} from "../src/crypto.ts";
import {cryptoRandom, cryptoHash, cryptoGenerateKey, cryptoEncrypt, cryptoDecrypt, cryptoSign, cryptoVerify} from "../src/crypto.ts";

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

Expand All @@ -14,6 +14,15 @@ const hashResult = new Uint8Array([
0x0D, 0xE6, 0x31, 0xDA, 0x4B, 0xBD, 0xD4, 0x58
]);

Deno.test({
name: "Crypto: Random",
async fn(){
const random = await cryptoRandom(16);

assertEquals(random.byteLength, 16);
}
});

Deno.test({
name: "Crypto: Hash",
async fn(){
Expand Down

0 comments on commit de09796

Please sign in to comment.