/// <summary>
 /// Get the MD5 hash value as a HEX-string
 /// </summary>
 /// <param name="md5">Instance of MD5</param>
 /// <param name="filepath">The input filepath for the hash code is to be calculated. </param>
 /// <returns>The calculated hash code as Hex-string</returns>
 public static string FromFile(this MD5 md5, string filepath)
 {
     using (var stream = File.OpenRead(filepath))
     {
         return md5.ComputeHash(stream).ToHexString();
     }
 }
 public static byte[] ComputeFileHash(this MD5CryptoServiceProvider md5, string filename)
 {
     using (var fileStream = new FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
     {
             return md5.ComputeHash(fileStream);
     }
 }
Example #3
0
 /// <summary>Computes the hash value for the specified <see cref="String" /> <paramref name="input"/>.</summary>
 /// <param name="md5">The <see cref="MD5" /> instance that will perform the hash computation.</param>
 /// <param name="input">A <see cref="String" /> value.</param>
 /// <param name="encoding">An <see cref="Encoding" /> object that will be used to encode <paramref name="input" /> to bytes.</param>
 /// <returns>A <see cref="String" /> containing the computed hash of <paramref name="input"/>.</returns>
 public static String ComputeHashString(this MD5 md5, String input, Encoding encoding)
 {            
     Byte[] inputBytes = encoding.GetBytes(input);
     var hashBuilder = new StringBuilder();
     List<Byte> hashBytes = md5.ComputeHash(inputBytes).ToList();
     hashBytes.ForEach(hashByte => hashBuilder.AppendFormat("{0:x2}", hashByte));
     return hashBuilder.ToString();            
 }
        public static string ComputeHash(this ICryptoService cryptoService, string data, string salt)
        {
            byte[] dataBytes = Encoding.UTF8.GetBytes(data);
            byte[] saltBytes = Convert.FromBase64String(salt);

            byte[] hashBytes = cryptoService.ComputeHash(dataBytes, saltBytes);

            return Convert.ToBase64String(hashBytes);
        }
        public static string ComputeHash(this HashAlgorithm Hash, string Buffer)
        {
            StringBuilder digestString = new StringBuilder();

            foreach (byte b in Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(Buffer)))
                digestString.Append(Convert.ToString(b, 16).PadLeft(2, '0'));

            return digestString.ToString();
        }
        public static string ComputeHashAsString(this HashAlgorithm hashAlgorithm, byte[] buffer)
        {
            var hashBytes = hashAlgorithm.ComputeHash(buffer);
            var builder = new StringBuilder();
            foreach (var @byte in hashBytes) {
                builder.Append(@byte.ToString("x"));
            }

            return builder.ToString();
        }
 public static string ComputeStringHash(this HashAlgorithm algorithm, byte[] content)
 {
     var computedHash = algorithm.ComputeHash(content);
     var sb = new StringBuilder();
     for (var i = 0; i < computedHash.Length; i++)
     {
         sb.Append(computedHash[i].ToString("x2"));
     }
     return sb.ToString();
 }
        /// <summary>
        /// Computes the hash value for the specified string.
        /// </summary>
        /// <param name="hashType">The type of algorithm to use when computing the hash.</param>
        /// <param name="value">The input to compute the hash code for.</param>
        /// <param name="encoding">
        /// The encoding to use when retrieving the binary representation of <paramref name="value"/>.
        /// If null or not supplied, then <see cref="Encoding.UTF8"/> is used instead.
        /// </param>
        /// <returns>The computed hash code.</returns>
        public static string ComputeHash(this HashType hashType, string value, Encoding encoding = null)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            encoding = encoding ?? Encoding.UTF8;

            return hashType.ComputeHash(encoding.GetBytes(value));
        }
Example #9
0
        // MD5 Hash object to string conversion
        public static string GetHash(this MD5 str, string pass)
        {
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(pass);
            byte[] hash = str.ComputeHash(inputBytes);

            // Convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }
Example #10
0
        public static string ComputeHash(this SHA1 sha1, string input, Encoding encoding)
        {
            // step 1, calculate SHA1 hash from input
            var inputBytes = encoding.GetBytes(input);
            var hash = sha1.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            var sb = new StringBuilder();
            foreach (var t in hash)
            {
                sb.Append(t.ToString("X2"));
            }
            return sb.ToString();
        }
Example #11
0
        public static string GetHashedString(this HashAlgorithm algorithm, string value)
        {
            if (string.IsNullOrEmpty(value)) return null;

            var hash = algorithm.ComputeHash(Encoding.UTF8.GetBytes(value));
            var sb = new StringBuilder();

            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("x2"));
            }

            return sb.ToString();
        }
Example #12
0
        public static string GetHash(this HashAlgorithm alg, string text)
        {
            string result = null;

            // Convert plain text into a byte array.
            byte[] textBytes = Encoding.UTF8.GetBytes(text);

            byte[] hashBytes = alg.ComputeHash(textBytes);

            // Convert result into a base64-encoded string.
            result = Convert.ToBase64String(hashBytes);

            return result;
        }
        public static string HashString(this System.Security.Cryptography.HashAlgorithm algo, string value, System.Text.Encoding encoding = null)
        {
            if (encoding == null) encoding = System.Text.Encoding.Default;

            algo.Initialize();
            byte[] hashBytes = algo.ComputeHash(encoding.GetBytes(value));

            StringBuilder sbHash = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                sbHash.Append(hashBytes[i].ToString("x").ToLower().PadLeft(2, '0'));
            }
            return sbHash.ToString();
        }
		public static unsafe int ComputeIntHash(this HashAlgorithm alg, byte[] dat)
		{
			int h;
			byte[] hash = alg.ComputeHash(dat);
			fixed(byte* barr = hash)
			{
				int* iarr = (int*)barr;
				h = iarr[0];
				for (int i = 1; i < hash.Length >> 2; i++)
				{
					h ^= iarr[i];
				}
			}
			return h;
		}
Example #15
0
        public static byte[] ComputeHash(this HashAlgorithm hashAlgorithm, string fileName)
        {
            if (hashAlgorithm == null)
                throw new ArgumentNullException("hashAlgorithm");

            if (fileName == null)
                throw new ArgumentNullException("filename");

            if (!File.Exists(fileName))
                throw new FileNotFoundException("Unable to locate file", fileName);

            using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                return hashAlgorithm.ComputeHash(stream);
            }
        }
Example #16
0
        /// <summary>
        /// Compute the string with the given <paramref name="algorithm"/>
        /// </summary>
        /// <param name="algorithm">
        /// The <see cref="HashAlgorithm"/>.
        /// </param>
        /// <param name="input">
        /// (string) value to get Hash code
        /// </param>
        /// <returns>
        /// (string) Hash value
        /// </returns>
        public static string ComputeHash(this HashAlgorithm algorithm, string input)
        {
            // Convert the input string to a byte array and compute the algorithm.
            byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes and create a string.
            var builder = 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++)
            {
                builder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return builder.ToString();
        }
Example #17
0
        public static string GetMd5HashString(this MD5 md5Hash, string input)
        {
            // Convert the input string to a byte array and compute the hash.
            var data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

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

            // Loop through each byte of the hashed data
            // and format each one as a hexadecimal string.
            foreach (var t in data)
            {
                sBuilder.Append(t.ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }
        /// <summary>
        /// Computes a hash from hash data brokers using the given
        /// hashing algorithm.
        /// </summary>
        /// <param name="algorithm">The hashing algorithm to use.</param>
        /// <param name="brokers">The data brokers to hash.</param>
        public static BigInteger FinalizeHash(this HashAlgorithm algorithm, params HashDataBroker[] brokers)
        {
            Contract.Requires(algorithm != null);
            Contract.Requires(brokers != null);
            Contract.Requires(brokers.Length >= 0);
            Contract.Ensures(Contract.Result<BigInteger>() != null);
            Contract.Ensures(Contract.Result<BigInteger>().ByteLength == algorithm.HashSize / 8);

            using (var buffer = new MemoryStream())
            {
                foreach (var broker in brokers)
                    buffer.Write(broker.GetRawData(), 0, broker.Length);

                buffer.Position = 0;

                var result = new BigInteger(algorithm.ComputeHash(buffer));
                Contract.Assume(result.ByteLength == algorithm.HashSize / 8);
                return result;
            }
        }
        /// <summary>
        /// Computes the hash.
        /// </summary>
        /// <param name="provider">The authentication provider.</param>
        /// <param name="version">The version.</param>
        /// <param name="header">The header.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="scope">The scope.</param>
        /// <param name="privacy">The privacy provider.</param>
        public static void ComputeHash(this IAuthenticationProvider provider, VersionCode version, Header header, SecurityParameters parameters, ISegment scope, IPrivacyProvider privacy)
        {
            if (provider == null)
            {
                throw new ArgumentNullException("provider");
            }
            
            if (header == null)
            {
                throw new ArgumentNullException("header");
            }

            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }

            if (privacy == null)
            {
                throw new ArgumentNullException("privacy");
            }

            if (provider is DefaultAuthenticationProvider)
            {
                return;
            }

            if (0 == (header.SecurityLevel & Levels.Authentication))
            {
                return;
            }

            var scopeData = privacy.GetScopeData(header, parameters, scope.GetData(version));
            parameters.AuthenticationParameters = provider.ComputeHash(version, header, parameters, scopeData, privacy, null); // replace the hash.
        }
Example #20
0
 /// <summary>
 /// Hash 값을 계산합니다.
 /// </summary>
 /// <param name="hasher">Hash 알고리즘을 이용하는 암호기</param>
 /// <param name="plainText">원본 문자열</param>
 /// <returns>Hashing 된 바이트 배열</returns>
 public static byte[] ComputeHashToBytes(this IHashEncryptor hasher, string plainText) {
     return hasher.ComputeHash(plainText);
 }
		public static string ComputeHex(this IHashAlgorithm hashAlgorithm, string value)
		{
			var hash = hashAlgorithm.ComputeHash(value);

			return hash.ComputeHex();
		}
Example #22
0
 /// <summary>
 /// Hash 값을 계산해서, 문자열로 반환합니다.
 /// </summary>
 /// <param name="hasher">Hash 알고리즘을 이용하는 암호기</param>
 /// <param name="plainText">원본 문자열</param>
 /// <param name="format">암호화된 정보의 문자열 형식(Base64|HexDecimal)</param>
 /// <returns>Hashing 된 문자열</returns>
 public static string ComputeHashToString(this IHashEncryptor hasher, string plainText,
                                          EncryptionStringFormat format = EncryptionStringFormat.HexDecimal) {
     return hasher.ComputeHash(plainText, format);
 }
 /// <summary>
 /// Computes the hash value for the specified <see cref="Stream"/> object.
 /// </summary>
 /// <param name="hashType">The type of algorithm to use when computing the hash.</param>
 /// <param name="inputStream">The input to compute the hash code for.</param>
 /// <returns>The computed hash code.</returns>
 public static string ComputeHash(this HashType hashType, Stream inputStream)
 {
     return hashType.ComputeHash(inputStream, false);
 }
		public static string ComputeHashString(this IHashAlgorithm hashAlgorithm, string value)
		{
			return Encoding.Default.GetString(hashAlgorithm.ComputeHash(value));
		}
		public static string ComputeHashBase64String(this IHashAlgorithm hashAlgorithm, string value)
		{
			return Convert.ToBase64String(hashAlgorithm.ComputeHash(value));
		}
		public static byte[] ComputeHash(this IHashAlgorithm hashAlgorithm, string value)
		{
			var hash = hashAlgorithm.ComputeHash(Encoding.Default.GetBytes(value));

			return hash;
		}
		public static string ComputeHex(this IHashAlgorithm hashAlgorithm, Stream stream)
		{
			var hash = hashAlgorithm.ComputeHash(stream);

			return ComputeHex(hash);
		}
		public static string ComputeHex(this IHashAlgorithm hashAlgorithm, byte[] buffer, int offset, int count)
		{
			var hash = hashAlgorithm.ComputeHash(buffer, offset, count);

			return hash.ComputeHex();
		}
		public static string ComputeHex(this IHashAlgorithm hashAlgorithm, byte[] buffer)
		{
			var hash = hashAlgorithm.ComputeHash(buffer);

			return hash.ComputeHex();
		}
		public static string ComputeHexWithEncoding(this IHashAlgorithm hashAlgorithm, string value)
		{
			var hash = hashAlgorithm.ComputeHash(Encoding.Default.GetBytes(value));

			return hash.ComputeHex();
		}