Skip to content

Commit

Permalink
cleanup syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
kspearrin committed Feb 18, 2024
1 parent 4f22810 commit 48475df
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 190 deletions.
41 changes: 19 additions & 22 deletions src/Otp.NET/Hotp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ namespace OtpNet;
/// </remarks>
public class Hotp : Otp
{
private readonly int _hotpSize;

/// <summary>
/// Gets the number of diigts that the returning HOTP should have.
/// </summary>
public int HotpSize => _hotpSize;

/// <summary>
/// Create a HOTP instance
/// </summary>
Expand All @@ -53,7 +46,7 @@ public Hotp(byte[] secretKey, OtpHashMode mode = OtpHashMode.Sha1, int hotpSize
: base(secretKey, mode)
{
VerifyParameters(hotpSize);
_hotpSize = hotpSize;
HotpSize = hotpSize;
}

/// <summary>
Expand All @@ -66,21 +59,13 @@ public Hotp(IKeyProvider key, OtpHashMode mode = OtpHashMode.Sha1, int hotpSize
: base(key, mode)
{
VerifyParameters(hotpSize);

_hotpSize = hotpSize;
HotpSize = hotpSize;
}

private static void VerifyParameters(int hotpSize)
{
if (hotpSize < 6)
{
throw new ArgumentOutOfRangeException(nameof(hotpSize));
}
if (hotpSize > 8)
{
throw new ArgumentOutOfRangeException(nameof(hotpSize));
}
}
/// <summary>
/// Gets the number of digits that the returning HOTP should have.
/// </summary>
public int HotpSize { get; private set; }

/// <summary>
/// Takes a counter and then computes a HOTP value
Expand Down Expand Up @@ -108,6 +93,18 @@ protected override string Compute(long counter, OtpHashMode mode)
{
var data = KeyUtilities.GetBigEndianBytes(counter);
var otp = CalculateOtp(data, mode);
return Digits(otp, _hotpSize);
return Digits(otp, HotpSize);
}

private static void VerifyParameters(int hotpSize)
{
if (hotpSize < 6)
{
throw new ArgumentOutOfRangeException(nameof(hotpSize));
}
if (hotpSize > 8)
{
throw new ArgumentOutOfRangeException(nameof(hotpSize));
}
}
}
19 changes: 6 additions & 13 deletions src/Otp.NET/InMemoryKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace OtpNet;
/// </remarks>
public class InMemoryKey : IKeyProvider
{
private readonly object _stateSync = new object();
private readonly object _stateSync = new();
private readonly byte[] _keyData;
private readonly int _keyLength;

Expand Down Expand Up @@ -122,20 +122,13 @@ public byte[] ComputeHmac(OtpHashMode mode, byte[] data)
/// </summary>
private static HMAC CreateHmacHash(OtpHashMode otpHashMode)
{
HMAC hmacAlgorithm;
switch (otpHashMode)
HMAC hmacAlgorithm = otpHashMode switch
{
case OtpHashMode.Sha256:
hmacAlgorithm = new HMACSHA256();
break;
case OtpHashMode.Sha512:
hmacAlgorithm = new HMACSHA512();
break;
OtpHashMode.Sha256 => new HMACSHA256(),
OtpHashMode.Sha512 => new HMACSHA512(),
// case OtpHashMode.Sha1:
default:
hmacAlgorithm = new HMACSHA1();
break;
}
_ => new HMACSHA1(),
};
return hmacAlgorithm;
}
}
27 changes: 5 additions & 22 deletions src/Otp.NET/KeyGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,14 @@ public static byte[] DeriveKeyFromMaster(
OtpHashMode mode = OtpHashMode.Sha1) =>
DeriveKeyFromMaster(masterKey, KeyUtilities.GetBigEndianBytes(serialNumber), mode);

private static HashAlgorithm GetHashAlgorithmForMode(OtpHashMode mode)
{
switch (mode)
{
case OtpHashMode.Sha256:
return SHA256.Create();
case OtpHashMode.Sha512:
return SHA512.Create();
// case OtpHashMode.Sha1:
default:
return SHA1.Create();
}
}

private static int LengthForMode(OtpHashMode mode)
{
switch (mode)
return mode switch
{
case OtpHashMode.Sha256:
return 32;
case OtpHashMode.Sha512:
return 64;
OtpHashMode.Sha256 => 32,
OtpHashMode.Sha512 => 64,
// case OtpHashMode.Sha1:
default:
return 20;
}
_ => 20,
};
}
}
86 changes: 43 additions & 43 deletions src/Otp.NET/OtpUri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,6 @@ namespace OtpNet;
// See https://github.com/google/google-authenticator/wiki/Key-Uri-Format
public class OtpUri
{
/// <summary>
/// What type of OTP is this uri for
/// <seealso cref="OtpType"/>
/// </summary>
public readonly OtpType Type;

/// <summary>
/// The secret parameter is an arbitrary key value encoded in Base32 according to RFC 3548.
/// The padding specified in RFC 3548 section 2.2 is not required and should be omitted.
/// </summary>
public readonly string Secret;

/// <summary>
/// Which account a key is associated with
/// </summary>
public readonly string User;

/// <summary>
/// The issuer parameter is a string value indicating the provider or service this account is
/// associated with, URL-encoded according to RFC 3986.
/// </summary>
public readonly string Issuer;

/// <summary>
/// The algorithm used by the generator
/// </summary>
public readonly OtpHashMode Algorithm;

/// <summary>
/// The amount of digits in the final code
/// </summary>
public readonly int Digits;

/// <summary>
/// The number of seconds that a code is valid. Only applies to TOTP, not HOTP
/// </summary>
public readonly int Period;

/// <summary>
/// Initial counter value for HOTP. This is ignored when using TOTP.
/// </summary>
public readonly int Counter;

/// <summary>
/// Create a new OTP Auth Uri
/// </summary>
Expand Down Expand Up @@ -104,6 +61,49 @@ public OtpUri(
algorithm, digits, period, counter)
{ }

/// <summary>
/// What type of OTP is this uri for
/// <seealso cref="OtpType"/>
/// </summary>
public OtpType Type { get; private set; }

/// <summary>
/// The secret parameter is an arbitrary key value encoded in Base32 according to RFC 3548.
/// The padding specified in RFC 3548 section 2.2 is not required and should be omitted.
/// </summary>
public string Secret { get; private set; }

/// <summary>
/// Which account a key is associated with
/// </summary>
public string User { get; private set; }

/// <summary>
/// The issuer parameter is a string value indicating the provider or service this account is
/// associated with, URL-encoded according to RFC 3986.
/// </summary>
public string Issuer { get; private set; }

/// <summary>
/// The algorithm used by the generator
/// </summary>
public OtpHashMode Algorithm { get; private set; }

/// <summary>
/// The amount of digits in the final code
/// </summary>
public int Digits { get; private set; }

/// <summary>
/// The number of seconds that a code is valid. Only applies to TOTP, not HOTP
/// </summary>
public int Period { get; private set; }

/// <summary>
/// Initial counter value for HOTP. This is ignored when using TOTP.
/// </summary>
public int Counter { get; private set; }

/// <summary>
/// Generates a Uri according to the parameters
/// </summary>
Expand Down
14 changes: 7 additions & 7 deletions src/Otp.NET/TimeCorrection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class TimeCorrection
/// <summary>
/// An instance that provides no correction factor
/// </summary>
public static readonly TimeCorrection UncorrectedInstance = new TimeCorrection();
public static readonly TimeCorrection UncorrectedInstance = new();

/// <summary>
/// Constructor used solely for the UncorrectedInstance static field to provide an instance without
Expand Down Expand Up @@ -77,19 +77,19 @@ public TimeCorrection(DateTime correctTime, DateTime referenceTime) =>
CorrectionFactor = referenceTime - correctTime;

/// <summary>
/// Applies the correction factor to the reference time and returns a corrected time
/// The timespan that is used to calculate a corrected time
/// </summary>
/// <param name="referenceTime">The reference time</param>
/// <returns>The reference time with the correction factor applied</returns>
public DateTime GetCorrectedTime(DateTime referenceTime) => referenceTime - CorrectionFactor;
public TimeSpan CorrectionFactor { get; }

/// <summary>
/// Applies the correction factor to the current system UTC time and returns a corrected time
/// </summary>
public DateTime CorrectedUtcNow => GetCorrectedTime(DateTime.UtcNow);

/// <summary>
/// The timespan that is used to calculate a corrected time
/// Applies the correction factor to the reference time and returns a corrected time
/// </summary>
public TimeSpan CorrectionFactor { get; }
/// <param name="referenceTime">The reference time</param>
/// <returns>The reference time with the correction factor applied</returns>
public DateTime GetCorrectedTime(DateTime referenceTime) => referenceTime - CorrectionFactor;
}
Loading

0 comments on commit 48475df

Please sign in to comment.