working on it ...

Filters

Explore Public Snippets

Sort by

Found 618 snippets matching: md5

    public by cghersi  290431  1  6  0

    How to simply compute the MD5 hash of a given string, being sure to have the same results retrieved by HASH method of MS SQL Server

    Returns the MD5 hash of the given string
    public static String computeMD5Hash(String input) {
    	if (isNullOrEmpty(input))
    		return "";
    	byte[] bytesOfMessage = null;
    	try {
    		bytesOfMessage = input.getBytes("UTF-16LE");
    	} catch (UnsupportedEncodingException e) {
    		log.error("Cannot support UTF-8 encoding due to " + e, e);
    		return "";
    	}
    	MessageDigest md = null;
    	try {
    		md = MessageDigest.getInstance("MD5");
    	} catch (NoSuchAlgorithmException e) {
    		log.error("Cannot support MD5 algorithm due to " + e, e);
    		return "";
    	}
    	byte[] thedigest = md.digest(bytesOfMessage);
    	return printHexByteArray(thedigest);
    }

    public by snip2code  22968  23  7  9

    Compute MD5 Hash in C#

    Returns an hash (computed with MD5 algorithm) of the given string. Note: this is compliant with the results given by HashBytes('MD5', @text) method of SQL Server.
    public static string ComputeMD5HashSQLServerCompliant(this string password)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] binaryInput = Encoding.Unicode.GetBytes(password);
        byte[] hashOfInput = md5.ComputeHash(binaryInput);
        return hashOfInput.PrintHexByteArray();
    }
    
    

    public by cghersi  10357  75  9  0

    How to encrypt and decrypt strings in C#

    This class can be used to encrypt and decrypt the given string. In the current implementation the passwrod is directly saved inside the code; depending on the infrastructure and architecture of the code sometimes it can be passed as parameter
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Security.Cryptography;
    
    public static class Encryptor
    {
        static private string _key = "myVeryStrongPsw";
    
        /// <summary>
        /// Encrypt the given string using the default key.
        /// </summary>
        /// <param name="strToEncrypt">The string to be encrypted.</param>
        /// <returns>The encrypted string.</returns>
        public static string Encrypt(string strToEncrypt)
        {
            try
            {
                return Encrypt(strToEncrypt, _key);
            }
    
            catch (Exception ex)
            {
                return "Wrong Input. " + ex.Message;
            }
        }
    
        /// <summary>
        /// Decrypt the given string using the default key.
        /// </summary>
        /// <param name="strEncrypted">The string to be decrypted.</param>
        /// <returns>The decrypted string.</returns>
        public static string Decrypt(string strEncrypted)
        {
            try
            {
                return Decrypt(strEncrypted, _key);
            }
            catch (Exception ex)
            {
                return "Wrong Input. " + ex.Message;
            }
        }
    
        /// <summary>
        /// Encrypt the given string using the specified key.
        /// </summary>
        /// <param name="strToEncrypt">The string to be encrypted.</param>
        /// <param name="strKey">The encryption key.</param>
        /// <returns>The encrypted string.</returns>
        public static string Encrypt(string strToEncrypt, string strKey)
        {
            try
            {
                TripleDESCryptoServiceProvider objDESCrypto =
                    new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
                byte[] byteHash, byteBuff;
                string strTempKey = strKey;
                byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
                objHashMD5 = null;
                objDESCrypto.Key = byteHash;
                objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
                byteBuff = ASCIIEncoding.ASCII.GetBytes(strToEncrypt);
                return Convert.ToBase64String(objDESCrypto.CreateEncryptor().
                    TransformFinalBlock(byteBuff, 0, byteBuff.Length));
            }
            catch (Exception ex)
            {
                return "Wrong Input. " + ex.Message;
            }
        }
    
        /// <summary>
        /// Decrypt the given string using the specified key.
        /// </summary>
        /// <param name="strEncrypted">The string to be decrypted.</param>
        /// <param name="strKey">The decryption key.</param>
        /// <returns>The decrypted string.</returns>
        public static string Decrypt(string strEncrypted, string strKey)
        {
            try
            {
                TripleDESCryptoServiceProvider objDESCrypto =
                    new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
                byte[] byteHash, byteBuff;
                string strTempKey = strKey;
                byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
                objHashMD5 = null;
                objDESCrypto.Key = byteHash;
                objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
                byteBuff = Convert.FromBase64String(strEncrypted);
                string strDecrypted = ASCIIEncoding.ASCII.GetString
                (objDESCrypto.CreateDecryptor().TransformFinalBlock
                (byteBuff, 0, byteBuff.Length));
                objDESCrypto = null;
                return strDecrypted;
            }
            catch (Exception ex)
            {
                return "Wrong Input. " + ex.Message;
            }
        }
    }
    

    public by msdn  1295  0  5  0

    MD5: Encodes a string into MD5 encryption

    Encodes a string into MD5 encryption String to encode MD5 version of the string
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Practices.EnterpriseLibrary.Logging;
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
    using Soat.AdventureWorks.ServerEntities;
    using System.Security.Cryptography;
    using Soat.AdventureWorks.DataContract.Common;
    
    /// <summary>
    /// Encodes a string into MD5 encryption
    /// </summary>
    /// <param name="password">String to encode</param>
    /// <returns>MD5 version of the string</returns>
    private static string MD5(string password)
    {
        byte[] textBytes = System.Text.Encoding.Default.GetBytes(password);
        try
        {
            System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
            cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hash = cryptHandler.ComputeHash(textBytes);
            string ret = "";
            foreach (byte a in hash)
            {
                if (a < 16)
                    ret += "0" + a.ToString("x");
                else
                    ret += a.ToString("x");
            }
            return ret;
        }
        catch
        {
            throw;
        }
    }

    public by msdn  1371  0  5  0

    GenerateBlockIDWithHash: Generates an block id using the given MD5 hash value. The id must be Base64 compatible.

    Generates an block id using the given MD5 hash value. The id must be Base64 compatible. The hash value to encode in the block id. The sequence prefix value. The block id.
    /// <summary>
    /// Generates an block id using the given MD5 hash value. The id must be Base64 compatible.
    /// </summary>
    /// <param name="hashVal">The hash value to encode in the block id.</param>
    /// <param name="seqNo">The sequence prefix value.</param>
    /// <returns>The block id.</returns>
    internal static string GenerateBlockIDWithHash(string hashVal, long seqNo)
    {
        byte[] tempArray = new byte[6];
    
        for (int m = 0; m < 6; m++)
        {
            tempArray[5 - m] = (byte)((seqNo >> (8 * m)) & 0xFF);
        }
    
        Convert.ToBase64String(tempArray);
                
        return String.Format(Protocol.Constants.V2MD5blockIdFormat, Convert.ToBase64String(tempArray), hashVal);                  
    }

    public by msdn  1371  0  6  0

    GetMD5Hash: Gets the MD5 hash of a string.

    Gets the MD5 hash of a string. The input. The MD5 hash of a string
    using System;
    using System.Globalization;
    using System.IO;
    using System.Security.Cryptography;
    using System.ServiceModel;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Imaging;
    using FeedProcessor;
    using FeedProcessor.Enums;
    using LiveStream.MicrosoftTagService;
    using LiveStream.Properties;
    
    /// <summary>
    /// Gets the MD5 hash of a string.
    /// </summary>
    /// <param name="input">The input.</param>
    /// <returns>The MD5 hash of a string</returns>
    private static string GetMD5Hash(string input)
    {
        using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
        {
            byte[] bytes = Encoding.UTF8.GetBytes(input);
            bytes = md5.ComputeHash(bytes);
            StringBuilder stringBuilder = new StringBuilder();
            foreach (byte b in bytes)
            {
                stringBuilder.Append(b.ToString("x2", CultureInfo.InvariantCulture).ToUpperInvariant());
            }
    
            return stringBuilder.ToString();
        }
    }

    public by msdn  1325  0  6  0

    ComputeMD5: Compute the MD5 hash of the stream.

    Compute the MD5 hash of the stream. The stream. A delegate for setting the resulting MD5 hash as a string. The sequence that when invoked results in an asynchronous MD5 computation.
    /// <summary>
    /// Buffer size needs to be page-aligned.
    /// </summary>
    private const int BufferSize = 64 * 1024;
    
    /// <summary>
    /// Compute the MD5 hash of the stream.
    /// </summary>
    /// <param name="stream">The stream.</param>
    /// <param name="setResult">A delegate for setting the resulting MD5 hash as a string.</param>
    /// <returns>The sequence that when invoked results in an asynchronous MD5 computation.</returns>
    [DebuggerNonUserCode]
    internal static TaskSequence ComputeMD5(this Stream stream, Action<string> setResult)
    {
        int readCount;
        MD5 md5 = MD5.Create();
        do
        {
            byte[] buffer = new byte[BufferSize];
            var readTask = stream.ReadAsync(buffer, 0, buffer.Length);
            yield return readTask;
            readCount = readTask.Result;
    
            StreamUtilities.ComputeHash(buffer, 0, readCount, md5);
        }
        while (readCount != 0);
    
        setResult(StreamUtilities.GetHashValue(md5));
    }

    public by msdn  1201  0  5  0

    ExtractMD5ValueFromBlockID: Extracts an MD5 value from either an v1 or v2 block id.

    Extracts an MD5 value from either an v1 or v2 block id. The block id containing an MD5. MD5 value, or null if invalid format.
    /// <summary>
    /// Extracts an MD5 value from either an v1 or v2 block id.
    /// </summary>
    /// <param name="blockId">The block id containing an MD5.</param>
    /// <returns>MD5 value, or null if invalid format.</returns>
    internal static string ExtractMD5ValueFromBlockID(string blockId)
    {
        if (blockId == null)
        {
            return null;
        }
    
        if (blockId.Length == Constants.V2MD5blockIdExpectedLength && blockId.StartsWith(Constants.V2blockPrefix))
        {
            return blockId.Substring(Constants.V2MD5blockIdMD5Offset);
        }
        else if (blockId.Length == Constants.V1MD5blockIdExpectedLength && blockId.StartsWith(Constants.V1BlockPrefix))
        {
            return blockId.Substring(Constants.V1BlockPrefix.Length);
        }
    
        return null;
    }

    public by msdn  2102  0  6  0

    GetHashBlock

    // Performs a single block transform of MD5 for a given set of ABCD inputs /* If implementing your own hashing framework, be sure to set the initial ABCD correctly according to RFC 1321: // A = 0x67452301; // B = 0xefcdab89; // C = 0x98badcfe; // D = 0x10325476; */
    using System;
    using System.Text;
    
    // Performs a single block transform of MD5 for a given set of ABCD inputs
    /* If implementing your own hashing framework, be sure to set the initial ABCD correctly according to RFC 1321:
    //    A = 0x67452301;
    //    B = 0xefcdab89;
    //    C = 0x98badcfe;
    //    D = 0x10325476;
    */
    internal static void GetHashBlock(byte[] input, ref ABCDStruct ABCDValue, int ibStart)
    {
        uint[] temp = Converter(input, ibStart);
        uint a = ABCDValue.A; 
        uint b = ABCDValue.B;
        uint c = ABCDValue.C;
        uint d = ABCDValue.D;
    
        a = r1(a, b, c, d, temp[0 ], 7,  0xd76aa478);
        d = r1(d, a, b, c, temp[1 ], 12, 0xe8c7b756);
        c = r1(c, d, a, b, temp[2 ], 17, 0x242070db);
        b = r1(b, c, d, a, temp[3 ], 22, 0xc1bdceee);
        a = r1(a, b, c, d, temp[4 ], 7,  0xf57c0faf);
        d = r1(d, a, b, c, temp[5 ], 12, 0x4787c62a);
        c = r1(c, d, a, b, temp[6 ], 17, 0xa8304613);
        b = r1(b, c, d, a, temp[7 ], 22, 0xfd469501);
        a = r1(a, b, c, d, temp[8 ], 7,  0x698098d8);
        d = r1(d, a, b, c, temp[9 ], 12, 0x8b44f7af);
        c = r1(c, d, a, b, temp[10], 17, 0xffff5bb1);
        b = r1(b, c, d, a, temp[11], 22, 0x895cd7be);
        a = r1(a, b, c, d, temp[12], 7,  0x6b901122);
        d = r1(d, a, b, c, temp[13], 12, 0xfd987193);
        c = r1(c, d, a, b, temp[14], 17, 0xa679438e);
        b = r1(b, c, d, a, temp[15], 22, 0x49b40821);
    
        a = r2(a, b, c, d, temp[1 ], 5,  0xf61e2562);
        d = r2(d, a, b, c, temp[6 ], 9,  0xc040b340);
        c = r2(c, d, a, b, temp[11], 14, 0x265e5a51);
        b = r2(b, c, d, a, temp[0 ], 20, 0xe9b6c7aa);
        a = r2(a, b, c, d, temp[5 ], 5,  0xd62f105d);
        d = r2(d, a, b, c, temp[10], 9,  0x02441453);
        c = r2(c, d, a, b, temp[15], 14, 0xd8a1e681);
        b = r2(b, c, d, a, temp[4 ], 20, 0xe7d3fbc8);
        a = r2(a, b, c, d, temp[9 ], 5,  0x21e1cde6);
        d = r2(d, a, b, c, temp[14], 9,  0xc33707d6);
        c = r2(c, d, a, b, temp[3 ], 14, 0xf4d50d87);
        b = r2(b, c, d, a, temp[8 ], 20, 0x455a14ed);
        a = r2(a, b, c, d, temp[13], 5,  0xa9e3e905);
        d = r2(d, a, b, c, temp[2 ], 9,  0xfcefa3f8);
        c = r2(c, d, a, b, temp[7 ], 14, 0x676f02d9);
        b = r2(b, c, d, a, temp[12], 20, 0x8d2a4c8a);
    
        a = r3(a, b, c, d, temp[5 ], 4,  0xfffa3942);
        d = r3(d, a, b, c, temp[8 ], 11, 0x8771f681);
        c = r3(c, d, a, b, temp[11], 16, 0x6d9d6122);
        b = r3(b, c, d, a, temp[14], 23, 0xfde5380c);
        a = r3(a, b, c, d, temp[1 ], 4,  0xa4beea44);
        d = r3(d, a, b, c, temp[4 ], 11, 0x4bdecfa9);
        c = r3(c, d, a, b, temp[7 ], 16, 0xf6bb4b60);
        b = r3(b, c, d, a, temp[10], 23, 0xbebfbc70);
        a = r3(a, b, c, d, temp[13], 4,  0x289b7ec6);
        d = r3(d, a, b, c, temp[0 ], 11, 0xeaa127fa);
        c = r3(c, d, a, b, temp[3 ], 16, 0xd4ef3085);
        b = r3(b, c, d, a, temp[6 ], 23, 0x04881d05);
        a = r3(a, b, c, d, temp[9 ], 4,  0xd9d4d039);
        d = r3(d, a, b, c, temp[12], 11, 0xe6db99e5);
        c = r3(c, d, a, b, temp[15], 16, 0x1fa27cf8);
        b = r3(b, c, d, a, temp[2 ], 23, 0xc4ac5665);
    
        a = r4(a, b, c, d, temp[0 ], 6,  0xf4292244);
        d = r4(d, a, b, c, temp[7 ], 10, 0x432aff97);
        c = r4(c, d, a, b, temp[14], 15, 0xab9423a7);
        b = r4(b, c, d, a, temp[5 ], 21, 0xfc93a039);
        a = r4(a, b, c, d, temp[12], 6,  0x655b59c3);
        d = r4(d, a, b, c, temp[3 ], 10, 0x8f0ccc92);
        c = r4(c, d, a, b, temp[10], 15, 0xffeff47d);
        b = r4(b, c, d, a, temp[1 ], 21, 0x85845dd1);
        a = r4(a, b, c, d, temp[8 ], 6,  0x6fa87e4f);
        d = r4(d, a, b, c, temp[15], 10, 0xfe2ce6e0);
        c = r4(c, d, a, b, temp[6 ], 15, 0xa3014314);
        b = r4(b, c, d, a, temp[13], 21, 0x4e0811a1);
        a = r4(a, b, c, d, temp[4 ], 6,  0xf7537e82);
        d = r4(d, a, b, c, temp[11], 10, 0xbd3af235);
        c = r4(c, d, a, b, temp[2 ], 15, 0x2ad7d2bb);
        b = r4(b, c, d, a, temp[9 ], 21, 0xeb86d391);
    
        ABCDValue.A = unchecked(a + ABCDValue.A);
        ABCDValue.B = unchecked(b + ABCDValue.B);
        ABCDValue.C = unchecked(c + ABCDValue.C);
        ABCDValue.D = unchecked(d + ABCDValue.D);
        return; 
    }

    public by msdn  1671  0  7  0

    HashEmailForGravatar

    Hashes an email with MD5. Suitable for use with Gravatar profile image urls
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Md5Test;
    
    /// Hashes an email with MD5.  Suitable for use with Gravatar profile
    /// image urls
    public static string HashEmailForGravatar(string email)
    {
        // Create a new instance of the MD5CryptoServiceProvider object.  
        var md5Hasher = MD5.Create();
    
        // Convert the input string to a byte array and compute the hash.  
        byte[] data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(email));
    
        // 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.  
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }
    
        return sBuilder.ToString();  // Return the hexadecimal string. 
    }
    • Public Snippets
    • Channels Snippets