Skip to content

Commit

Permalink
Changes Time.now to Time.utc which is more correct
Browse files Browse the repository at this point in the history
  • Loading branch information
philnash committed Aug 18, 2019
1 parent 36fbda7 commit 79651eb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions spec/crotp/totp_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe CrOTP::TOTP do
totp = CrOTP::TOTP.new(secret)

it "verifies the current time" do
totp.verify(totp.generate, at: Time.now).should be_true
totp.verify(totp.generate, at: Time.utc).should be_true
end

results[:sha1].each do |(time, result)|
Expand All @@ -109,7 +109,7 @@ describe CrOTP::TOTP do
totp = CrOTP::TOTP.new(secret, algorithm: OpenSSL::Algorithm::SHA256)

it "verifies the current time" do
totp.verify(totp.generate, at: Time.now).should be_true
totp.verify(totp.generate, at: Time.utc).should be_true
end

results[:sha256].each do |(time, result)|
Expand All @@ -131,7 +131,7 @@ describe CrOTP::TOTP do
totp = CrOTP::TOTP.new(secret, algorithm: OpenSSL::Algorithm::SHA512)

it "verifies the current time" do
totp.verify(totp.generate, at: Time.now).should be_true
totp.verify(totp.generate, at: Time.utc).should be_true
end

results[:sha512].each do |(time, result)|
Expand Down
4 changes: 2 additions & 2 deletions src/crotp/totp.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module CrOTP
end
end

def generate(at : Int = Time.now.to_unix) : String
def generate(at : Int = Time.utc.to_unix) : String
counter = at // PERIOD
generate_otp(counter)
end
Expand All @@ -21,7 +21,7 @@ module CrOTP
generate(at.to_unix)
end

def verify(token : String, at : Int = Time.now.to_unix, allowed_drift : Int = 0) : Bool
def verify(token : String, at : Int = Time.utc.to_unix, allowed_drift : Int = 0) : Bool
counter = at // PERIOD
Array.new(allowed_drift + 1) { |i| verify_otp(token, counter - i) }.any?
end
Expand Down

0 comments on commit 79651eb

Please sign in to comment.