diff options
-rw-r--r-- | TwoStepsAuthenticator.TestApp/App.config | 6 | ||||
-rw-r--r-- | TwoStepsAuthenticator.TestApp/Program.cs | 17 | ||||
-rw-r--r-- | TwoStepsAuthenticator.TestApp/Properties/AssemblyInfo.cs | 36 | ||||
-rw-r--r-- | TwoStepsAuthenticator.TestApp/TwoStepsAuthenticator.TestApp.csproj | 64 | ||||
-rw-r--r-- | TwoStepsAuthenticator.TestWebsite.sln | 6 | ||||
-rw-r--r-- | TwoStepsAuthenticator/TwoStepsAuthenticator.cs | 65 |
6 files changed, 47 insertions, 147 deletions
diff --git a/TwoStepsAuthenticator.TestApp/App.config b/TwoStepsAuthenticator.TestApp/App.config deleted file mode 100644 index 8e15646..0000000 --- a/TwoStepsAuthenticator.TestApp/App.config +++ /dev/null @@ -1,6 +0,0 @@ -<?xml version="1.0" encoding="utf-8" ?> -<configuration> - <startup> - <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> - </startup> -</configuration>
\ No newline at end of file diff --git a/TwoStepsAuthenticator.TestApp/Program.cs b/TwoStepsAuthenticator.TestApp/Program.cs deleted file mode 100644 index 6aa9a3a..0000000 --- a/TwoStepsAuthenticator.TestApp/Program.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TwoStepsAuthenticator.TestApp -{ - class Program - { - static void Main(string[] args) - { - var auth = new TwoStepsAuthenticator(); - auth.GetPinCode("JBSWY3DPEHPK3PXP", new DateTime(2013, 6, 12, 18, 0, 0)); - } - } -} diff --git a/TwoStepsAuthenticator.TestApp/Properties/AssemblyInfo.cs b/TwoStepsAuthenticator.TestApp/Properties/AssemblyInfo.cs deleted file mode 100644 index 780e009..0000000 --- a/TwoStepsAuthenticator.TestApp/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("TwoStepsAuthenticator.TestApp")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("TwoStepsAuthenticator.TestApp")] -[assembly: AssemblyCopyright("Copyright © 2013")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("76a40173-f28a-4d49-80b9-3d25353ab795")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/TwoStepsAuthenticator.TestApp/TwoStepsAuthenticator.TestApp.csproj b/TwoStepsAuthenticator.TestApp/TwoStepsAuthenticator.TestApp.csproj deleted file mode 100644 index 31b0300..0000000 --- a/TwoStepsAuthenticator.TestApp/TwoStepsAuthenticator.TestApp.csproj +++ /dev/null @@ -1,64 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> - <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> - <PropertyGroup> - <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> - <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> - <ProjectGuid>{D14B3558-877D-4219-817F-F61670A62A26}</ProjectGuid> - <OutputType>Exe</OutputType> - <AppDesignerFolder>Properties</AppDesignerFolder> - <RootNamespace>TwoStepsAuthenticator.TestApp</RootNamespace> - <AssemblyName>TwoStepsAuthenticator.TestApp</AssemblyName> - <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> - <FileAlignment>512</FileAlignment> - </PropertyGroup> - <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> - <PlatformTarget>AnyCPU</PlatformTarget> - <DebugSymbols>true</DebugSymbols> - <DebugType>full</DebugType> - <Optimize>false</Optimize> - <OutputPath>bin\Debug\</OutputPath> - <DefineConstants>DEBUG;TRACE</DefineConstants> - <ErrorReport>prompt</ErrorReport> - <WarningLevel>4</WarningLevel> - </PropertyGroup> - <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> - <PlatformTarget>AnyCPU</PlatformTarget> - <DebugType>pdbonly</DebugType> - <Optimize>true</Optimize> - <OutputPath>bin\Release\</OutputPath> - <DefineConstants>TRACE</DefineConstants> - <ErrorReport>prompt</ErrorReport> - <WarningLevel>4</WarningLevel> - </PropertyGroup> - <ItemGroup> - <Reference Include="System" /> - <Reference Include="System.Core" /> - <Reference Include="System.Xml.Linq" /> - <Reference Include="System.Data.DataSetExtensions" /> - <Reference Include="Microsoft.CSharp" /> - <Reference Include="System.Data" /> - <Reference Include="System.Xml" /> - </ItemGroup> - <ItemGroup> - <Compile Include="Program.cs" /> - <Compile Include="Properties\AssemblyInfo.cs" /> - </ItemGroup> - <ItemGroup> - <None Include="App.config" /> - </ItemGroup> - <ItemGroup> - <ProjectReference Include="..\TwoStepsAuthenticator\TwoStepsAuthenticator.csproj"> - <Project>{6c898cd1-0bf3-4711-847e-ad7dac815cd8}</Project> - <Name>TwoStepsAuthenticator</Name> - </ProjectReference> - </ItemGroup> - <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> - <!-- To modify your build process, add your task inside one of the targets below and uncomment it. - Other similar extension points exist, see Microsoft.Common.targets. - <Target Name="BeforeBuild"> - </Target> - <Target Name="AfterBuild"> - </Target> - --> -</Project>
\ No newline at end of file diff --git a/TwoStepsAuthenticator.TestWebsite.sln b/TwoStepsAuthenticator.TestWebsite.sln index 1f71921..2cf0269 100644 --- a/TwoStepsAuthenticator.TestWebsite.sln +++ b/TwoStepsAuthenticator.TestWebsite.sln @@ -5,8 +5,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TwoStepsAuthenticator.TestW EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TwoStepsAuthenticator", "TwoStepsAuthenticator\TwoStepsAuthenticator.csproj", "{6C898CD1-0BF3-4711-847E-AD7DAC815CD8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TwoStepsAuthenticator.TestApp", "TwoStepsAuthenticator.TestApp\TwoStepsAuthenticator.TestApp.csproj", "{D14B3558-877D-4219-817F-F61670A62A26}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,10 +19,6 @@ Global {6C898CD1-0BF3-4711-847E-AD7DAC815CD8}.Debug|Any CPU.Build.0 = Debug|Any CPU {6C898CD1-0BF3-4711-847E-AD7DAC815CD8}.Release|Any CPU.ActiveCfg = Release|Any CPU {6C898CD1-0BF3-4711-847E-AD7DAC815CD8}.Release|Any CPU.Build.0 = Release|Any CPU - {D14B3558-877D-4219-817F-F61670A62A26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D14B3558-877D-4219-817F-F61670A62A26}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D14B3558-877D-4219-817F-F61670A62A26}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D14B3558-877D-4219-817F-F61670A62A26}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TwoStepsAuthenticator/TwoStepsAuthenticator.cs b/TwoStepsAuthenticator/TwoStepsAuthenticator.cs index 5cd599c..958c211 100644 --- a/TwoStepsAuthenticator/TwoStepsAuthenticator.cs +++ b/TwoStepsAuthenticator/TwoStepsAuthenticator.cs @@ -9,34 +9,63 @@ namespace TwoStepsAuthenticator { public class TwoStepsAuthenticator { - //private static final int PASS_CODE_LENGTH = 6; - ///** Default passcode timeout period (in seconds) */ - //private static final int INTERVAL = 30; - HMACSHA1 mac; - public void GetPinCode(string secret, DateTime date) - { + public string GetCode(string secret) + { + return GetCode(secret, DateTime.Now); + } + + public string GetCode(string secret, DateTime date) + { + var key = Base32Encoding.ToBytes(secret); + for (int i = secret.Length; i < key.Length; i++) + { + key[i] = 0; + } + TimeSpan ts = (date.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)); var interval = (long)ts.TotalSeconds / 30; - var challengeBytes = BitConverter.GetBytes(interval); + long chlg = interval; + byte[] challenge = new byte[8]; + for (int j = 7; j >= 0; j--) + { + challenge[j] = (byte)((int)chlg & 0xff); + chlg >>= 8; + } + HMACSHA1 mac = new HMACSHA1(key); + var hash = mac.ComputeHash(challenge); - var key = Base32Encoding.ToBytes(secret); - // OK jusque là - mac = new HMACSHA1(key); - - var hash = mac.ComputeHash(challengeBytes); - //var hash = mac.TransformFinalBlock(challengeBytes); - //int offset = hash[hash.Length - 1] & 0xF; - //int truncatedHash = hashToInt(hash, offset) & 0x7FFFFFFF; - } + int offset = hash[hash.Length - 1] & 0xf; + + int truncatedHash = 0; + for (int j = 0; j < 4; j++) + { + truncatedHash <<= 8; + truncatedHash |= hash[offset + j]; + } + truncatedHash &= 0x7FFFFFFF; + truncatedHash %= 1000000; + string code = truncatedHash.ToString(); + return code.PadLeft(6, '0'); + } - private int hashToInt(byte[] bytes, int start) + public bool CheckCode(string secret, string code) { - return BitConverter.ToInt32(bytes, start); + var baseTime = DateTime.Now; + for (int i = -2; i <= 2; i++) + { + var checkTime = baseTime.AddSeconds(30 * i); + if (GetCode(secret, checkTime) == code) + return true; + } + + return false; } + + } } |