diff options
-rw-r--r-- | OATH.Net.Test/Settings.StyleCop | 14 | ||||
-rw-r--r-- | OATH.Net/CounterBasedOtp.cs | 30 | ||||
-rw-r--r-- | OATH.Net/MiscExtensions.cs | 8 | ||||
-rw-r--r-- | OATH.Net/TimeBasedOtp.cs | 38 | ||||
-rw-r--r-- | Settings.StyleCop | 8 |
5 files changed, 91 insertions, 7 deletions
diff --git a/OATH.Net.Test/Settings.StyleCop b/OATH.Net.Test/Settings.StyleCop new file mode 100644 index 0000000..c442f66 --- /dev/null +++ b/OATH.Net.Test/Settings.StyleCop @@ -0,0 +1,14 @@ +<StyleCopSettings Version="4.3"> + <Analyzers> + <Analyzer AnalyzerId="Microsoft.StyleCop.CSharp.DocumentationRules"> + <Rules> + <Rule Name="ElementsMustBeDocumented"> + <RuleSettings> + <BooleanProperty Name="Enabled">False</BooleanProperty> + </RuleSettings> + </Rule> + </Rules> + <AnalyzerSettings/> + </Analyzer> + </Analyzers> +</StyleCopSettings>
\ No newline at end of file diff --git a/OATH.Net/CounterBasedOtp.cs b/OATH.Net/CounterBasedOtp.cs index 217d86f..cca7b89 100644 --- a/OATH.Net/CounterBasedOtp.cs +++ b/OATH.Net/CounterBasedOtp.cs @@ -12,6 +12,17 @@ namespace OathNet using System.Security.Cryptography; using System.Text; + /// <summary> + /// Implements the OATH HOTP algorithm. + /// </summary> + /// <example> + /// <code> + /// CounterBasedOtp otp = new CounterBasedOtp("01234567", 6); + /// int counter = 5555; + /// string expectedCode = otp.ComputeOtp(counter); + /// bool validCode = userSuppliedCode == expectedCode; + /// </code> + /// </example> public class CounterBasedOtp { private static int[] digits = new int[] @@ -31,18 +42,37 @@ namespace OathNet private int otpLength; + /// <summary> + /// Initializes a new instance of the CounterBasedOtp class. This is used + /// when the client and server share a counter value. + /// </summary> + /// <param name="secretKey">The secret key.</param> + /// <param name="otpLength">The number of digits in the OTP to generate.</param> public CounterBasedOtp(byte[] secretKey, int otpLength) { this.secretKey = secretKey; this.otpLength = otpLength; } + /// <summary> + /// Initializes a new instance of the CounterBasedOtp class. This is used + /// when the client and server share a counter value. + /// </summary> + /// <param name="secretKeyHex">The secret key represented as a sequence of hexadecimal digits.</param> + /// <param name="otpLength">The number of digits in the OTP to generate.</param> public CounterBasedOtp(string secretKeyHex, int otpLength) { this.secretKey = secretKeyHex.HexStringToByteArray(); this.otpLength = otpLength; } + /// <summary> + /// Computes the OTP for the given counter value. The client and server + /// compute this independently and come up with the same result, provided + /// they use the same shared key. + /// </summary> + /// <param name="counter">The counter value to use.</param> + /// <returns>The OTP for the given counter value.</returns> public virtual string ComputeOtp(int counter) { var text = new byte[8]; diff --git a/OATH.Net/MiscExtensions.cs b/OATH.Net/MiscExtensions.cs index ad93f49..843cea5 100644 --- a/OATH.Net/MiscExtensions.cs +++ b/OATH.Net/MiscExtensions.cs @@ -10,8 +10,16 @@ namespace OathNet using System.Globalization; using System.Linq; + /// <summary> + /// A collection of miscellaneous extension methods. + /// </summary> public static class MiscExtensions { + /// <summary> + /// Converts a hexadecimal string to a byte array. + /// </summary> + /// <param name="s">The hexadecimal string.</param> + /// <returns>A byte array representing the given string.</returns> public static byte[] HexStringToByteArray(this string s) { return Enumerable.Range(0, s.Length - 1) diff --git a/OATH.Net/TimeBasedOtp.cs b/OATH.Net/TimeBasedOtp.cs index 61e41c4..ab0cd40 100644 --- a/OATH.Net/TimeBasedOtp.cs +++ b/OATH.Net/TimeBasedOtp.cs @@ -12,20 +12,58 @@ namespace OathNet using System.Security.Cryptography; using System.Text; + /// <summary> + /// Implements the OATH HOTP algorithm. + /// </summary> + /// <remarks> + /// OATH TOTP is a derevation of the HOTP algorithm, + /// where the counter is derived from the time since + /// the UNIX epoch. + /// </remarks> + /// <example> + /// <code> + /// TimeBasedOtp otp = new TimeBasedOtp("01234567", 6); + /// string expectedCode = otp.ComputeOtp(DateTime.UtcNow); + /// bool validCode = userSuppliedCode == expectedCode; + /// </code> + /// </example> public class TimeBasedOtp { private CounterBasedOtp counterOtp; + /// <summary> + /// Initializes a new instance of the TimeBasedOtp class. This + /// is used when the client and server do not share a counter + /// value but the clocks between the two are synchronized within + /// reasonable margins of each other. + /// </summary> + /// <param name="secretKey">The secret key.</param> + /// <param name="otpLength">The number of digits in the OTP to generate.</param> public TimeBasedOtp(byte[] secretKey, int otpLength) { this.counterOtp = new CounterBasedOtp(secretKey, otpLength); } + /// <summary> + /// Initializes a new instance of the TimeBasedOtp class. This + /// is used when the client and server do not share a counter + /// value but the clocks between the two are synchronized within + /// reasonable margins of each other. + /// </summary> + /// <param name="secretKeyHex">The secret key represented as a sequence of hexadecimal digits.</param> + /// <param name="otpLength">The number of digits in the OTP to generate.</param> public TimeBasedOtp(string secretKeyHex, int otpLength) { this.counterOtp = new CounterBasedOtp(secretKeyHex, otpLength); } + /// <summary> + /// Computes the OTP for the <paramref name="time"/> parameter. + /// The client and server compute this independently and come up + /// with the same result, provided they use the same shared key. + /// </summary> + /// <param name="time">The date and time for which to generate an OTP.</param> + /// <returns>The OTP for the given secret key and DateTime.</returns> public string ComputeOtp(DateTime time) { var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); diff --git a/Settings.StyleCop b/Settings.StyleCop index b56fa9c..3d2d39e 100644 --- a/Settings.StyleCop +++ b/Settings.StyleCop @@ -1,13 +1,7 @@ <StyleCopSettings Version="4.3"> <Analyzers> <Analyzer AnalyzerId="Microsoft.StyleCop.CSharp.DocumentationRules"> - <Rules> - <Rule Name="ElementsMustBeDocumented"> - <RuleSettings> - <BooleanProperty Name="Enabled">False</BooleanProperty> - </RuleSettings> - </Rule> - </Rules> + <Rules/> <AnalyzerSettings> <StringProperty Name="CompanyName">Stephen Jennings</StringProperty> <StringProperty Name="Copyright">Copyright 2011 Stephen Jennings. Licensed under the Apache License, Version 2.0.</StringProperty> |