Skip to content

Commit

Permalink
Update output
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeder committed Apr 19, 2023
1 parent ceeb5e4 commit 4e98954
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 72 deletions.
125 changes: 76 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,52 @@ Scrypt Key Derivation Function in Rust

## Usage

```bash
Usage: scrypt-kdf [options]
Version: v0.7.0
### General

```sh
Usage: scrypt-kdf [COMMAND]

Commands:
derive Derive a value using Scrypt KDF
test-vectors Print test vectors

Options:
-i, --iterations ITER
set the number of required iterations (default: 100)
-n, --workFactor N set the work factor (default: 1048576)
-r, --blocksize R set the blocksize parameter (default: 8)
-p, --parallel P set the parallelization parameter (default: 1)
-k, --keysize SIZE set the length of the derived (default: 16)
-t, --test print test vectors
-h, --help print this help menu
-v, --version print version information
-h, --help Print help
-V, --version Print version
```

### Deriving

```sh
Derive a value using Scrypt KDF

Usage: scrypt-kdf derive --iterations <ITERATIONS> --log-n <LOG_N> --r <R> --p <P> --l <L>

Options:
-i, --iterations <ITERATIONS> Number of iterations [default: 100]
-l, --log-n <LOG_N> Work factor [default: 20]
-r, --r <R> Block size [default: 8]
-p, --p <P> Parallelization parameter [default: 1]
-l, --l <L> Length of the derived result [default: 16]
-h, --help Print help
```

### Printing Test Vectors

```sh
Print test vectors

Usage: scrypt-kdf test-vectors

Options:
-h, --help Print help
```

## Build

### Mac OS

```bash
```sh
git clone https://github.com/lbeder/scrypt-kdf
cd scrypt-kdf

Expand All @@ -37,26 +62,26 @@ cargo build --release

In order to get stuff working later, use the `nightly` branch of Rust:

```bash
```sh
rustup override set nightly
```

Install a standard Linux target on a Mac (note, that the opposite is currently impossible):

```bash
```sh
rustup target add x86_64-unknown-linux-musl
```

Use `homebrew` to install a community-provided macOS cross-compiler toolchains:

```bash
```sh
brew tap messense/macos-cross-toolchains
brew install x86_64-unknown-linux-musl
```

Now you can build it:

```bash
```sh
export CC_x86_64_unknown_linux_musl=x86_64-unknown-linux-musl-gcc
export CXX_x86_64_unknown_linux_musl=x86_64-unknown-linux-musl-g++
export AR_x86_64_unknown_linux_musl=x86_64-unknown-linux-musl-ar
Expand All @@ -69,18 +94,12 @@ cargo build --target=x86_64-unknown-linux-musl

Let's try to derive the key for the secret `test`, using the salt `salt`:

> ./target/release/scrypt-kdf
> scrypt-kdf derive
```bash
Enter your
Scrypt KDF v0.7.0
```sh
Scrypt KDF v0.10.0

Deriving with settings:
CPU/memory cost parameter (N): 1048576
Block size parameter (R): 8
Parallelization parameter (P): 1
Iterations: 100
Key size: 16
Parameters: Scrypt (log_n: 20, r: 8, p: 1, len: 16)

Enter your salt: salt
Enter your secret: 🔑
Expand All @@ -91,14 +110,17 @@ Processing: 12 / 100 [==============>-------------------------------------------

Final result:

```bash
```sh
Enter your salt: salt
Enter your secret: 🔑
Enter your secret again: 🔑

Processing: 100 / 100 [=======================================================================================================================================] 100.00 %
Finished in 5m 6s
Key is (please highlight to see): ff08101f061aa670158601bf5be5efa6

Key is (please highlight to see):
16e27a594f879ec5edbc3c1995907b49

Finished in 3m 29s
```

## Test Vectors
Expand All @@ -112,49 +134,54 @@ Test vectors:
* Secret: "" (the empty string)
* Salt: empty
* Parameters:
* N: 16384
* R: 8
* P: 1
* Key size: 128
* log_n: 14
* r: 8
* p: 1
* length: 64
* Iterations: 1

### #2

* Secret: "Hello World"
* Salt: empty
* Parameters:
* N: 16384
* R: 8
* P: 1
* Key size: 128
* log_n: 14
* r: 8
* p: 1
* length: 64
* Iterations: 3

Results should be:

```bash
Scrypt KDF v0.7.0

```sh
Printing test vectors...

Deriving with settings:
CPU/memory cost parameter (N): 16384
Block size parameter (R): 8
Parallelization parameter (P): 1
CPU/memory cost parameter (log(n)): 14
Block size parameter (r): 8
Parallelization parameter (p): 1
Iterations: 1
Key size: 128
Length: 64

Key for test vector "" is:
d72c87d0f077c7766f2985dfab30e8955c373a13a1e93d315203939f542ff86e73ee37c31f4c4b571f4719fa8e3589f12db8dcb57ea9f56764bb7d58f64cf705f1f64bdd91c35da954a6fb7896f1839e6ba03f68f08b686527f9f1588ab103c22152046258e2d679842252afeb3cd6eb4e01fe9c285eb916da7e4b7a39ee5eba

Deriving with settings:
CPU/memory cost parameter (N): 14
Block size parameter (R): 8
Parallelization parameter (P): 1
CPU/memory cost parameter (log(n)): 14
Block size parameter (r): 8
Parallelization parameter (p): 1
Iterations: 3
Key size: 128
Length: 64

Key for test vector "Hello World" is:
38f3b062f703aa0c958fc8944c9f005f1bd03a056048d5cdc6186979e4c178504050580fab8744c0272253f7df87a2e2f9bb5449a2361f0fed5105ea549e86e41f68d8b160cda5ca91e020067b0c53fc20ae19993e1f40db60d8963ec8c7c0fe74d48a44f1f78a4259f0376f6d7dd2c07d2e7aaae023b8bdfa87ddbf503fe9a3

Test vector parameters: Scrypt (log_n: 14, r: 8, p: 1, iterations: 1, len: 64)
Key for test vector "" is: d72c87d0f077c7766f2985dfab30e8955c373a13a1e93d315203939f542ff86e73ee37c31f4c4b571f4719fa8e3589f12db8dcb57ea9f56764bb7d58f64cf705

Test vector parameters: Scrypt (log_n: 14, r: 8, p: 1, iterations: 3, len: 64)
Key for test vector "Hello World" is: 1487e1ac9c7a63e785b1f3e9560ea749913d50c9797dc6ca8d0db953fe03df1c66af878bd6dcce79884e8b7e3e29f39cb709cd63b7e7f4099d82ab199664eab3

```

## License
Expand Down
28 changes: 5 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crossterm::{
Result,
};
use humantime::format_duration;
use log::{info, Level, LevelFilter};
use pbr::ProgressBar;
use scrypt_kdf::DEFAULT_SCRYPT_KDF_OPTIONS;
use std::{
Expand Down Expand Up @@ -102,21 +101,6 @@ fn main() {
better_panic::install();
color_backtrace::install();

env_logger::builder()
.filter_level(LevelFilter::Info)
.format(|buf, record| {
let level = match record.level() {
Level::Info => Level::Info.to_string().green(),
Level::Warn => Level::Warn.to_string().yellow(),
Level::Error => Level::Error.to_string().red(),
Level::Debug => Level::Debug.to_string().grey(),
Level::Trace => Level::Trace.to_string().grey(),
};

writeln!(buf, "[{}]: {}", level, record.args())
})
.init();

println!("Scrypt KDF v{VERSION}\n");

let cli = Cli::parse();
Expand All @@ -129,7 +113,7 @@ fn main() {
p,
l,
}) => {
info!(
println!(
"Parameters: {} (log_n: {}, r: {}, p: {}, len: {})\n",
"Scrypt".yellow(),
log_n.to_string().cyan(),
Expand Down Expand Up @@ -161,8 +145,8 @@ fn main() {
pb.inc();
});

info!("Key is (please highlight to see): ");
info!("{}", hex::encode(&res as &[u8]).black().on_black());
println!("Key is (please highlight to see): ");
println!("{}", hex::encode(&res as &[u8]).black().on_black());

pb.finish_println(&format!(
"Finished in {}\n",
Expand All @@ -177,7 +161,7 @@ fn main() {
for (i, key) in test_keys.iter().enumerate() {
let opts = &TEST_VECTORS[i].opts;

info!(
println!(
"Test vector parameters: {} (log_n: {}, r: {}, p: {}, iterations: {}, len: {})",
"Scrypt".yellow(),
opts.log_n.to_string().cyan(),
Expand All @@ -187,9 +171,7 @@ fn main() {
opts.len.to_string().cyan(),
);

println!();

info!(
println!(
"Key for test vector \"{}\" is: {}",
TEST_VECTORS[i].secret.to_string().cyan(),
hex::encode(key as &[u8]).cyan()
Expand Down

0 comments on commit 4e98954

Please sign in to comment.