HashAlgorithm.ComputeHash Method (System.Security.Cryptography) (2024)

Table of Contents
Examples Applies to
Source:
HashAlgorithm.cs
Source:
HashAlgorithm.cs
Source:
HashAlgorithm.cs

Computes the hash value for the specified byte array.

public: cli::array <System::Byte> ^ ComputeHash(cli::array <System::Byte> ^ buffer);
public byte[] ComputeHash (byte[] buffer);
member this.ComputeHash : byte[] -> byte[]
Public Function ComputeHash (buffer As Byte()) As Byte()

Parameters

Returns

The computed hash code.

Exceptions

The object has already been disposed.

Examples

The following example computes the SHA256 hash value of a string and returns the hash as a 64-character, hexadecimal-formatted string. The hash string created by this code example is compatible with any SHA256 hash function (on any platform) that creates a 64-character, hexadecimal-formatted hash string.

using System;using System.Security.Cryptography;using System.Text;public class Program{ public static void Main() { string source = "Hello World!"; using (SHA256 sha256Hash = SHA256.Create()) { string hash = GetHash(sha256Hash, source); Console.WriteLine($"The SHA256 hash of {source} is: {hash}."); Console.WriteLine("Verifying the hash..."); if (VerifyHash(sha256Hash, source, hash)) { Console.WriteLine("The hashes are the same."); } else { Console.WriteLine("The hashes are not same."); } } } private static string GetHash(HashAlgorithm hashAlgorithm, string input) { // Convert the input string to a byte array and compute the hash. byte[] data = hashAlgorithm.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. for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } // Return the hexadecimal string. return sBuilder.ToString(); } // Verify a hash against a string. private static bool VerifyHash(HashAlgorithm hashAlgorithm, string input, string hash) { // Hash the input. var hashOfInput = GetHash(hashAlgorithm, input); // Create a StringComparer an compare the hashes. StringComparer comparer = StringComparer.OrdinalIgnoreCase; return comparer.Compare(hashOfInput, hash) == 0; }}// The example displays the following output:// The SHA256 hash of Hello World! is: 7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069.// Verifying the hash...// The hashes are the same.
Imports System.Security.CryptographyImports System.TextPublic Module Program Public Sub Main() Dim source As String = "Hello World!" Using sha256Hash As SHA256 = SHA256.Create() Dim hash As String = GetHash(sha256Hash, source) Console.WriteLine($"The SHA256 hash of {source} is: {hash}.") Console.WriteLine("Verifying the hash...") If VerifyHash(sha256Hash, source, hash) Then Console.WriteLine("The hashes are the same.") Else Console.WriteLine("The hashes are not same.") End If End Using End Sub Private Function GetHash(ByVal hashAlgorithm As HashAlgorithm, ByVal input As String) As String ' Convert the input string to a byte array and compute the hash. Dim data As Byte() = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(input)) ' Create a new Stringbuilder to collect the bytes ' and create a string. Dim sBuilder As New StringBuilder() ' Loop through each byte of the hashed data ' and format each one as a hexadecimal string. For i As Integer = 0 To data.Length - 1 sBuilder.Append(data(i).ToString("x2")) Next ' Return the hexadecimal string. Return sBuilder.ToString() End Function ' Verify a hash against a string. Private Function VerifyHash(hashAlgorithm As HashAlgorithm, input As String, hash As String) As Boolean ' Hash the input. Dim hashOfInput As String = GetHash(hashAlgorithm, input) ' Create a StringComparer an compare the hashes. Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase Return comparer.Compare(hashOfInput, hash) = 0 End FunctionEnd Module' The example displays the following output:' The SHA256 hash of Hello World! is: 7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069.' Verifying the hash...' The hashes are the same.

Applies to

HashAlgorithm.ComputeHash Method (System.Security.Cryptography) (2024)
Top Articles
6 Good Reasons to Eat a Banana Today
SNAP Forecast — Price Target — Prediction for 2025 — TradingView
Crete Il Forum
Golden Grain Pizza East Greenbush - Rensselaer
Brenda Song Wikifeet
Western Razor David Angelo Net Worth
Mercy Baggot Street Mypay
SUN WOOD ➠ the ultimate alternative to reclaimed wood & precious wood
Vados X Male Reader
Reston, VA | Reston Demographics in 2024
pittsburgh gigs - craigslist
Tmj4 Weather Milwaukee
Umn Pay Calendar
Imponte Phoenix
Walgreens On Osborn And Scottsdale
Hdmovieshub In
Puff Hall Road
Tampa Bay Times Mugshots
M3Gan Showtimes Near Regal City North
Dinar Guru Recaps Updates
Publix Super Market At Lockwood Commons
Marylynn Lee
Varsity Competition Results 2022
Fruit Of Enchantment Bdo
Christmas Days Away
385-437-2602
Cvs Minuteclinic Locations Near Me
Vermont Free Stuff Craigslist
Monster Hunter Rise Steam Unlocked
Standard Page Field
The Hub.fcagroup.com Login Page
_Ashleyjoyyy
According To The Wall Street Journal Weegy
Trumbull County Jail Commissary
How To Get The Unphased Badge In Slap Battles
Tristatehomepage Evansville
Two Number Lunchtime Today Come
Please Help Me: What to Do When You Need Help
Craigslist Kalispell Montana Personals
Yellow Sun Of Ecuador by The Classics
Tvlistings.com
Go Karts For Sale Near Me Used
B&B Auto Salvage Okc
Tyrones Unblocked Games Basketball Stars
Www.socialserve.com Ga
Steamunlocked Starfield
Soap Central Message Boards Bold And Beautiful
Kristenhart Cam
Sweaty Gta Names Tryhard Gamertags
Busted Mugshots Buena Vista Va
Zuercher Portal Marion County Wv
Metro Pcs Locations Near Me
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 6004

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.