Browse files

Initial commit

  • Loading branch information...
0 parents commit 2fd9927cee3bcadb4ee2c049e3854df2bd91cd52 @joeyb committed Feb 15, 2012
Showing with 229 additions and 0 deletions.
  1. +32 −0 .gitignore
  2. +20 −0 JoeyB.Security.sln
  3. +88 −0 JoeyB.Security/Cryptography.cs
  4. +54 −0 JoeyB.Security/JoeyB.Security.csproj
  5. +35 −0 JoeyB.Security/Properties/AssemblyInfo.cs
32 .gitignore
@@ -0,0 +1,32 @@
+*.obj
+*.pdb
+*.user
+*.aps
+*.pch
+*.vspscc
+*.vssscc
+*_i.c
+*_p.c
+*.ncb
+*.suo
+*.tlb
+*.tlh
+*.bak
+*.cache
+*.ilk
+*.log
+*.lib
+*.sbr
+*.scc
+*.DS_Store
+[Bb]in
+[Dd]ebug*/
+obj/
+[Rr]elease*/
+_ReSharper*/
+[Tt]humbs.db
+[Tt]est[Rr]esult*
+[Bb]uild[Ll]og.*
+*.[Pp]ublish.xml
+*.resharper
+[Pp]ackages
20 JoeyB.Security.sln
@@ -0,0 +1,20 @@
+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JoeyB.Security", "JoeyB.Security\JoeyB.Security.csproj", "{9259EF16-70DC-482F-B06D-4946757440B8}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {9259EF16-70DC-482F-B06D-4946757440B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {9259EF16-70DC-482F-B06D-4946757440B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {9259EF16-70DC-482F-B06D-4946757440B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {9259EF16-70DC-482F-B06D-4946757440B8}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
88 JoeyB.Security/Cryptography.cs
@@ -0,0 +1,88 @@
+using System;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace JoeyB.Security
+{
+ public static class Cryptography
+ {
+ #region Constants
+
+ private const int DefaultHashIterations = 1000;
+ private const int SaltSize = 16;
+
+ #endregion
+
+ #region Fields
+
+ /// <summary>
+ /// Source for crypto-grade random numbers.
+ ///
+ /// Should only be accessed through the Random() method for locking purposes.
+ /// </summary>
+ private static readonly RandomNumberGenerator RandomNumberGenerator = RandomNumberGenerator.Create();
+
+ #endregion
+
+ #region Methods
+
+ /// <summary>
+ /// Crypto-grade, thread-safe random number generator.
+ /// </summary>
+ /// <param name="bytes"></param>
+ /// <returns></returns>
+ public static byte[] Random(int bytes)
+ {
+ var output = new byte[bytes];
+
+ lock (RandomNumberGenerator) RandomNumberGenerator.GetBytes(output);
+
+ return output;
+ }
+
+ /// <summary>
+ /// Generate a random salt.
+ ///
+ /// The format for salts is [number of iterations in hex].[salt number] (without the brackets).
+ /// </summary>
+ /// <param name="explicitIterations"></param>
+ /// <returns></returns>
+ public static string GenerateSalt(int? explicitIterations = null)
+ {
+ if (explicitIterations.HasValue && explicitIterations.Value < DefaultHashIterations)
+ throw new ArgumentOutOfRangeException("explicitIterations", explicitIterations.Value,
+ "Cannot be less than " + DefaultHashIterations);
+
+ var bytes = Random(SaltSize);
+
+ var iterations = (explicitIterations ?? DefaultHashIterations).ToString("X");
+
+ return iterations + "." + Convert.ToBase64String(bytes);
+ }
+
+ /// <summary>
+ /// Securely hash a string using the specified salt.
+ ///
+ /// The salt is expected to be in the same format as returned by the GenerateSalt() method.
+ /// </summary>
+ /// <param name="value"></param>
+ /// <param name="salt"></param>
+ /// <returns></returns>
+ public static string Hash(string value, string salt)
+ {
+ var i = salt.IndexOf('.');
+ var iterations = int.Parse(salt.Substring(0, i), System.Globalization.NumberStyles.HexNumber);
+
+ salt = salt.Substring(i + 1);
+
+ using (var pbkdf2 = new Rfc2898DeriveBytes(Encoding.UTF8.GetBytes(value), Convert.FromBase64String(salt), iterations))
+ {
+ var key = pbkdf2.GetBytes(24);
+
+ return Convert.ToBase64String(key);
+ }
+ }
+
+ #endregion
+ }
+}
54 JoeyB.Security/JoeyB.Security.csproj
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.30703</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{9259EF16-70DC-482F-B06D-4946757440B8}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>JoeyB.Security</RootNamespace>
+ <AssemblyName>JoeyB.Security</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <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' ">
+ <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="Cryptography.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </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>
35 JoeyB.Security/Properties/AssemblyInfo.cs
@@ -0,0 +1,35 @@
+using System.Reflection;
+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("JoeyB.Security")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("JoeyB.Security")]
+[assembly: AssemblyCopyright("Copyright © 2012")]
+[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("32d1144c-0f58-4887-aac9-d96d59c4aa95")]
+
+// 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")]

0 comments on commit 2fd9927

Please sign in to comment.