pub trait Key {
// Required methods
fn as_any(&self) -> &dyn Any;
fn get_code(&mut self) -> Result<String, Error>;
fn get_name(&self) -> &str;
fn get_recovery_codes(&self) -> Vec<String>;
fn get_type(&self) -> KeyType;
fn set_name(&mut self, name: &str);
fn set_recovery_codes(&mut self, recovery_codes: Vec<String>);
}Expand description
Key is the interface for the keys
usage:
use libr2fa::HOTPKey;
use libr2fa::HMACType;
use libr2fa::Key;
let mut hotp_key = HOTPKey {
key: "MFSWS5LGNBUXKZLBO5TGQ33JO5SWC2DGNF2WCZLIMZUXKZLXMFUGM2LVNFQWK53IMZUXK2A=".to_string(),
hmac_type: HMACType::SHA1,
..Default::default()
};
let code = hotp_key.get_code().unwrap();Required Methods§
Sourcefn as_any(&self) -> &dyn Any
fn as_any(&self) -> &dyn Any
use to downcast to original type
use libr2fa::HOTPKey;
use libr2fa::HMACType;
use libr2fa::Key;
let hotp_key = HOTPKey {
key: "MFSWS5LGNBUXKZLBO5TGQ33JO5SWC2DGNF2WCZLIMZUXKZLXMFUGM2LVNFQWK53IMZUXK2A=".to_string(),
hmac_type: HMACType::SHA1,
..Default::default()
};
let key: Box<dyn Key> = Box::new(hotp_key);
let hotp_key = key.as_any().downcast_ref::<HOTPKey>();
assert!(hotp_key.is_some());Sourcefn get_code(&mut self) -> Result<String, Error>
fn get_code(&mut self) -> Result<String, Error>
get_code returns the code for the key
if it is HTOP key, it will increment the counter
Sourcefn get_name(&self) -> &str
fn get_name(&self) -> &str
get the name of the key
use libr2fa::HOTPKey;
use libr2fa::HMACType;
use libr2fa::Key;
let mut hotp_key = HOTPKey {
key: "MFSWS5LGNBUXKZLBO5TGQ33JO5SWC2DGNF2WCZLIMZUXKZLXMFUGM2LVNFQWK53IMZUXK2A=".to_string(),
hmac_type: HMACType::SHA1,
..Default::default()
};
hotp_key.set_name("test");
assert_eq!(hotp_key.get_name(), "test")Sourcefn get_recovery_codes(&self) -> Vec<String>
fn get_recovery_codes(&self) -> Vec<String>
get the recovery codes
use libr2fa::HOTPKey;
use libr2fa::HMACType;
use libr2fa::Key;
let mut hotp_key = HOTPKey {
key: "MFSWS5LGNBUXKZLBO5TGQ33JO5SWC2DGNF2WCZLIMZUXKZLXMFUGM2LVNFQWK53IMZUXK2A=".to_string(),
hmac_type: HMACType::SHA1,
..Default::default()
};
hotp_key.set_recovery_codes(vec!["test".to_string()]);
assert_eq!(hotp_key.get_recovery_codes(), &["test".to_string()])
Sourcefn set_name(&mut self, name: &str)
fn set_name(&mut self, name: &str)
set the name of the key
use libr2fa::HOTPKey;
use libr2fa::HMACType;
use libr2fa::Key;
let mut hotp_key = HOTPKey {
name: "".to_string(),
key: "MFSWS5LGNBUXKZLBO5TGQ33JO5SWC2DGNF2WCZLIMZUXKZLXMFUGM2LVNFQWK53IMZUXK2A=".to_string(),
hmac_type: HMACType::SHA1,
..Default::default()
};
hotp_key.set_name("test");
assert_eq!(hotp_key.get_name(), "test")Sourcefn set_recovery_codes(&mut self, recovery_codes: Vec<String>)
fn set_recovery_codes(&mut self, recovery_codes: Vec<String>)
set the recovery codes
use libr2fa::HOTPKey;
use libr2fa::HMACType;
use libr2fa::Key;
let mut hotp_key = HOTPKey {
key: "MFSWS5LGNBUXKZLBO5TGQ33JO5SWC2DGNF2WCZLIMZUXKZLXMFUGM2LVNFQWK53IMZUXK2A=".to_string(),
hmac_type: HMACType::SHA1,
..Default::default()
};
hotp_key.set_recovery_codes(vec!["test".to_string()]);
assert_eq!(hotp_key.get_recovery_codes(), &["test".to_string()])