Skip to content

Commit

Permalink
adding a totp::generate_code(secret: String) method for easy librar…
Browse files Browse the repository at this point in the history
…y SHA1 code generation
  • Loading branch information
jakeswenson committed Jul 25, 2020
1 parent 3274cf3 commit 54fb4d5
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/totp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ pub fn standard_totp(config: Config, name: &str) -> TotpResult<String> {
.totp
.get(name)
.ok_or(TotpError("Can't find the specified config"))?;
let secret = secrets::get_secret(name, &totp_settings)?;

generate_code(secret)
}

pub fn generate_code(secret: String) -> TotpResult<String> {
let now = SystemTime::now();
let seconds: Duration = now
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Can't get time since UNIX_EPOCH?");

let secret = base32::decode(ALPHABET, &secrets::get_secret(name, &totp_settings)?)
let secret = base32::decode(ALPHABET, &secret)
.ok_or(TotpError("Failed to decode secret from base32"))?;

totp(&secret, seconds, Duration::from_secs(30), 6, Sha1::new())
Expand Down

0 comments on commit 54fb4d5

Please sign in to comment.