-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Federico
committed
Apr 14, 2020
1 parent
5fe1988
commit 77a3b81
Showing
12 changed files
with
163 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,4 +154,4 @@ Deno.test({ | |
}, | ||
}); | ||
|
||
Deno.runTests() | ||
Deno.runTests(); |