andrew4582

CryptoHelper

Feb 10th, 2013
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.05 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7.  
  8. namespace Helpers
  9. {
  10.     /// <summary>
  11.     /// Crypto helper class
  12.     /// </summary>
  13.     public static class CryptoHelper
  14.     {
  15.         // Fields
  16.         private const int PBKDF2_ITER_COUNT = 0x3e8;
  17.         private const int PBKDF2_SUBKEY_LENGTH = 0x20;
  18.         private const int SALT_SIZE = 0x10;
  19.  
  20.         // Methods
  21.         internal static string BinaryToHex(byte[] data)
  22.         {
  23.             char[] chArray = new char[data.Length * 2];
  24.             for (int i = 0; i < data.Length; i++)
  25.             {
  26.                 byte num2 = (byte)(data[i] >> 4);
  27.                 chArray[i * 2] = (num2 > 9) ? ((char)(num2 + 0x37)) : ((char)(num2 + 0x30));
  28.                 num2 = (byte)(data[i] & 15);
  29.                 chArray[(i * 2) + 1] = (num2 > 9) ? ((char)(num2 + 0x37)) : ((char)(num2 + 0x30));
  30.             }
  31.             return new string(chArray);
  32.         }
  33.  
  34.         [MethodImpl(MethodImplOptions.NoOptimization)]
  35.         private static bool ByteArraysEqual(byte[] a, byte[] b)
  36.         {
  37.             if (object.ReferenceEquals(a, b))
  38.             {
  39.                 return true;
  40.             }
  41.             if (((a == null) || (b == null)) || (a.Length != b.Length))
  42.             {
  43.                 return false;
  44.             }
  45.             bool flag = true;
  46.             for (int i = 0; i < a.Length; i++)
  47.             {
  48.                 flag &= a[i] == b[i];
  49.             }
  50.             return flag;
  51.         }
  52.  
  53.         public static string GenerateSalt([Optional, DefaultParameterValue(0x10)] int byteLength)
  54.         {
  55.             return Convert.ToBase64String(GenerateSaltInternal(byteLength));
  56.         }
  57.  
  58.         internal static byte[] GenerateSaltInternal([Optional, DefaultParameterValue(0x10)] int byteLength)
  59.         {
  60.             byte[] data = new byte[byteLength];
  61.             using (RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider())
  62.             {
  63.                 provider.GetBytes(data);
  64.             }
  65.             return data;
  66.         }
  67.  
  68.         public static string Hash(byte[] input, [Optional, DefaultParameterValue("sha256")] string algorithm)
  69.         {
  70.             if (input == null)
  71.             {
  72.                 throw new ArgumentNullException("input");
  73.             }
  74.             using (HashAlgorithm algorithm2 = HashAlgorithm.Create(algorithm))
  75.             {
  76.                 if (algorithm2 == null)
  77.                 {
  78.                     throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
  79.                         "Crypto_NotSupportedHashAlg {0}", new object[] { algorithm }));
  80.                 }
  81.                 return BinaryToHex(algorithm2.ComputeHash(input));
  82.             }
  83.         }
  84.  
  85.         public static string Hash(string input, [Optional, DefaultParameterValue("sha256")] string algorithm)
  86.         {
  87.             if (input == null)
  88.             {
  89.                 throw new ArgumentNullException("input");
  90.             }
  91.             return Hash(Encoding.UTF8.GetBytes(input), algorithm);
  92.         }
  93.  
  94.         public static string HashPassword(string password)
  95.         {
  96.             byte[] salt;
  97.             byte[] buffer2;
  98.             if (password == null)
  99.             {
  100.                 throw new ArgumentNullException("password");
  101.             }
  102.             using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, 0x10, 0x3e8))
  103.             {
  104.                 salt = bytes.Salt;
  105.                 buffer2 = bytes.GetBytes(0x20);
  106.             }
  107.             byte[] dst = new byte[0x31];
  108.             Buffer.BlockCopy(salt, 0, dst, 1, 0x10);
  109.             Buffer.BlockCopy(buffer2, 0, dst, 0x11, 0x20);
  110.             return Convert.ToBase64String(dst);
  111.         }
  112.  
  113.         public static string SHA1(string input)
  114.         {
  115.             return Hash(input, "sha1");
  116.         }
  117.  
  118.         public static string SHA256(string input)
  119.         {
  120.             return Hash(input, "sha256");
  121.         }
  122.  
  123.         public static bool VerifyHashedPassword(string hashedPassword, string password)
  124.         {
  125.             byte[] buffer4;
  126.             if (hashedPassword == null)
  127.             {
  128.                 throw new ArgumentNullException("hashedPassword");
  129.             }
  130.             if (password == null)
  131.             {
  132.                 throw new ArgumentNullException("password");
  133.             }
  134.             byte[] src = Convert.FromBase64String(hashedPassword);
  135.             if ((src.Length != 0x31) || (src[0] != 0))
  136.             {
  137.                 return false;
  138.             }
  139.             byte[] dst = new byte[0x10];
  140.             Buffer.BlockCopy(src, 1, dst, 0, 0x10);
  141.             byte[] buffer3 = new byte[0x20];
  142.             Buffer.BlockCopy(src, 0x11, buffer3, 0, 0x20);
  143.             using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, dst, 0x3e8))
  144.             {
  145.                 buffer4 = bytes.GetBytes(0x20);
  146.             }
  147.             return ByteArraysEqual(buffer3, buffer4);
  148.         }
  149.     }
  150.  
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment