25 lines
636 B
Rust
25 lines
636 B
Rust
use totp_rs::{Algorithm, TotpUrlError, TOTP};
|
|
|
|
pub use totp_rs::Secret;
|
|
|
|
// error
|
|
#[derive(thiserror::Error)]
|
|
// the rest
|
|
#[derive(Debug)]
|
|
pub enum Error {
|
|
#[error("Totp url error: {0}")]
|
|
Url(#[from] TotpUrlError),
|
|
|
|
#[error("Totp secret error: {0}")]
|
|
Secret(String),
|
|
}
|
|
|
|
pub fn new(secret: Vec<u8>, issuer: Option<String>, account_name: String) -> Result<TOTP, Error> {
|
|
TOTP::new(Algorithm::SHA1, 6, 1, 30, secret, issuer, account_name).map_err(Error::Url)
|
|
}
|
|
|
|
pub fn secret_to_bytes(secret: &Secret) -> Result<Vec<u8>, Error> {
|
|
secret
|
|
.to_bytes()
|
|
.map_err(|e| Error::Secret(format!("{:?}", e)))
|
|
}
|