Authentication added. Login/Logout operational.

This commit is contained in:
2012-12-20 21:39:13 -05:00
parent b30d3068e7
commit f129142dab
24 changed files with 640 additions and 65 deletions
+7 -39
View File
@@ -8,24 +8,11 @@ namespace MileageTraker.Web.Membership
{
public static class Crypto
{
private const int TokenSizeInBytes = 16;
private const int Pbkdf2Count = 1000;
private const int Pbkdf2SubkeyLength = 256/8;
private const int SaltSize = 128/8;
public static string GenerateSalt(int byteLength = SaltSize)
{
var Buff = new byte[byteLength];
using (var Prng = new RNGCryptoServiceProvider())
{
Prng.GetBytes(Buff);
}
return Convert.ToBase64String(Buff);
}
public static string Hash(string input, string algorithm = "sha256")
private static string Hash(string input, string algorithm = "sha256")
{
if (input == null)
{
@@ -35,37 +22,24 @@ namespace MileageTraker.Web.Membership
return Hash(Encoding.UTF8.GetBytes(input), algorithm);
}
public static string Hash(byte[] input, string algorithm = "sha256")
private static string Hash(byte[] input, string algorithm = "sha256")
{
if (input == null)
{
throw new ArgumentNullException("input");
}
using (HashAlgorithm alg = HashAlgorithm.Create(algorithm))
using (var alg = HashAlgorithm.Create(algorithm))
{
if (alg != null)
{
byte[] hashData = alg.ComputeHash(input);
var hashData = alg.ComputeHash(input);
return BinaryToHex(hashData);
}
else
{
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "not supported hash alg", algorithm));
}
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "not supported hash alg", algorithm));
}
}
public static string SHA1(string input)
{
return Hash(input, "sha1");
}
public static string SHA256(string input)
{
return Hash(input, "sha256");
}
/* =======================
* HASHED PASSWORD FORMATS
* =======================
@@ -79,9 +53,7 @@ namespace MileageTraker.Web.Membership
public static string HashPassword(string password)
{
if (password == null)
{
throw new ArgumentNullException("password");
}
byte[] salt;
byte[] subkey;
@@ -101,15 +73,11 @@ namespace MileageTraker.Web.Membership
public static bool VerifyHashedPassword(string hashedPassword, string password)
{
if (hashedPassword == null)
{
throw new ArgumentNullException("hashedPassword");
}
if (password == null)
{
throw new ArgumentNullException("password");
}
byte[] hashedPasswordBytes = Convert.FromBase64String(hashedPassword);
var hashedPasswordBytes = Convert.FromBase64String(hashedPassword);
// Verify a version 0 (see comment above) password hash.
@@ -132,7 +100,7 @@ namespace MileageTraker.Web.Membership
return ByteArraysEqual(storedSubkey, generatedSubkey);
}
internal static string BinaryToHex(byte[] data)
private static string BinaryToHex(byte[] data)
{
var hex = new char[data.Length*2];