pub enum Algorithm {
SHA1,
SHA256,
SHA512,
}
Expand description
Enumeration of supported cryptographic hash algorithms for use with HMAC.
This enum allows users to choose between SHA-1, SHA-256, and SHA-512 as required by OTP generation specifications.
The default value is SHA1
, which is the original algorithm used in HOTP.
Variants§
Implementations§
Source§impl Algorithm
impl Algorithm
Sourcepub fn hash_hex(&self, data: &[u8]) -> String
pub fn hash_hex(&self, data: &[u8]) -> String
Hashes binary input data using the selected algorithm, returning the result as a hex string.
§Arguments
data
- The input string to hash
§Returns
A hexadecimal representation of the hash output.
§Example
use otp::Algorithm;
let sha1_hash = Algorithm::SHA1.hash_hex(b"");
assert_eq!(sha1_hash.len(), 40); // 160-bit = 20 bytes = 40 hex chars
let sha256_hash = Algorithm::SHA256.hash_hex(b"");
assert_eq!(sha256_hash.len(), 64); // 256-bit = 32 bytes = 64 hex chars
let sha512_hash = Algorithm::SHA512.hash_hex(b"");
assert_eq!(sha512_hash.len(), 128); // 512-bit = 64 bytes = 128 hex chars
Sourcepub fn hash_bytes(&self, data: &[u8]) -> Vec<u8> ⓘ
pub fn hash_bytes(&self, data: &[u8]) -> Vec<u8> ⓘ
Hashes binary input data using the selected algorithm, returning raw bytes.
§Arguments
data
- A byte slice of input data to hash
§Returns
A Vec<u8>
containing the hash output.
§Example
use otp::Algorithm;
let sha1_hash = Algorithm::SHA1.hash_bytes(b"");
assert_eq!(sha1_hash.len(), 20); // 160-bit = 20 bytes
let sha256_hash = Algorithm::SHA256.hash_bytes(b"");
assert_eq!(sha256_hash.len(), 32); // 256-bit = 32 bytes
let sha512_hash = Algorithm::SHA512.hash_bytes(b"");
assert_eq!(sha512_hash.len(), 64); // 512-bit = 64 bytes
Trait Implementations§
impl Copy for Algorithm
impl Eq for Algorithm
impl StructuralPartialEq for Algorithm
Auto Trait Implementations§
impl Freeze for Algorithm
impl RefUnwindSafe for Algorithm
impl Send for Algorithm
impl Sync for Algorithm
impl Unpin for Algorithm
impl UnwindSafe for Algorithm
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more