protected override void Dispose(bool disposing)
 {
     if (disposing && _implementation != null)
     {
         _implementation.Dispose();
         _implementation = null;
     }
     base.Dispose(disposing);
 }
Exemplo n.º 2
0
        // функция для хэширования массива байт заданным алгоритмом, на выходе 16-ричная строка
        // аргументы: входные байты сообщения, алгоритм хэширования
        public static string HeshAlg(Byte[] arrayByte_in, string selectedAlgHesh)
        {
            byte[] arrayByte_out = new byte[0]; // Выходная последовательность байт после хеширования
            string heshString    = "";          // Хеш строка (16 ричный вид)

            try
            {
                switch (selectedAlgHesh) // Получение хеша определенным алгоритмом
                {
                case "MD5":
                    MD5 md5 = MD5.Create();
                    arrayByte_out = md5.ComputeHash(arrayByte_in);
                    md5.Dispose();
                    break;

                case "RIPEMD160":
                    RIPEMD160 ripemd160 = RIPEMD160.Create();
                    arrayByte_out = ripemd160.ComputeHash(arrayByte_in);
                    ripemd160.Dispose();
                    break;

                case "SHA1":
                    SHA1 sha1 = SHA1.Create();
                    arrayByte_out = sha1.ComputeHash(arrayByte_in);
                    sha1.Dispose();
                    break;

                case "SHA256":
                    SHA256 sha256 = SHA256.Create();
                    arrayByte_out = sha256.ComputeHash(arrayByte_in);
                    sha256.Dispose();
                    break;

                case "SHA384":
                    SHA384 sha384 = SHA384.Create();
                    arrayByte_out = sha384.ComputeHash(arrayByte_in);
                    sha384.Dispose();
                    break;

                case "SHA512":
                    SHA512 sha512 = SHA512.Create();
                    arrayByte_out = sha512.ComputeHash(arrayByte_in);
                    sha512.Dispose();
                    break;

                default: break;
                }
                heshString = BitConverter.ToString(arrayByte_out).Replace("-", "");
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message, "НЕПРЕДВИДЕННАЯ ОШИБКА", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(heshString);
        }
Exemplo n.º 3
0
        public static string GetSHA384(string input)
        {
            SHA384 sha384 = SHA384.Create();

            byte[] data = sha384.ComputeHash(Encoding.UTF8.GetBytes(input));

            sha384.Dispose();

            return(BytesToHash(data));
        }
Exemplo n.º 4
0
        public static byte[] ComputeHash(string path, HashType htype)
        {
            FileStream fs = null;

            Byte[] hash = null;

            fs          = new FileStream(path, FileMode.Open, FileAccess.Read);
            fs.Position = 0;

            switch (htype)
            {
            case HashType.MD5:
                MD5 md5 = MD5.Create();
                hash = md5.ComputeHash(fs);
                md5.Dispose();
                break;

            case HashType.SHA1:
                SHA1 sha1 = SHA1.Create();
                hash = sha1.ComputeHash(fs);
                sha1.Dispose();
                break;

            case HashType.SHA256:
                SHA256 sha256 = SHA256.Create();
                hash = sha256.ComputeHash(fs);
                sha256.Dispose();
                break;

            case HashType.SHA384:
                SHA384 sha384 = SHA384.Create();
                hash = sha384.ComputeHash(fs);
                sha384.Dispose();
                break;

            case HashType.SHA512:
                SHA512 sha512 = SHA512.Create();
                hash = sha512.ComputeHash(fs);
                sha512.Dispose();
                break;

            default:
                fs.Close();
                throw new HashException("invalid hash algorithm");
            }

            fs.Close();
            return(hash);
        }
Exemplo n.º 5
0
        public static string GetHash(TypeHash type, string text, byte[] salt = null)
        {
            salt = salt ?? GenerateSalt();
            byte[] plain = ASCIIEncoding.UTF8.GetBytes(text);

            int lenghtPlant = plain.Length,
                lenghtSalt  = salt.Length,
                lenght      = lenghtPlant + lenghtSalt;

            byte[] bytes = new byte[lenght];
            for (int i = 0; i < lenght; ++i)
            {
                if (i < lenghtPlant)
                {
                    bytes[i] = plain[i];
                }
                else
                {
                    bytes[i] = salt[i - lenghtPlant];
                }
            }

            byte[] hash = null;
            switch (type)
            {
            case TypeHash.SHA256:
                SHA256 sha256 = SHA256Managed.Create();
                hash = sha256.ComputeHash(bytes);
                sha256.Dispose();
                break;

            case TypeHash.SHA384:
                SHA384 sha384 = SHA384Managed.Create();
                hash = sha384.ComputeHash(bytes);
                sha384.Dispose();
                break;

            case TypeHash.SHA512:
                SHA512 sha512 = SHA512Managed.Create();
                hash = sha512.ComputeHash(bytes);
                sha512.Dispose();
                break;
            }

            return(GetStringFromHash(hash));
        }
Exemplo n.º 6
0
        public static byte[] ComputeHash(Stream s, HashType htype)
        {
            Byte[] hash = null;

            switch (htype)
            {
            case HashType.MD5:
                MD5 md5 = MD5.Create();
                hash = md5.ComputeHash(s);
                md5.Dispose();
                break;

            case HashType.SHA1:
                SHA1 sha1 = SHA1.Create();
                hash = sha1.ComputeHash(s);
                sha1.Dispose();
                break;

            case HashType.SHA256:
                SHA256 sha256 = SHA256.Create();
                hash = sha256.ComputeHash(s);
                sha256.Dispose();
                break;

            case HashType.SHA384:
                SHA384 sha384 = SHA384.Create();
                hash = sha384.ComputeHash(s);
                sha384.Dispose();
                break;

            case HashType.SHA512:
                SHA512 sha512 = SHA512.Create();
                hash = sha512.ComputeHash(s);
                sha512.Dispose();
                break;

            default:
                throw new HashException("invalid hash algorithm");
            }

            return(hash);
        }
Exemplo n.º 7
0
        public static string GetSHA384Hash(this string input)
        {
            SHA384 SHA384Hash = SHA384.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = SHA384Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2", CultureInfo.CurrentCulture));
            }
            SHA384Hash.Dispose();
            // Return the hexadecimal string.
            return(sBuilder.ToString());
        }
Exemplo n.º 8
0
 public void Dispose()
 {
     hasher?.Dispose();
 }
Exemplo n.º 9
0
 public void Dispose()
 {
     _sha384.Clear();
     _sha384.Dispose();
 }