Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
Cargo.lock
target/

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "libotp"
version = "0.1.4"
edition = "2021"
authors = ["Naim A. <naim94a@gmail.com>"]
description = "One Time Password generator"
license = "GPL-3.0"
Expand All @@ -10,7 +11,8 @@ keywords = ["RFC4226", "RFC6238", "OTP", "Authentication"]
categories = ["authentication"]

[dependencies]
ring = "^0.14"
#ring = "^0.14"
ring = "0.16.7"
binascii = "^0.1"

[lib]
Expand Down
18 changes: 8 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,24 @@ pub enum HOTPAlgorithm {
impl HOTPAlgorithm {
pub fn from_buffer_len(buffer_length: usize) -> Option<HOTPAlgorithm> {
match buffer_length {
ring::digest::SHA1_OUTPUT_LEN => {
Option::Some(HOTPAlgorithm::HMACSHA1)
},
ring::digest::SHA256_OUTPUT_LEN => {
Option::Some(HOTPAlgorithm::HMACSHA256)
},
ring::digest::SHA512_OUTPUT_LEN => {
Option::Some(HOTPAlgorithm::HMACSHA512)
},
_ => {
Option::None
Option::Some(HOTPAlgorithm::HMACSHA1)
},
}
}

pub fn get_algorithm<'a>(&self) -> &'a ring::digest::Algorithm {
pub fn get_algorithm(&self) -> ring::hmac::Algorithm {

match *self {
HOTPAlgorithm::HMACSHA1 => &ring::digest::SHA1,
HOTPAlgorithm::HMACSHA256 => &ring::digest::SHA256,
HOTPAlgorithm::HMACSHA512 => &ring::digest::SHA512,
HOTPAlgorithm::HMACSHA1 => ring::hmac::HMAC_SHA1_FOR_LEGACY_USE_ONLY,
HOTPAlgorithm::HMACSHA256 => ring::hmac::HMAC_SHA256,
HOTPAlgorithm::HMACSHA512 => ring::hmac::HMAC_SHA512,
}
}
}
Expand All @@ -88,7 +86,7 @@ impl HOTP {
pub fn new(algorithm: HOTPAlgorithm) -> Result<HOTP, ring::error::Unspecified> {
let algo = algorithm.get_algorithm();

match HOTP::generate_secret(algo.output_len) {
match HOTP::generate_secret(algo.digest_algorithm().output_len) {
Ok(secret) => {
Result::Ok(HOTP {
secret,
Expand Down Expand Up @@ -183,7 +181,7 @@ impl HOTP {
pub fn get_otp(&self, counter: &[u8], digits: u32) -> u32 {
let algorithm = self.algorithm.get_algorithm();

let signer = ring::hmac::SigningKey::new(algorithm, self.secret.as_slice());
let signer = ring::hmac::Key::new(algorithm, self.secret.as_slice());
let hmac = ring::hmac::sign(&signer, counter);
let block = hmac.as_ref();
let num = HOTP::get_hotp_value(block);
Expand Down