Skip to content

Commit

Permalink
initial port of otpsharp
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Spearrin committed Jan 14, 2017
1 parent 354040e commit eb78321
Show file tree
Hide file tree
Showing 19 changed files with 1,089 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,6 @@ paket-files/
# JetBrains Rider
.idea/
*.sln.iml

.nuget/
node_modules/
File renamed without changes.
16 changes: 16 additions & 0 deletions Otp.NET.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?><package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Otp.NET</id>
<version>1.0.0</version>
<authors>Kyle Spearrin</authors>
<projectUrl>https://github.com/kspearrin/Otp.NET</projectUrl>
<licenseUrl>https://raw.githubusercontent.com/kspearrin/Otp.NET/master/LICENSE.txt</licenseUrl>
<title>Otp.NET</title>
<description>An implementation of TOTP which is commonly used for multi factor authentication by using a shared key between the client and the server to generate and verify one time use codes. For documentation, visit https://github.com/kspearrin/BerTlv.NET</description>
<tags>otp totp 2fa</tags>
</metadata>
<files>
<file src="src\Otp.NET\bin\Release\net45\Otp.NET.dll" target="lib\net45"/>
<file src="src\Otp.NET\bin\Release\netstandard1.3\Otp.NET.dll" target="lib\netstandard1.3"/>
</files>
</package>
32 changes: 32 additions & 0 deletions Otp.NET.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{57F82DBE-510A-4E78-ADCD-7A18DB80AA87}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E5579496-CD66-4961-8DD1-A53BA74229E3}"
ProjectSection(SolutionItems) = preProject
global.json = global.json
EndProjectSection
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Otp.NET", "src\Otp.NET\Otp.NET.xproj", "{E630B67F-150A-4978-A2DD-51B8D8E783EF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E630B67F-150A-4978-A2DD-51B8D8E783EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E630B67F-150A-4978-A2DD-51B8D8E783EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E630B67F-150A-4978-A2DD-51B8D8E783EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E630B67F-150A-4978-A2DD-51B8D8E783EF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E630B67F-150A-4978-A2DD-51B8D8E783EF} = {57F82DBE-510A-4E78-ADCD-7A18DB80AA87}
EndGlobalSection
EndGlobal
19 changes: 19 additions & 0 deletions build.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@echo off
cd %~dp0

SETLOCAL
SET CACHED_NUGET=%LocalAppData%\NuGet\NuGet.exe

IF EXIST %CACHED_NUGET% goto copynuget
IF NOT EXIST %LocalAppData%\NuGet md %LocalAppData%\NuGet
@powershell -NoProfile -ExecutionPolicy unrestricted -Command "$ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest 'https://www.nuget.org/nuget.exe' -OutFile '%CACHED_NUGET%'"

:copynuget
IF EXIST .nuget\nuget.exe goto build
md .nuget
copy %CACHED_NUGET% .nuget\nuget.exe > nul

:build
call npm install -g gulp
call npm install
call gulp
6 changes: 6 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-preview2-003131"
}
}
52 changes: 52 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var p = require('./package.json'),
gulp = require('gulp'),
assemblyInfo = require('gulp-dotnet-assembly-info'),
xmlpoke = require('gulp-xmlpoke'),
msbuild = require('gulp-msbuild'),
nuget = require('nuget-runner')({
apiKey: process.env.NUGET_API_KEY,
nugetPath: '.nuget/nuget.exe'
});

gulp.task('default', ['nuget']);

gulp.task('restore', [], function () {
return nuget
.restore({
packages: 'Otp.NET.sln',
verbosity: 'normal'
});
});

gulp.task('build', ['restore'], function () {
return gulp
.src('Otp.NET.sln')
.pipe(msbuild({
toolsVersion: 14.0,
targets: ['Clean', 'Build'],
errorOnFail: true,
configuration: 'Release'
}));
});

gulp.task('nuspec', ['build'], function () {
return gulp
.src('Otp.NET.nuspec')
.pipe(xmlpoke({
replacements: [{
xpath: "//package:version",
namespaces: { "package": "http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd" },
value: p.version
}]
}))
.pipe(gulp.dest('.'));
});

gulp.task('nuget', ['nuspec'], function () {
return nuget
.pack({
spec: 'Otp.NET.nuspec',
outputDirectory: 'src/Otp.NET/bin/Release',
version: p.version
});
});
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "otpnet",
"version": "1.0.0",
"description": "An implementation of TOTP which is commonly used for multi factor authentication by using a shared key between the client and the server to generate and verify one time use codes.",
"homepage": "https://github.com/kspearrin/Otp.NET",
"author": "Kyle Spearrin",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/kspearrin/Otp.NET"
},
"dependencies": {
"gulp": "^3.9.0",
"gulp-dotnet-assembly-info": "^0.1.10",
"gulp-msbuild": "^0.2.11",
"gulp-xmlpoke": "^0.2.0",
"nuget-runner": "^0.1.5"
}
}
133 changes: 133 additions & 0 deletions src/Otp.NET/Base32Encoding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
Credits to "Shane" from SO answer here:
http://stackoverflow.com/a/7135008/1090359
*/

using System;

namespace OtpNet
{
public class Base32Encoding
{
public static byte[] ToBytes(string input)
{
if(string.IsNullOrEmpty(input))
{
throw new ArgumentNullException("input");
}

input = input.TrimEnd('='); //remove padding characters
int byteCount = input.Length * 5 / 8; //this must be TRUNCATED
byte[] returnArray = new byte[byteCount];

byte curByte = 0, bitsRemaining = 8;
int mask = 0, arrayIndex = 0;

foreach(char c in input)
{
int cValue = CharToValue(c);

if(bitsRemaining > 5)
{
mask = cValue << (bitsRemaining - 5);
curByte = (byte)(curByte | mask);
bitsRemaining -= 5;
}
else
{
mask = cValue >> (5 - bitsRemaining);
curByte = (byte)(curByte | mask);
returnArray[arrayIndex++] = curByte;
curByte = (byte)(cValue << (3 + bitsRemaining));
bitsRemaining += 3;
}
}

//if we didn't end with a full byte
if(arrayIndex != byteCount)
{
returnArray[arrayIndex] = curByte;
}

return returnArray;
}

public static string ToString(byte[] input)
{
if(input == null || input.Length == 0)
{
throw new ArgumentNullException("input");
}

int charCount = (int)Math.Ceiling(input.Length / 5d) * 8;
char[] returnArray = new char[charCount];

byte nextChar = 0, bitsRemaining = 5;
int arrayIndex = 0;

foreach(byte b in input)
{
nextChar = (byte)(nextChar | (b >> (8 - bitsRemaining)));
returnArray[arrayIndex++] = ValueToChar(nextChar);

if(bitsRemaining < 4)
{
nextChar = (byte)((b >> (3 - bitsRemaining)) & 31);
returnArray[arrayIndex++] = ValueToChar(nextChar);
bitsRemaining += 5;
}

bitsRemaining -= 3;
nextChar = (byte)((b << bitsRemaining) & 31);
}

//if we didn't end with a full char
if(arrayIndex != charCount)
{
returnArray[arrayIndex++] = ValueToChar(nextChar);
while(arrayIndex != charCount) returnArray[arrayIndex++] = '='; //padding
}

return new string(returnArray);
}

private static int CharToValue(char c)
{
int value = (int)c;

//65-90 == uppercase letters
if(value < 91 && value > 64)
{
return value - 65;
}
//50-55 == numbers 2-7
if(value < 56 && value > 49)
{
return value - 24;
}
//97-122 == lowercase letters
if(value < 123 && value > 96)
{
return value - 97;
}

throw new ArgumentException("Character is not a Base32 character.", "c");
}

private static char ValueToChar(byte b)
{
if(b < 26)
{
return (char)(b + 65);
}

if(b < 32)
{
return (char)(b + 24);
}

throw new ArgumentException("Byte is not a value Base32 value.", "b");
}

}
}
71 changes: 71 additions & 0 deletions src/Otp.NET/KeyGeneration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Credits to Devin Martin and the original OtpSharp library:
https://bitbucket.org/devinmartin/otp-sharp/overview
Copyright (C) 2012 Devin Martin
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

namespace OtpNet
{
/// <summary>
/// Helpers to work with keys
/// </summary>
public static class KeyGeneration
{
/// <summary>
/// Generates a random key in accordance with the RFC recommened length for each algorithm
/// </summary>
/// <param name="length">Key length</param>
/// <returns>The generated key</returns>
public static byte[] GenerateRandomKey(int length)
{
byte[] key = new byte[length];
using(var rnd = System.Security.Cryptography.RandomNumberGenerator.Create())
{
rnd.GetBytes(key);
return key;
}
}

/// <summary>
/// Generates a random key in accordance with the RFC recommened length for each algorithm
/// </summary>
/// <param name="mode">HashMode</param>
/// <returns>Key</returns>
public static byte[] GenerateRandomKey(OtpHashMode mode = OtpHashMode.Sha1)
{
return GenerateRandomKey(LengthForMode(mode));
}

private static int LengthForMode(OtpHashMode mode)
{
switch(mode)
{
case OtpHashMode.Sha256:
return 32;
case OtpHashMode.Sha512:
return 64;
default: //case OtpHashMode.Sha1:
return 20;
}
}
}
}
Loading

0 comments on commit eb78321

Please sign in to comment.