Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using System.Security.Cryptography;
- using System.Text;
- namespace Helpers
- {
- /// <summary>
- /// Crypto helper class
- /// </summary>
- public static class CryptoHelper
- {
- // Fields
- private const int PBKDF2_ITER_COUNT = 0x3e8;
- private const int PBKDF2_SUBKEY_LENGTH = 0x20;
- private const int SALT_SIZE = 0x10;
- // Methods
- internal static string BinaryToHex(byte[] data)
- {
- char[] chArray = new char[data.Length * 2];
- for (int i = 0; i < data.Length; i++)
- {
- byte num2 = (byte)(data[i] >> 4);
- chArray[i * 2] = (num2 > 9) ? ((char)(num2 + 0x37)) : ((char)(num2 + 0x30));
- num2 = (byte)(data[i] & 15);
- chArray[(i * 2) + 1] = (num2 > 9) ? ((char)(num2 + 0x37)) : ((char)(num2 + 0x30));
- }
- return new string(chArray);
- }
- [MethodImpl(MethodImplOptions.NoOptimization)]
- private static bool ByteArraysEqual(byte[] a, byte[] b)
- {
- if (object.ReferenceEquals(a, b))
- {
- return true;
- }
- if (((a == null) || (b == null)) || (a.Length != b.Length))
- {
- return false;
- }
- bool flag = true;
- for (int i = 0; i < a.Length; i++)
- {
- flag &= a[i] == b[i];
- }
- return flag;
- }
- public static string GenerateSalt([Optional, DefaultParameterValue(0x10)] int byteLength)
- {
- return Convert.ToBase64String(GenerateSaltInternal(byteLength));
- }
- internal static byte[] GenerateSaltInternal([Optional, DefaultParameterValue(0x10)] int byteLength)
- {
- byte[] data = new byte[byteLength];
- using (RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider())
- {
- provider.GetBytes(data);
- }
- return data;
- }
- public static string Hash(byte[] input, [Optional, DefaultParameterValue("sha256")] string algorithm)
- {
- if (input == null)
- {
- throw new ArgumentNullException("input");
- }
- using (HashAlgorithm algorithm2 = HashAlgorithm.Create(algorithm))
- {
- if (algorithm2 == null)
- {
- throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
- "Crypto_NotSupportedHashAlg {0}", new object[] { algorithm }));
- }
- return BinaryToHex(algorithm2.ComputeHash(input));
- }
- }
- public static string Hash(string input, [Optional, DefaultParameterValue("sha256")] string algorithm)
- {
- if (input == null)
- {
- throw new ArgumentNullException("input");
- }
- return Hash(Encoding.UTF8.GetBytes(input), algorithm);
- }
- public static string HashPassword(string password)
- {
- byte[] salt;
- byte[] buffer2;
- if (password == null)
- {
- throw new ArgumentNullException("password");
- }
- using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, 0x10, 0x3e8))
- {
- salt = bytes.Salt;
- buffer2 = bytes.GetBytes(0x20);
- }
- byte[] dst = new byte[0x31];
- Buffer.BlockCopy(salt, 0, dst, 1, 0x10);
- Buffer.BlockCopy(buffer2, 0, dst, 0x11, 0x20);
- return Convert.ToBase64String(dst);
- }
- public static string SHA1(string input)
- {
- return Hash(input, "sha1");
- }
- public static string SHA256(string input)
- {
- return Hash(input, "sha256");
- }
- public static bool VerifyHashedPassword(string hashedPassword, string password)
- {
- byte[] buffer4;
- if (hashedPassword == null)
- {
- throw new ArgumentNullException("hashedPassword");
- }
- if (password == null)
- {
- throw new ArgumentNullException("password");
- }
- byte[] src = Convert.FromBase64String(hashedPassword);
- if ((src.Length != 0x31) || (src[0] != 0))
- {
- return false;
- }
- byte[] dst = new byte[0x10];
- Buffer.BlockCopy(src, 1, dst, 0, 0x10);
- byte[] buffer3 = new byte[0x20];
- Buffer.BlockCopy(src, 0x11, buffer3, 0, 0x20);
- using (Rfc2898DeriveBytes bytes = new Rfc2898DeriveBytes(password, dst, 0x3e8))
- {
- buffer4 = bytes.GetBytes(0x20);
- }
- return ByteArraysEqual(buffer3, buffer4);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment