Skip to content

Commit

Permalink
add base32 read and write
Browse files Browse the repository at this point in the history
  • Loading branch information
KaneGreen committed Apr 22, 2021
1 parent e1a0244 commit 6ae209e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,16 @@ description = "library for generating TOTP codes (tokens) defined in RFC 6238"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[package.metadata.docs.rs]
features = [ "oathuri" ]

[features]
default = []
oathuri = [ "data-encoding", "url", "percent-encoding" ]

[dependencies]
ring = '0.16'
data-encoding = { version = "2.3", optional = true }
percent-encoding = { version = "2.1", optional = true }
url = { version = "2.2", optional = true }
zeroize = { version = "1.3", optional = true }
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
//! * Read or write QR codes.
pub mod high_level;
pub mod low_level;
#[cfg(feature = "oathuri")]
pub mod oath_uri;

pub use high_level::TotpGenerator;
pub use low_level::HashAlgorithm;
28 changes: 28 additions & 0 deletions src/oath_uri.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Read or write URIs start with `otpauth://totp/`.
use data_encoding::{DecodeError, BASE32_NOPAD};
use zeroize::Zeroize;

/// Read key bytes from a base32-encoded String.
pub fn key_from_base32(mut encoded: String) -> Result<Vec<u8>, DecodeError> {
let mut tmp = encoded.to_ascii_uppercase();
encoded.zeroize();
let output = BASE32_NOPAD.decode(tmp.as_bytes());
tmp.zeroize();
output
}

/// Write key bytes to a base32-encoded String in uppercase.
pub fn key_to_base32_uppercase<T: AsRef<[u8]> + Zeroize>(mut key: T) -> String {
let output = BASE32_NOPAD.encode(key.as_ref());
key.zeroize();
output
}

/// Write key bytes to a base32-encoded String in lowercase.
pub fn key_to_base32_lowercase<T: AsRef<[u8]> + Zeroize>(key: T) -> String {
let mut uppercase = key_to_base32_uppercase(key);
let output = uppercase.to_ascii_lowercase();
uppercase.zeroize();
output
}

0 comments on commit 6ae209e

Please sign in to comment.