summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Jennings <Stephen.G.Jennings@gmail.com>2011-11-13 15:42:35 -0800
committerStephen Jennings <Stephen.G.Jennings@gmail.com>2011-11-13 15:42:35 -0800
commit7d47def566300ce199394d79e9a4945039635a96 (patch)
treeeede040951a07097e318e7ae233a5e992bc80930
parent8500067fa96d9e2e387ab5d26ad5c176894b8b9a (diff)
downloadOATH.Net-7d47def566300ce199394d79e9a4945039635a96.zip
OATH.Net-7d47def566300ce199394d79e9a4945039635a96.tar.gz
OATH.Net-7d47def566300ce199394d79e9a4945039635a96.tar.bz2
Remove hex string methods, use BitConverter instead.
-rw-r--r--OATH.Net.Test/MiscExtensionsTests.cs46
-rw-r--r--OATH.Net.Test/OATH.Net.Test.csproj1
-rw-r--r--OATH.Net/CounterBasedOtpGenerator.cs16
-rw-r--r--OATH.Net/MiscExtensions.cs46
-rw-r--r--OATH.Net/OATH.Net.csproj1
5 files changed, 13 insertions, 97 deletions
diff --git a/OATH.Net.Test/MiscExtensionsTests.cs b/OATH.Net.Test/MiscExtensionsTests.cs
deleted file mode 100644
index c829a06..0000000
--- a/OATH.Net.Test/MiscExtensionsTests.cs
+++ /dev/null
@@ -1,46 +0,0 @@
-//------------------------------------------------------------------------------------
-// <copyright file="MiscExtensionsTests.cs" company="Stephen Jennings">
-// Copyright 2011 Stephen Jennings. Licensed under the Apache License, Version 2.0.
-// </copyright>
-//------------------------------------------------------------------------------------
-
-namespace OathNet.Test
-{
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using NUnit.Framework;
- using OathNet;
-
- public class MiscExtensionsTests
- {
- [Test]
- public void HexStringToByteArray_test_1()
- {
- var s = "01234567DEADBEEF";
- var result = s.HexStringToByteArray();
- var expected = new byte[] { 0x01, 0x23, 0x45, 0x67, 0xDE, 0xAD, 0xBE, 0xEF };
- Assert.AreEqual(expected, result);
- }
-
- [Test]
- public void HexStringToByteArray_pads_upper_byte_1()
- {
- var s = "1234567DEADBEEF";
- var result = s.HexStringToByteArray();
- var expected = new byte[] { 0x01, 0x23, 0x45, 0x67, 0xDE, 0xAD, 0xBE, 0xEF };
- Assert.AreEqual(expected, result);
- }
-
- [Test]
- public void ByteArrayToHexString_test_1()
- {
- var data = new byte[] { 0x01, 0x23, 0x45, 0x67, 0xDE, 0xAD, 0xBE, 0xEF };
- var result = data.ByteArrayToHexString();
- var expected = "01234567DEADBEEF";
-
- Assert.AreEqual(expected, result);
- }
- }
-}
diff --git a/OATH.Net.Test/OATH.Net.Test.csproj b/OATH.Net.Test/OATH.Net.Test.csproj
index 5004614..0b7e00f 100644
--- a/OATH.Net.Test/OATH.Net.Test.csproj
+++ b/OATH.Net.Test/OATH.Net.Test.csproj
@@ -57,7 +57,6 @@
<ItemGroup>
<Compile Include="Base32Tests.cs" />
<Compile Include="KeyTests.cs" />
- <Compile Include="MiscExtensionsTests.cs" />
<Compile Include="CounterBasedOtpGeneratorTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="..\OATH.Net\Properties\CommonAssemblyInfo.cs">
diff --git a/OATH.Net/CounterBasedOtpGenerator.cs b/OATH.Net/CounterBasedOtpGenerator.cs
index d876430..44f65d0 100644
--- a/OATH.Net/CounterBasedOtpGenerator.cs
+++ b/OATH.Net/CounterBasedOtpGenerator.cs
@@ -77,9 +77,19 @@ namespace OathNet
/// <returns>The OTP for the given counter value.</returns>
public virtual string GenerateOtp(int counter)
{
- var text = new byte[8];
- var hex = counter.ToString("X16");
- text = hex.HexStringToByteArray();
+ var text = BitConverter.GetBytes(counter);
+
+ if (BitConverter.IsLittleEndian)
+ {
+ Array.Resize(ref text, 8); // text = { 04, 03, 02, 01, 00, 00, 00, 00 }
+ Array.Reverse(text); // text = { 00, 00, 00, 00, 01, 02, 03, 04 }
+ }
+ else
+ {
+ Array.Reverse(text); // text = { 04, 03, 02, 01 }
+ Array.Resize(ref text, 8); // text = { 04, 03, 02, 01, 00, 00, 00, 00 }
+ Array.Reverse(text); // text = { 00, 00, 00, 00, 01, 02, 03, 04 }
+ }
var hash = this.hmacAlgorithm.ComputeHash(this.secretKey.Binary, text);
diff --git a/OATH.Net/MiscExtensions.cs b/OATH.Net/MiscExtensions.cs
deleted file mode 100644
index e99f1f2..0000000
--- a/OATH.Net/MiscExtensions.cs
+++ /dev/null
@@ -1,46 +0,0 @@
-//------------------------------------------------------------------------------------
-// <copyright file="MiscExtensions.cs" company="Stephen Jennings">
-// Copyright 2011 Stephen Jennings. Licensed under the Apache License, Version 2.0.
-// </copyright>
-//------------------------------------------------------------------------------------
-
-namespace OathNet
-{
- using System;
- 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)
- {
- if (s.Length % 2 == 1)
- {
- s = "0" + s;
- }
-
- return Enumerable.Range(0, s.Length - 1)
- .Where(x => x % 2 == 0)
- .Select(x => Byte.Parse(s.Substring(x, 2), NumberStyles.HexNumber))
- .ToArray();
- }
-
- /// <summary>
- /// Converts a byte array to a hexadecimal string.
- /// </summary>
- /// <param name="data">The byte array.</param>
- /// <returns>A hexadecimal string representing the given byte array.</returns>
- public static string ByteArrayToHexString(this byte[] data)
- {
- return BitConverter.ToString(data).Replace("-", String.Empty);
- }
- }
-}
diff --git a/OATH.Net/OATH.Net.csproj b/OATH.Net/OATH.Net.csproj
index f1d7dea..1531105 100644
--- a/OATH.Net/OATH.Net.csproj
+++ b/OATH.Net/OATH.Net.csproj
@@ -69,7 +69,6 @@
<Compile Include="SHA256HMACAlgorithm.cs" />
<Compile Include="CounterBasedOtpGenerator.cs" />
<Compile Include="IHMACAlgorithm.cs" />
- <Compile Include="MiscExtensions.cs" />
<Compile Include="SHA1HMACAlgorithm.cs" />
<Compile Include="TimeBasedOtpGenerator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />