summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Jennings <Stephen.G.Jennings@gmail.com>2011-10-16 22:27:02 -0700
committerStephen Jennings <Stephen.G.Jennings@gmail.com>2011-10-16 23:27:57 -0700
commit5de1c1dae34a3f110ec4c076b583d160dacb6b71 (patch)
treeab2f9c55ba8a01eed2feef14782cc458b643dff9
parent0b1bdf68974336a6e74e5c7d953feae5b845e2ff (diff)
downloadOATH.Net-5de1c1dae34a3f110ec4c076b583d160dacb6b71.zip
OATH.Net-5de1c1dae34a3f110ec4c076b583d160dacb6b71.tar.gz
OATH.Net-5de1c1dae34a3f110ec4c076b583d160dacb6b71.tar.bz2
Refactor TimeBasedOtp to be a wrapper around CounterBasedOtp.
-rw-r--r--OATH.Net/TimeBasedOtp.cs42
1 files changed, 4 insertions, 38 deletions
diff --git a/OATH.Net/TimeBasedOtp.cs b/OATH.Net/TimeBasedOtp.cs
index 3fea09c..61e41c4 100644
--- a/OATH.Net/TimeBasedOtp.cs
+++ b/OATH.Net/TimeBasedOtp.cs
@@ -14,33 +14,16 @@ namespace OathNet
public class TimeBasedOtp
{
- private static int[] digits = new int[]
- {
- 1, // 0
- 10, // 1
- 100, // 2
- 1000, // 3
- 10000, // 4
- 100000, // 5
- 1000000, // 6
- 10000000, // 7
- 100000000 // 8
- };
-
- private byte[] secretKey;
-
- private int otpLength;
+ private CounterBasedOtp counterOtp;
public TimeBasedOtp(byte[] secretKey, int otpLength)
{
- this.secretKey = secretKey;
- this.otpLength = otpLength;
+ this.counterOtp = new CounterBasedOtp(secretKey, otpLength);
}
public TimeBasedOtp(string secretKeyHex, int otpLength)
{
- this.secretKey = secretKeyHex.HexStringToByteArray();
- this.otpLength = otpLength;
+ this.counterOtp = new CounterBasedOtp(secretKeyHex, otpLength);
}
public string ComputeOtp(DateTime time)
@@ -48,25 +31,8 @@ namespace OathNet
var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var span = time.ToUniversalTime() - unixEpoch;
var steps = (int)(span.TotalSeconds / 30);
- var stepString = steps.ToString("X16");
-
- var counter = stepString.HexStringToByteArray();
-
- var hmac = new HMACSHA1(this.secretKey);
- var hash = hmac.ComputeHash(counter);
-
- int offset = hash[hash.Length - 1] & 0xF;
-
- int binary = ((hash[offset] & 0x7F) << 24) |
- ((hash[offset + 1] & 0xFF) << 16) |
- ((hash[offset + 2] & 0xFF) << 8) |
- (hash[offset + 3] & 0xFF);
-
- var otp = binary % TimeBasedOtp.digits[this.otpLength];
-
- var result = otp.ToString("D" + this.otpLength.ToString());
- return result;
+ return this.counterOtp.ComputeOtp(steps);
}
}
}