Computes hash algorithm for the source string - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:Hash

Description

Computes hash algorithm for the source string

Demo Code


using Windows.Storage.Streams;
using Windows.Security.Cryptography.Core;
using Windows.Security.Cryptography;
using System.Text;

public class Main{
        /// <summary>
        /// Computes hash algorithm for the source string
        /// </summary>
        /// <param name="source">Source string to compute hash from</param>
        /// <param name="algorithm">HashAlgorithmNames.Sha1</param>
        /// <returns>hash from the source string</returns>
        public static string ComputeHash(string source, string algorithm)
        {//from w  w w.  ja v a2s.co  m
            HashAlgorithmProvider sha1 = HashAlgorithmProvider.OpenAlgorithm(algorithm);
            byte[] bytes = Encoding.UTF8.GetBytes(source);
            IBuffer bytesBuffer = CryptographicBuffer.CreateFromByteArray(bytes);
            IBuffer hashBuffer = sha1.HashData(bytesBuffer);
            return CryptographicBuffer.EncodeToHexString(hashBuffer);
        }
}

Related Tutorials