Skip to main content

Algorithm

Enum Algorithm 

Source
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§

§

SHA1

§

SHA256

§

SHA512

Implementations§

Source§

impl Algorithm

Source

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
Source

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§

Source§

impl Clone for Algorithm

Source§

fn clone(&self) -> Algorithm

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Algorithm

Source§

impl Default for Algorithm

Source§

fn default() -> Algorithm

Returns the “default value” for a type. Read more
Source§

impl Display for Algorithm

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Algorithm

Source§

impl PartialEq for Algorithm

Source§

fn eq(&self, other: &Algorithm) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Algorithm

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.