Skip to content

Commit

Permalink
docs: reorder the README examples (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
louib authored Feb 24, 2024
1 parent 68ef922 commit 2744ff6
Showing 1 changed file with 59 additions and 53 deletions.
112 changes: 59 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,62 @@ Add this to your Cargo.toml
challenge_response = "0"
```

### Perform a Challenge-Response (HMAC-SHA1 mode)

If you are using a YubiKey, you can configure the HMAC-SHA1 Challenge-Response
with the [Yubikey Personalization GUI](https://developers.yubico.com/yubikey-personalization-gui/).

```rust
extern crate challenge_response;
extern crate hex;

use challenge_response::config::{Config, Mode, Slot};
use challenge_response::ChallengeResponse;
use std::ops::Deref;

fn main() {
let mut cr_client = match ChallengeResponse::new() {
Ok(c) => c,
Err(e) => {
eprintln!("{}", e.to_string());
return;
}
};

let device = match cr_client.find_device() {
Ok(d) => d,
Err(e) => {
eprintln!("Device not found: {}", e.to_string());
return;
}
};

println!(
"Vendor ID: {:?} Product ID {:?}",
device.vendor_id, device.product_id
);

let config = Config::new_from(device)
.set_variable_size(true)
.set_mode(Mode::Sha1)
.set_slot(Slot::Slot2);

// Challenge can not be greater than 64 bytes
let challenge = String::from("mychallenge");
// In HMAC Mode, the result will always be the
// SAME for the SAME provided challenge
let hmac_result = cr_client
.challenge_response_hmac(challenge.as_bytes(), config)
.unwrap();

// Just for debug, lets check the hex
let v: &[u8] = hmac_result.deref();
let hex_string = hex::encode(v);

println!("{}", hex_string);
}
```

### Configure Yubikey (HMAC-SHA1 mode)

Note, please read about the [initial configuration](https://wiki.archlinux.org/index.php/yubikey#Initial_configuration)
Expand All @@ -55,15 +111,15 @@ use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};

fn main() {
let mut challenge_response = match ChallengeResponse::new() {
let mut cr_client = match ChallengeResponse::new() {
Ok(y) => y,
Err(e) => {
eprintln!("{}", e.to_string());
return;
}
};

if let Ok(device) = challenge_response.find_device() {
if let Ok(device) = cr_client.find_device() {
println!(
"Vendor ID: {:?} Product ID {:?}",
device.vendor_id, device.product_id
Expand All @@ -81,7 +137,7 @@ fn main() {
let mut device_config = DeviceModeConfig::default();
device_config.challenge_response_hmac(&hmac_key, false, false);

if let Err(err) = challenge_response.write_config(config, &mut device_config) {
if let Err(err) = cr_client.write_config(config, &mut device_config) {
println!("{:?}", err);
} else {
println!("Device configured");
Expand All @@ -92,56 +148,6 @@ fn main() {
}
```

### Example Challenge-Response (HMAC-SHA1 mode)

Configure the yubikey with [Yubikey Personalization GUI](https://developers.yubico.com/yubikey-personalization-gui/)

```rust
extern crate challenge_response;
extern crate hex;

use challenge_response::config::{Config, Mode, Slot};
use challenge_response::ChallengeResponse;
use std::ops::Deref;

fn main() {
let mut challenge_response = match ChallengeResponse::new() {
Ok(y) => y,
Err(e) => {
eprintln!("{}", e.to_string());
return;
}
};

if let Ok(device) = challenge_response.find_device() {
println!(
"Vendor ID: {:?} Product ID {:?}",
device.vendor_id, device.product_id
);

let config = Config::new_from(device)
.set_variable_size(true)
.set_mode(Mode::Sha1)
.set_slot(Slot::Slot2);

// Challenge can not be greater than 64 bytes
let challenge = String::from("mychallenge");
// In HMAC Mode, the result will always be the SAME for the SAME provided challenge
let hmac_result = challenge_response
.challenge_response_hmac(challenge.as_bytes(), config)
.unwrap();

// Just for debug, lets check the hex
let v: &[u8] = hmac_result.deref();
let hex_string = hex::encode(v);

println!("{}", hex_string);
} else {
println!("Device not found");
}
}
```

## Credits

This library was originally a fork of the [yubico_manager](https://crates.io/crates/yubico_manager) library.

0 comments on commit 2744ff6

Please sign in to comment.