Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README and CI workflows #29

Merged
merged 43 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
546b41f
Remove explicit IV
mryhryki Aug 5, 2023
0618e34
Update README.md
mryhryki Aug 5, 2023
85011e4
Update .github/workflows/check_deno.yml
mryhryki Aug 5, 2023
a2cee96
Update .github/workflows/check_node16.yml
mryhryki Aug 5, 2023
83fb993
Rename .github/workflows/check_node.yml
mryhryki Aug 5, 2023
d105dc7
Delete .github/workflows/check_node18.yml
mryhryki Aug 5, 2023
96126be
Delete .github/workflows/check_node20.yml
mryhryki Aug 5, 2023
e7efe10
Update .github/workflows/check_node.yml
mryhryki Aug 5, 2023
dac20a3
Update .github/workflows/check_node.yml
mryhryki Aug 5, 2023
07a2f37
Update package.json
mryhryki Aug 5, 2023
9bf57bf
Add .editorconfig
mryhryki Aug 5, 2023
7a7a5df
Update .github/workflows/check_code.yml
mryhryki Aug 5, 2023
a197358
Add ESLint
mryhryki Aug 5, 2023
b09b114
Update .github/workflows/check_code.yml
mryhryki Aug 5, 2023
aebaad0
Revert "Add ESLint"
mryhryki Aug 5, 2023
79db2bb
Update .github/workflows/check_code.yml
mryhryki Aug 5, 2023
62ecf83
Update .github/workflows/publish_to_npm.yml
mryhryki Aug 5, 2023
cfb3b16
Update src/hex.test.ts
mryhryki Aug 5, 2023
2634dc9
Fix errors by `deno lint`
mryhryki Aug 5, 2023
a11a88e
Build by deno_emit
mryhryki Aug 5, 2023
c91527f
Define tasks
mryhryki Aug 5, 2023
2f55fad
Update src/bundle.ts
mryhryki Aug 5, 2023
4a03e34
Update deno.jsonc
mryhryki Aug 5, 2023
3066986
Update .github/workflows/check_node.yml
mryhryki Aug 5, 2023
11ed6fa
Rename src/script/bundle.ts
mryhryki Aug 5, 2023
76f61f0
Fix test by note
mryhryki Aug 5, 2023
860fbc5
Update .gitignore
mryhryki Aug 5, 2023
4da97a6
Update src/test/index_node.js
mryhryki Aug 5, 2023
758c417
Update README.md
mryhryki Aug 5, 2023
9c48bfe
Update src/types.d.ts
mryhryki Aug 5, 2023
9ca4f2f
Update src/index.ts
mryhryki Aug 5, 2023
f17d56c
Update src/types.d.ts
mryhryki Aug 5, 2023
0d374c2
Update README.md
mryhryki Aug 5, 2023
d93a94e
Update README.md
mryhryki Aug 5, 2023
a4a6493
Change export type name
mryhryki Aug 5, 2023
021dec5
Update deno.json
mryhryki Aug 5, 2023
aff7f9d
Update script/bundle.ts
mryhryki Aug 5, 2023
ac9055d
Update .gitignore
mryhryki Aug 5, 2023
4a2d624
Update script/bundle_test.ts
mryhryki Aug 5, 2023
d27ea65
Update package.json
mryhryki Aug 5, 2023
310177a
Update .github/workflows/check_code.yml
mryhryki Aug 5, 2023
fa049c3
Update .gitignore
mryhryki Aug 5, 2023
ab057ac
Update .github/workflows/publish_to_npm.yml
mryhryki Aug 5, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[{*.html, *.css, *.scss, *.cjs, *.js, *.jsx, *.json, *.ts, *.tsx}]
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2
max_line_length = 120
13 changes: 8 additions & 5 deletions .github/workflows/check_code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ jobs:
deno-version: v1.x
- name: Show environment
run: set -x; pwd; ls -la; deno --version;

- name: Test
run: deno test ./src/
- name: Check
run: deno check ./src/test/index.ts
run: deno task check
- name: Lint
run: deno task lint
- name: Format
run: deno fmt ./src/
run: deno task format
- name: Test
run: deno task test
- name: Build
run: deno task build
4 changes: 1 addition & 3 deletions .github/workflows/check_deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ jobs:
- name: Show environment
run: set -x; pwd; ls -la; deno --version;
- name: Run test
run: |
deno test
deno run ./src/test/index_deno.ts
run: deno run ./src/test/index_deno.ts
36 changes: 36 additions & 0 deletions .github/workflows/check_node.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: check_node

on: push

jobs:
check:
runs-on: ubuntu-latest
permissions:
contents: read
strategy:
matrix:
node-version:
- 16
- 18
- 20
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Setup Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: Show environment
run: set -x; pwd; ls -la; node -v; npm -v;
- name: Run test with packed tarball
run: |
set -xe
npm pack
TARBALL="$(ls -1 mryhryki-simple-encryption-*.tgz | head -n 1)"
npm install "./${TARBALL}"
deno task build:test
node src/test/index_node.js
26 changes: 0 additions & 26 deletions .github/workflows/check_node16.yml

This file was deleted.

26 changes: 0 additions & 26 deletions .github/workflows/check_node18.yml

This file was deleted.

26 changes: 0 additions & 26 deletions .github/workflows/check_node20.yml

This file was deleted.

8 changes: 6 additions & 2 deletions .github/workflows/publish_to_npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ jobs:
uses: actions/checkout@v3
- name: Setup Node
uses: actions/setup-node@v3
- name: Setup Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: Show environment
run: set -x; pwd; ls -la; node -v; npm -v;
- name: Run npm install
run: set -x; pwd; ls -la; node -v; npm -v; deno --version;
- name: Install dependencies
run: npm install
- name: Configure git
run: |
Expand Down
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
dist/
*.tgz
mryhryki-simple-encryption-*.tgz
deno.lock
node_modules/
package-lock.json
package/
src/index.js
src/test/index.js
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
.gitignore
.idea/
.vscode/
deno.*
docs/
package-lock.json
script/
68 changes: 35 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ $ npm install @mryhryki/simple-encryption
```

Set `"type": "module"` in your `package.json`.
(If you use bundler (webpack, esbuild, etc.), you may don't need to set this.)

```shell
# Check settings
Expand All @@ -79,21 +80,24 @@ $ cat package.json | grep '"type":'
Add `index.js` file:

```javascript
// index.js
import { webcrypto as crypto } from "crypto";
import { decrypt, encrypt } from "@mryhryki/simple-encryption";
// If you are using Node.js v19 and later, there is `crypto` in `globalThis`, you don't need to import crypto.
//
// Ref:
// - https://github.com/nodejs/node/pull/42083
// - https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V19.md#19.0.0
import {webcrypto as crypto} from "crypto";
import {decrypt, encrypt} from "@mryhryki/simple-encryption";

(async () => {
const key = "522a432195523d9f8cb65ee85c42e06f6e4f1839e8e6cf11a19631600e17d726"; // This value is sample
const iv = "a7dd2a80bd982113ba5fe7a77a6b22b7"; // Optional
const plainData = new TextEncoder().encode("cf0f2168-ddfc-4c98-be81-1d34e660dd1a"); // Use TextEncoder if you want to encrypt string

// Encrypt
const encryptResult = await encrypt({ key, iv, plainData }, crypto);
const encryptResult = await encrypt({key, iv, plainData, crypto});
console.log("Encrypt Result:", JSON.stringify(encryptResult, null, 2));

// Decrypt
const decryptResult = await decrypt({ ...encryptResult, key }, crypto);
const decryptResult = await decrypt({...encryptResult, key, crypto});
console.log("Decrypt Result:", new TextDecoder().decode(decryptResult.plainData)); // Use TextDecoder if you want to decrypt as string
})();
```
Expand All @@ -116,21 +120,20 @@ Add `index.js` file:

```javascript
// index.js
import { decrypt, encrypt } from "npm:@mryhryki/simple-encryption";
import {decrypt, encrypt} from "npm:@mryhryki/simple-encryption";
// or Using CDN
// import { decrypt, encrypt } from "https://cdn.skypack.dev/@mryhryki/simple-encryption";
// import { decrypt, encrypt } from "https://esm.sh/@mryhryki/simple-encryption";

const key = "522a432195523d9f8cb65ee85c42e06f6e4f1839e8e6cf11a19631600e17d726"; // This value is sample
const iv = "a7dd2a80bd982113ba5fe7a77a6b22b7"; // Optional
const plainData = new TextEncoder().encode("cf0f2168-ddfc-4c98-be81-1d34e660dd1a"); // Use TextEncoder if you want to encrypt string

// Encrypt
const encryptResult = await encrypt({ key, iv, plainData });
const encryptResult = await encrypt({key, iv, plainData});
console.log("Encrypt Result:", JSON.stringify(encryptResult, null, 2));

// Decrypt
const decryptResult = await decrypt({ ...encryptResult, key });
const decryptResult = await decrypt({...encryptResult, key});
console.log("Decrypt Result:", new TextDecoder().decode(decryptResult.plainData)); // Use TextDecoder if you want to decrypt as string
```

Expand All @@ -152,20 +155,19 @@ Add `index.js` file:

```javascript
(async () => {
const { encrypt, decrypt } = await import("https://cdn.skypack.dev/@mryhryki/simple-encryption");
const {encrypt, decrypt} = await import("https://cdn.skypack.dev/@mryhryki/simple-encryption");
// or
// const {encrypt, decrypt} = await import("https://esm.sh/@mryhryki/simple-encryption")

const key = "522a432195523d9f8cb65ee85c42e06f6e4f1839e8e6cf11a19631600e17d726"; // This value is sample
const iv = "a7dd2a80bd982113ba5fe7a77a6b22b7"; // Optional
const plainData = new TextEncoder().encode("cf0f2168-ddfc-4c98-be81-1d34e660dd1a"); // Use TextEncoder if you want to encrypt string

// Encrypt
const encryptResult = await encrypt({ key, iv, plainData });
const encryptResult = await encrypt({key, iv, plainData});
console.log("Encrypt Result:", JSON.stringify(encryptResult, null, 2));

// Decrypt
const decryptResult = await decrypt({ ...encryptResult, key });
const decryptResult = await decrypt({...encryptResult, key});
console.log("Decrypt Result:", new TextDecoder().decode(decryptResult.plainData)); // Use TextDecoder if you want to decrypt as string
})();
```
Expand Down Expand Up @@ -196,14 +198,13 @@ Decrypt Result: cf0f2168-ddfc-4c98-be81-1d34e660dd1a

#### Arguments

| Name | Type | Required | Description |
|------------------|-------------------------------------------------------------|----------|-------------------------------------------------------------|
| `args` | `object` | Yes | Arguments object. |
| `args.alg` | `string` | No | Algorithm name: `AES-GCM` or `AES-CBC` (Default: `AES-GCM`) |
| `args.iv` | `string` (Hex) | No | Initial vector. |
| `args.key` | `string` (Hex) | Yes | Your secret key. |
| `args.plainData` | `Uint8Array` | Yes | Plain data you want to encrypt. |
| `crypto` | [Crypto](https://developer.mozilla.org/docs/Web/API/Crypto) | No | Crypto object. Required if using Node.js. |
| Name | Type | Required | Description |
|-------------|-------------------------------------------------------------|----------|-------------------------------------------------------------|
| `alg` | `string` | No | Algorithm name: `AES-GCM` or `AES-CBC` (Default: `AES-GCM`) |
| `iv` | `string` (Hex) | No | Initial vector. DON'T specify this if you don't need. |
| `key` | `string` (Hex) | Yes | Your secret key. |
| `plainData` | `Uint8Array` | Yes | Plain data you want to encrypt. |
| `crypto` | [Crypto](https://developer.mozilla.org/docs/Web/API/Crypto) | No | Crypto object. Required if using Node.js (<19.x). |

#### Return Value

Expand All @@ -217,14 +218,13 @@ Decrypt Result: cf0f2168-ddfc-4c98-be81-1d34e660dd1a

#### Arguments

| Name | Type | Required | Description |
|-------------|-------------------------------------------------------------|----------|---------------------------------------------------------------------------------------|
| `args` | `object` | Yes | Arguments object. |
| `args.alg` | `string` | Yes | Algorithm name: `AES-GCM` or `AES-CBC`. Must specify same value as during encryption. |
| `args.data` | `string` (Hex) | Yes | Encrypted data. |
| `args.iv` | `string` (Hex) | Yes | Initial vector. Must specify same value as during encryption. |
| `args.key` | `string` (Hex) | Yes | Your secret key. Must specify same value as during encryption. |
| `crypto` | [Crypto](https://developer.mozilla.org/docs/Web/API/Crypto) | No | Crypto object. Required if using Node.js. |
| Name | Type | Required | Description |
|----------|-------------------------------------------------------------|----------|---------------------------------------------------------------------------------------|
| `alg` | `string` | Yes | Algorithm name: `AES-GCM` or `AES-CBC`. Must specify same value as during encryption. |
| `data` | `string` (Hex) | Yes | Encrypted data. |
| `iv` | `string` (Hex) | Yes | Initial vector. Must specify same value as during encryption. |
| `key` | `string` (Hex) | Yes | Your secret key. Must specify same value as during encryption. |
| `crypto` | [Crypto](https://developer.mozilla.org/docs/Web/API/Crypto) | No | Crypto object. Required if using Node.js (<19.x). |

#### Return Value

Expand All @@ -234,6 +234,8 @@ Decrypt Result: cf0f2168-ddfc-4c98-be81-1d34e660dd1a

## Development

1. Install [Deno](https://deno.land/manual@v1.35.0/getting_started/installation)
2. Editing source code.
3. Push to GitHub and create Pull Request. (CI will run test)
1. Fork this repository.
2. Install [Deno](https://deno.land/manual@v1.35.0/getting_started/installation).
3. Edit source code.
4. Run test by `npm test`.
5. Push to GitHub and create Pull Request, so CI will run tests.
11 changes: 11 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"tasks": {
"build": "deno run --allow-env --allow-read --allow-write --allow-net ./script/bundle.ts",
"build:test": "deno run --allow-env --allow-read --allow-write --allow-net ./script/bundle_test.ts",
"check": "deno check --all ./src/index.ts",
"format": "deno fmt --check ./src/ ./script/",
"lint": "deno lint ./src/ ./script/",
"test": "deno test ./src/",
"test:watch": "deno test --watch ./src/"
}
}
14 changes: 2 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@mryhryki/simple-encryption",
"version": "0.0.9",
"main": "dist/index.js",
"main": "src/index.js",
"types": "src/types.d.ts",
"type": "module",
"author": {
Expand Down Expand Up @@ -33,16 +33,6 @@
"crypto"
],
"scripts": {
"build": "esbuild --bundle --platform=neutral --outfile=./dist/index.js ./src/index.ts",
"check": "deno check ./src/test/index.ts",
"fmt": "deno fmt ./src/",
"prepare": "npm run build",
"test": "deno test ./src/",
"test:deno": "deno run src/test/index_deno.ts",
"test:node": "esbuild --bundle --platform=node ./src/test/index_node.ts | node",
"test:node_with_packed_tarball": "npm pack && mv ./mryhryki-simple-encryption-*.tgz ./packed.tgz && npm install ./packed.tgz && esbuild --bundle --platform=node ./src/test/index_node.ts | node"
},
"devDependencies": {
"esbuild": "^0.18.15"
"prepare": "deno task build"
}
}
4 changes: 4 additions & 0 deletions script/bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { bundle } from "https://deno.land/x/emit/mod.ts";

const { code } = await bundle("./src/index.ts");
await Deno.writeTextFile("./src/index.js", code);
4 changes: 4 additions & 0 deletions script/bundle_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { bundle } from "https://deno.land/x/emit/mod.ts";

const { code } = await bundle("./src/test/index.ts");
await Deno.writeTextFile("./src/test/index.js", code);
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