Skip to content

Commit

Permalink
Expose argon2 in wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Mar 25, 2024
1 parent 67e743f commit 99d5178
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions crates/bitwarden-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ keywords.workspace = true
crate-type = ["cdylib"]

[dependencies]
argon2 = { version = ">=0.5.0, <0.6", features = [
"alloc",
"zeroize",
], default-features = false }
base64 = ">=0.21.2, <0.22"
bitwarden-json = { path = "../bitwarden-json", features = [
"secrets",
"internal",
] }
console_error_panic_hook = "0.1.7"
console_log = { version = "1.0.0", features = ["color"] }
js-sys = "0.3.68"
Expand All @@ -23,10 +32,5 @@ serde = { version = "1.0.196", features = ["derive"] }
wasm-bindgen = { version = "0.2.91", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4.41"

bitwarden-json = { path = "../bitwarden-json", features = [
"secrets",
"internal",
] }

[dev-dependencies]
wasm-bindgen-test = "0.3.41"
28 changes: 28 additions & 0 deletions crates/bitwarden-wasm/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extern crate console_error_panic_hook;
use std::rc::Rc;

use argon2::{Algorithm, Argon2, Params, Version};
use base64::{engine::general_purpose::STANDARD, Engine};
use bitwarden_json::client::Client as JsonClient;
use js_sys::Promise;
use log::Level;
Expand Down Expand Up @@ -54,3 +56,29 @@ impl BitwardenClient {
})
}
}

#[wasm_bindgen]
pub fn argon2(
password: &[u8],
salt: &[u8],
iterations: u32,
memory: u32,
parallelism: u32,
) -> String {
let argon = Argon2::new(
Algorithm::Argon2id,
Version::V0x13,
Params::new(
memory * 1024, // Convert MiB to KiB
iterations,
parallelism,
Some(32),
)
.unwrap(),
);

let mut hash = [0u8; 32];
argon.hash_password_into(password, salt, &mut hash).unwrap();

STANDARD.encode(hash)
}

Check warning on line 84 in crates/bitwarden-wasm/src/client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-wasm/src/client.rs#L60-L84

Added lines #L60 - L84 were not covered by tests

0 comments on commit 99d5178

Please sign in to comment.