Skip to content

Commit

Permalink
Release v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico committed Apr 14, 2020
1 parent 5fe1988 commit 77a3b81
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 19 deletions.
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# Argon2 for Deno

[Argon2](https://github.com/P-H-C/phc-winner-argon2) encryption library for [Deno](https://deno.land).

It uses [rust-argon2](https://github.com/sru-systems/rust-argon2) under the hood.

## API

- `hash(password: string, options?: HashOptions): Promise<string>`

- `verify(hash: string, password: string): Promise<boolean>`

### Error handling

In case of error, all methods of this library will throw an [`Argon2Error`](src/error.ts) type.

## Usage
```ts
import { assert } from "https://deno.land/std/testing/asserts.ts";
Expand All @@ -13,15 +24,27 @@ let hash = await hash("test");
assert(await verify(hash, "test"));
```

## API
### CLI

- `hash(password: string, options: HashOptions): Promise<string>`
It is possible to install deno-argon2 as a CLI tool insatiable via `deno install`.

- `verify(hash: string, password: string): Promise<boolean>`
<details>

### Error handling
<summary>Installation snippet</summary>

In case of error, all methods of this library will throw an [`Argon2Error`](src/error.ts) type.
```sh
deno install \
--allow-env \
--allow-run \
--allow-read \
--allow-write \
--allow-plugin \
--allow-net \
argon2 https://deno.land/x/argon2/cli/mod.ts
```
</details>

After install run `--help` to inspect all possible commands.

## Permissions

Expand All @@ -33,6 +56,7 @@ This library automatically download the static library and initialize Deno plugi
deno \
--allow-read .deno_plugins \
--allow-write .deno_plugins \
--allow-net
--allow-plugin \
src/mod.ts
```
Expand Down
70 changes: 70 additions & 0 deletions cli/commands/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Command, encode, argon2 } from "../deps.ts";
import { readStdin } from "../util.ts";

export let hash = new Command()
.version(argon2.version())
.description("Hash a new password or verify an already existing one.")
.option("-s, --salt <arg:string>", "")
.option("-S, --secret <arg:string>", "")
.option("-m, --memory-cost <arg:number>", "")
.option("-t, --time-cost <arg:number>", "")
.option("-l, --lanes <arg:number>", "")
.option("-T, --thread-mode <arg:thread-mode>", "")
.option("-v, --variant <arg:variant>", "")
.option("-d, --data <arg:json>", "")
.type("thread-mode", (option, _, value) => {
switch (value) {
case "sequential": {
return argon2.ThreadMode.Sequential;
}
case "parallel": {
return argon2.ThreadMode.Parallel;
}
case undefined: {}
default: {
throw new Error(
`Option --${option.name} must be either "sequential" or "parallel": ${value}`,
);
}
}
})
.type("variant", (option, _, value) => {
switch (value) {
case argon2.Variant.Argon2i:
case argon2.Variant.Argon2d:
case argon2.Variant.Argon2id: {
return value;
}
case undefined: {}
default: {
throw new Error(
`Option --${option.name} must be either "${argon2.Variant.Argon2i}", "${argon2.Variant.Argon2d}" or "${argon2.Variant.Argon2id}": ${value}`,
);
}
}
})
.type("json", (option, _, value) => {
try {
if (value !== undefined) {
return JSON.parse(value);
}
} catch (_) {
throw new Error(
`Option --${option.name} must be a valid json object: ${value}`,
);
}
})
.action(async (options) => {
let password = await readStdin();

console.log(await argon2.hash(password, {
salt: options.salt ? encode(options.salt) : undefined,
secret: options.secret ? encode(options.secret) : undefined,
memoryCost: options.memoryCost ? options.memoryCost : undefined,
timeCost: options.timeCost ? options.timeCost : undefined,
lanes: options.lanes ? options.lanes : undefined,
threadMode: "threadMode" in options ? options.threadMode : undefined,
variant: options.variant ? options.variant : undefined,
data: options.data ? options.data : undefined,
}));
});
13 changes: 13 additions & 0 deletions cli/commands/verify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Command, argon2 } from "../deps.ts";

import { readStdin } from "../util.ts";

export let verify = new Command()
.version(argon2.version())
.description("Hash a new password or verify an already existing one.")
.option("-H, --hash <arg:string>", "", { required: true })
.action(async (options) => {
let password = await readStdin();

console.log(await argon2.verify(options.hash, password));
});
5 changes: 5 additions & 0 deletions cli/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { Command } from "https://deno.land/x/cliffy/command.ts";

export { encode, decode } from "../src/deps.ts";

export * as argon2 from "../src/mod.ts";
11 changes: 11 additions & 0 deletions cli/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Command, argon2 } from "./deps.ts";

import { hash } from "./commands/hash.ts";
import { verify } from "./commands/verify.ts";

await new Command()
.version(argon2.version())
.description("Hash a new password or verify an existing one.")
.command("hash", hash)
.command("verify", verify)
.parse(Deno.args);
5 changes: 5 additions & 0 deletions cli/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { decode } from "./deps.ts";

export async function readStdin() {
return decode(await Deno.readAll(Deno.stdin));
}
4 changes: 4 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ export interface HashOptions<T extends {} = {}> {
threadMode: ThreadMode;
lanes: number;
}

export function version() {
return "0.3.0";
}
6 changes: 6 additions & 0 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { encode, decode } from "https://deno.land/std/encoding/utf8.ts";

export {
prepare,
PreprareOptions,
} from "https://deno.land/x/plugin_prepare@v0.3.1/mod.ts";
14 changes: 8 additions & 6 deletions src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { encode, decode } from "https://deno.land/std/encoding/utf8.ts";

import {
prepare,
PreprareOptions,
} from "https://deno.land/x/plugin_prepare@v0.3.1/mod.ts";
import { encode, decode, prepare, PreprareOptions } from "./deps.ts";

import { MIN_SALT_SIZE, HashOptions } from "./common.ts";
import { Argon2ErrorType, Argon2Error } from "./error.ts";
Expand Down Expand Up @@ -58,6 +53,13 @@ export async function installPlugin(
) {
let plugin = await preparing;

if (typeof password !== "string") {
throw new Argon2Error(
Argon2ErrorType.InvalidInput,
"Password argument must be a string.",
);
}

let salt = options.salt
? options.salt
: crypto.getRandomValues(
Expand Down
15 changes: 8 additions & 7 deletions src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { HashOptions } from "./common.ts";
import { HashOptions, version } from "./common.ts";
import { installPlugin } from "./internal.ts";

export * from "./common.ts";
export * from "./error.ts";

export let version = "0.2.0";

let plugin = await installPlugin(`https://github.com/fdionisi/deno-argon2/releases/download/v${version}`, {
printLog: false,
checkCache: true,
});
let plugin = await installPlugin(
`https://github.com/fdionisi/deno-argon2/releases/download/v${version()}`,
{
printLog: false,
checkCache: true,
},
);

/**
* Hash a string.
Expand Down
3 changes: 3 additions & 0 deletions tests/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env -S deno --allow-env --allow-run --allow-read --allow-write --allow-net --allow-plugin

import "../cli/mod.ts";
2 changes: 1 addition & 1 deletion tests/deno.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,4 @@ Deno.test({
},
});

Deno.runTests()
Deno.runTests();

0 comments on commit 77a3b81

Please sign in to comment.