Fixing issues reported in google code

This commit is contained in:
Nathan Adams 2014-04-20 23:23:18 -05:00
parent 15a298afad
commit 5d1e6a1301
2 changed files with 11 additions and 7 deletions

View File

@ -5,20 +5,22 @@ using System.Text;
namespace OTPNet namespace OTPNet
{ {
// Issue reported by larry.ellis
// Patch provided by andrea.bertin
// https://code.google.com/p/otpnet/issues/detail?id=1
class Unixtime class Unixtime
{ {
private DateTime date; private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private readonly DateTime date;
public Unixtime() public Unixtime()
{ {
this.date = DateTime.Now; this.date = DateTime.UtcNow;
} }
public Unixtime(int timestamp) public Unixtime(int timestamp)
{ {
DateTime converted = new DateTime(1970, 1, 1, 0, 0, 0, 0); this.date = Epoch.AddSeconds(timestamp);
DateTime newDateTime = converted.AddSeconds(timestamp);
this.date = newDateTime.ToLocalTime();
} }
public DateTime toDateTime() public DateTime toDateTime()
@ -28,8 +30,7 @@ namespace OTPNet
public long toTimeStamp() public long toTimeStamp()
{ {
TimeSpan span = (this.date - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).ToLocalTime()); return (long)this.date.Subtract(Epoch).TotalSeconds;
return Convert.ToInt64(span.TotalSeconds);
} }
} }
} }

View File

@ -67,6 +67,9 @@ namespace OTPNet
public OTP(string secret, int digits) public OTP(string secret, int digits)
{ {
// Issue reported by scox.nz
// https://code.google.com/p/otpnet/issues/detail?id=2
this.secret = secret;
this.digits = digits; this.digits = digits;
this.digest = HashAlgorithm.SHA1; this.digest = HashAlgorithm.SHA1;
} }