Skip to content

Commit 0c75d6f

Browse files
committed
fix readme test
1 parent edd061c commit 0c75d6f

2 files changed

Lines changed: 79 additions & 19 deletions

File tree

README.md

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,25 +116,39 @@ cargo run --bin uniffi-bindgen generate --library target/debug/libmfkdf2.dylib -
116116

117117
```rust
118118
# use std::collections::HashMap;
119-
use mfkdf2::{derive, setup};
120-
use mfkdf2::setup::{factors::hotp::HOTPOptions, password::PasswordOptions, key::MFKDF2Options};
121-
use mfkdf2::derive::factors::{hotp::HOTPOptions, password::PasswordOptions};
122-
123-
// 1. Define factors
124-
let password_factor = setup::password("my-super-secret-password", PasswordOptions::default())?;
125-
let totp_factor = setup::hotp("base32-encoded-secret", HOTPOptions::default())?;
126-
127-
// 2. Set up the key with the policy
128-
let key = setup::key(vec![password_factor, totp_factor], MFKDF2Options::default())?;
129-
130-
println!("Key: {:?}", key);
131-
132-
let factors = HashMap::from([
133-
("password".to_string(), derive::factors::password("my-super-secret-password")?),
134-
"hotp".to_string(), derive::factors::hotp("123456")?),
135-
]);
136-
// 3. Derive the key using user inputs
137-
let derived_key = derive::key(&key.policy, factors, true, false)?;
119+
use mfkdf2::setup::factors::{totp::TOTPOptions, password::PasswordOptions};
120+
use mfkdf2::definitions::MFKDF2Options;
121+
122+
let setup = mfkdf2::setup::key(
123+
&[
124+
mfkdf2::setup::factors::password("password1", PasswordOptions::default())?,
125+
mfkdf2::setup::factors::totp(TOTPOptions {
126+
secret: Some(b"abcdefghijklmnopqrst".to_vec()),
127+
time: Some(1),
128+
..Default::default()
129+
})?,
130+
],
131+
MFKDF2Options::default(),
132+
)?;
133+
134+
let derived_key = mfkdf2::derive::key(
135+
&setup.policy,
136+
HashMap::from([
137+
("password".to_string(), mfkdf2::derive::factors::password("password1")?),
138+
(
139+
"totp".to_string(),
140+
mfkdf2::derive::factors::totp(
141+
241063,
142+
Some(mfkdf2::derive::factors::totp::TOTPDeriveOptions {
143+
time: Some(30001),
144+
..Default::default()
145+
}),
146+
)?,
147+
),
148+
]),
149+
true,
150+
false,
151+
)?;
138152
139153
println!("Derived Key: {:?}", derived_key);
140154

mfkdf2/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ After you have setup a key policy, you can derive the key from the policy and th
142142

143143
## Examples
144144

145+
### Password + HOTP + HMACSHA1
146+
145147
Derive a composite key with password, hmacsha1 and hotp factors. Derive returns the
146148
[MFKDF2DerivedKey](`crate::definitions::MFKDF2DerivedKey`) and updated [Policy](`crate::policy::Policy`).
147149

@@ -222,6 +224,50 @@ let derived_key = derive::key(
222224
# Ok::<(), mfkdf2::error::MFKDF2Error>(())
223225
```
224226

227+
### Password + TOTP
228+
229+
```rust
230+
# use std::collections::HashMap;
231+
use mfkdf2::setup::factors::{totp::TOTPOptions, password::PasswordOptions};
232+
use mfkdf2::definitions::MFKDF2Options;
233+
234+
let setup = mfkdf2::setup::key(
235+
&[
236+
mfkdf2::setup::factors::password("password1", PasswordOptions::default())?,
237+
mfkdf2::setup::factors::totp(TOTPOptions {
238+
secret: Some(b"abcdefghijklmnopqrst".to_vec()),
239+
time: Some(1),
240+
..Default::default()
241+
})?,
242+
],
243+
MFKDF2Options::default(),
244+
)?;
245+
246+
let derived_key = mfkdf2::derive::key(
247+
&setup.policy,
248+
HashMap::from([
249+
("password".to_string(), mfkdf2::derive::factors::password("password1")?),
250+
(
251+
"totp".to_string(),
252+
mfkdf2::derive::factors::totp(
253+
241063,
254+
Some(mfkdf2::derive::factors::totp::TOTPDeriveOptions {
255+
time: Some(30001),
256+
..Default::default()
257+
}),
258+
)?,
259+
),
260+
]),
261+
true,
262+
false,
263+
)?;
264+
265+
println!("Derived Key: {:?}", derived_key);
266+
267+
# assert_eq!(setup.key, derived_key.key);
268+
# Ok::<(), mfkdf2::error::MFKDF2Error>(())
269+
```
270+
225271
# Threshold Recovery
226272

227273
Threshold recovery generalizes a multi‑factor policy from “all factors required” to a

0 commit comments

Comments
 (0)