chore: Create base32_decode function to decode base32

This commit is contained in:
Awiteb 2024-05-19 04:19:49 +03:00
parent 8a1768631d
commit 00694e6f86
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F

View file

@ -50,8 +50,7 @@ pub fn totp_now(secret_base32: &str, hash_function: &TotpHash) -> LprsResult<(St
.expect("SystemTime before UNIX EPOCH!") .expect("SystemTime before UNIX EPOCH!")
.as_secs(); .as_secs();
let remaining = 30 - (now % 30) as u8; let remaining = 30 - (now % 30) as u8;
let secret = base32::decode(Base32Alphabet::RFC4648 { padding: true }, secret_base32) let secret = base32_decode(secret_base32)?;
.ok_or_else(|| LprsError::Base32("Can't decode the TOTP secret".to_owned()))?;
Ok(match hash_function { Ok(match hash_function {
TotpHash::Sha1 => { TotpHash::Sha1 => {
( (
@ -74,6 +73,15 @@ pub fn totp_now(secret_base32: &str, hash_function: &TotpHash) -> LprsResult<(St
}) })
} }
/// Base32 decode
///
/// ## Errors
/// - If the given string not valid base32 string
pub fn base32_decode(base32_string: &str) -> LprsResult<Vec<u8>> {
base32::decode(Base32Alphabet::RFC4648 { padding: true }, base32_string)
.ok_or_else(|| LprsError::Base32("Invalid base32 string".to_owned()))
}
/// Encrypt the given data by the given key using AES-256 CBC /// Encrypt the given data by the given key using AES-256 CBC
/// ///
/// Note: The IV will be add it to the end of the ciphertext (Last 16 bytes) /// Note: The IV will be add it to the end of the ciphertext (Last 16 bytes)