Skip to content

Commit d6947a3

Browse files
authored
Modernize SHA-256 samples (#33254)
1 parent 31570fa commit d6947a3

File tree

7 files changed

+43
-130
lines changed

7 files changed

+43
-130
lines changed

docs/standard/security/ensuring-data-integrity-with-hash-codes.md

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Ensuring Data Integrity with Hash Codes"
33
description: Learn how to ensure data integrity using hash codes in .NET. A hash value is a numeric value of a fixed length that uniquely identifies data.
4-
ms.date: 07/14/2020
4+
ms.date: 12/28/2022
55
dev_langs:
66
- "csharp"
77
- "vb"
@@ -23,36 +23,26 @@ This topic describes how to generate and verify hash codes by using the classes
2323

2424
## Generating a Hash
2525

26-
The managed hash classes can hash either an array of bytes or a managed stream object. The following example uses the SHA1 hash algorithm to create a hash value for a string. The example uses the <xref:System.Text.UnicodeEncoding> class to convert the string into an array of bytes that are hashed by using the <xref:System.Security.Cryptography.SHA256> class. The hash value is then displayed to the console.
26+
The hash classes can hash either an array of bytes or a stream object. The following example uses the SHA-256 hash algorithm to create a hash value for a string. The example uses <xref:System.Text.Encoding.UTF8?displayProperty=nameWithType> to convert the string into an array of bytes that are hashed by using the <xref:System.Security.Cryptography.SHA256> class. The hash value is then displayed to the console.
2727

2828
[!code-csharp[GeneratingAHash#1](../../../samples/snippets/csharp/VS_Snippets_CLR/generatingahash/cs/program.cs#1)]
2929
[!code-vb[GeneratingAHash#1](../../../samples/snippets/visualbasic/VS_Snippets_CLR/generatingahash/vb/program.vb#1)]
3030

3131
This code will display the following string to the console:
32-
33-
`185 203 236 22 3 228 27 130 87 23 244 15 87 88 14 43 37 61 106 224 81 172 224 211 104 85 194 197 194 25 120 217`
32+
33+
```console
34+
67A1790DCA55B8803AD024EE28F616A284DF5DD7B8BA5F68B4B252A5E925AF79
35+
```
3436

3537
## Verifying a Hash
3638

3739
Data can be compared to a hash value to determine its integrity. Usually, data is hashed at a certain time and the hash value is protected in some way. At a later time, the data can be hashed again and compared to the protected value. If the hash values match, the data has not been altered. If the values do not match, the data has been corrupted. For this system to work, the protected hash must be encrypted or kept secret from all untrusted parties.
3840

39-
The following example compares the previous hash value of a string to a new hash value. This example loops through each byte of the hash values and makes a comparison.
41+
The following example compares the previous hash value of a string to a new hash value.
4042

4143
[!code-csharp[VerifyingAHash#1](../../../samples/snippets/csharp/VS_Snippets_CLR/verifyingahash/cs/program.cs#1)]
4244
[!code-vb[VerifyingAHash#1](../../../samples/snippets/visualbasic/VS_Snippets_CLR/verifyingahash/vb/program.vb#1)]
4345

44-
If the two hash values match, this code displays the following to the console:
45-
46-
```console
47-
The hash codes match.
48-
```
49-
50-
If they do not match, the code displays the following:
51-
52-
```console
53-
The hash codes do not match.
54-
```
55-
5646
## See also
5747

5848
- [Cryptography Model](cryptography-model.md)

samples/snippets/csharp/VS_Snippets_CLR/generatingahash/cs/generatingahash.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,4 @@
55
<TargetFramework>net6.0</TargetFramework>
66
</PropertyGroup>
77

8-
<ItemGroup>
9-
<PackageReference Include="System.Security.Cryptography.Xml" Version="7.0.0" />
10-
</ItemGroup>
11-
128
</Project>

samples/snippets/csharp/VS_Snippets_CLR/generatingahash/cs/program.cs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,14 @@
44
using System.Security.Cryptography;
55
using System.Text;
66

7-
class Class1
8-
{
9-
static void Main(string[] args)
10-
{
11-
byte[] hashValue;
7+
string messageString = "This is the original message!";
128

13-
string messageString = "This is the original message!";
9+
//Convert the string into an array of bytes.
10+
byte[] messageBytes = Encoding.UTF8.GetBytes(messageString);
1411

15-
//Create a new instance of the UnicodeEncoding class to
16-
//convert the string into an array of Unicode bytes.
17-
UnicodeEncoding ue = new UnicodeEncoding();
12+
//Create the hash value from the array of bytes.
13+
byte[] hashValue = SHA256.HashData(messageBytes);
1814

19-
//Convert the string into an array of bytes.
20-
byte[] messageBytes = ue.GetBytes(messageString);
21-
22-
//Create a new instance of the SHA256 class to create
23-
//the hash value.
24-
SHA256 shHash = SHA256.Create();
25-
26-
//Create the hash value from the array of bytes.
27-
hashValue = shHash.ComputeHash(messageBytes);
28-
29-
//Display the hash value to the console.
30-
foreach (byte b in hashValue)
31-
{
32-
Console.Write("{0} ", b);
33-
}
34-
}
35-
}
15+
//Display the hash value to the console.
16+
Console.WriteLine(Convert.ToHexString(hashValue));
3617
//</Snippet1>
Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,32 @@
11
//<Snippet1>
22
using System;
3+
using System.Linq;
34
using System.Security.Cryptography;
45
using System.Text;
56

6-
class Class1
7-
{
8-
static void Main()
9-
{
10-
//This hash value is produced from "This is the original message!"
11-
//using SHA256.
12-
byte[] sentHashValue = { 185, 203, 236, 22, 3, 228, 27, 130, 87, 23, 244, 15, 87, 88, 14, 43, 37, 61, 106, 224, 81, 172, 224, 211, 104, 85, 194, 197, 194, 25, 120, 217 };
13-
14-
//This is the string that corresponds to the previous hash value.
15-
string messageString = "This is the original message!";
16-
17-
byte[] compareHashValue;
18-
19-
//Create a new instance of the UnicodeEncoding class to
20-
//convert the string into an array of Unicode bytes.
21-
UnicodeEncoding ue = new UnicodeEncoding();
7+
//This hash value is produced from "This is the original message!"
8+
//using SHA256.
9+
byte[] sentHashValue = Convert.FromHexString("67A1790DCA55B8803AD024EE28F616A284DF5DD7B8BA5F68B4B252A5E925AF79");
2210

23-
//Convert the string into an array of bytes.
24-
byte[] messageBytes = ue.GetBytes(messageString);
11+
//This is the string that corresponds to the previous hash value.
12+
string messageString = "This is the original message!";
2513

26-
//Create a new instance of the SHA256 class to create
27-
//the hash value.
28-
SHA256 shHash = SHA256.Create();
14+
//Convert the string into an array of bytes.
15+
byte[] messageBytes = Encoding.UTF8.GetBytes(messageString);
2916

30-
//Create the hash value from the array of bytes.
31-
compareHashValue = shHash.ComputeHash(messageBytes);
17+
//Create the hash value from the array of bytes.
18+
byte[] compareHashValue = SHA256.HashData(messageBytes);
3219

33-
bool same = true;
20+
//Compare the values of the two byte arrays.
21+
bool same = sentHashValue.SequenceEqual(compareHashValue);
3422

35-
//Compare the values of the two byte arrays.
36-
for (int x = 0; x < sentHashValue.Length; x++)
37-
{
38-
if (sentHashValue[x] != compareHashValue[x])
39-
{
40-
same = false;
41-
}
42-
}
43-
//Display whether or not the hash values are the same.
44-
if (same)
45-
{
46-
Console.WriteLine("The hash codes match.");
47-
}
48-
else
49-
{
50-
Console.WriteLine("The hash codes do not match.");
51-
}
52-
}
23+
//Display whether or not the hash values are the same.
24+
if (same)
25+
{
26+
Console.WriteLine("The hash codes match.");
27+
}
28+
else
29+
{
30+
Console.WriteLine("The hash codes do not match.");
5331
}
5432
//</Snippet1>

samples/snippets/visualbasic/VS_Snippets_CLR/generatingahash/generatingahash.vbproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,4 @@
55
<TargetFramework>net6.0</TargetFramework>
66
</PropertyGroup>
77

8-
<ItemGroup>
9-
<PackageReference Include="System.Security.Cryptography.Xml" Version="7.0.0" />
10-
</ItemGroup>
11-
128
</Project>

samples/snippets/visualbasic/VS_Snippets_CLR/generatingahash/vb/program.vb

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,16 @@ Imports System.Text
44

55
Module Program
66
Sub Main()
7-
Dim hashValue() As Byte
8-
97
Dim messageString As String = "This is the original message!"
108

11-
'Create a new instance of the UnicodeEncoding class to
12-
'convert the string into an array of Unicode bytes.
13-
Dim ue As New UnicodeEncoding()
14-
159
'Convert the string into an array of bytes.
16-
Dim messageBytes As Byte() = ue.GetBytes(messageString)
17-
18-
'Create a new instance of the SHA256 class to create
19-
'the hash value.
20-
Dim shHash As SHA256 = SHA256.Create()
10+
Dim messageBytes As Byte() = Encoding.UTF8.GetBytes(messageString)
2111

2212
'Create the hash value from the array of bytes.
23-
hashValue = shHash.ComputeHash(messageBytes)
13+
Dim hashValue As Byte() = SHA256.HashData(messageBytes)
2414

2515
'Display the hash value to the console.
26-
Dim b As Byte
27-
For Each b In hashValue
28-
Console.Write("{0} ", b)
29-
Next b
16+
Console.WriteLine(Convert.ToHexString(hashValue))
3017
End Sub
3118
End Module
3219
'</Snippet1>

samples/snippets/visualbasic/VS_Snippets_CLR/verifyingahash/vb/program.vb

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,26 @@
11
'<Snippet1>
2+
Imports System.Linq
23
Imports System.Security.Cryptography
34
Imports System.Text
45

56
Module Module1
67
Sub Main()
78
'This hash value is produced from "This is the original message!"
89
'using SHA256.
9-
Dim sentHashValue As Byte() = {185, 203, 236, 22, 3, 228, 27, 130, 87, 23, 244, 15, 87, 88, 14, 43, 37, 61, 106, 224, 81, 172, 224, 211, 104, 85, 194, 197, 194, 25, 120, 217}
10+
Dim sentHashValue As Byte() = Convert.FromHexString("67A1790DCA55B8803AD024EE28F616A284DF5DD7B8BA5F68B4B252A5E925AF79")
1011

1112
'This is the string that corresponds to the previous hash value.
1213
Dim messageString As String = "This is the original message!"
1314

14-
Dim compareHashValue() As Byte
15-
16-
'Create a new instance of the UnicodeEncoding class to
17-
'convert the string into an array of Unicode bytes.
18-
Dim ue As New UnicodeEncoding()
19-
2015
'Convert the string into an array of bytes.
21-
Dim messageBytes As Byte() = ue.GetBytes(messageString)
22-
23-
'Create a new instance of the SHA1Managed class to create
24-
'the hash value.
25-
Dim shHash As SHA256 = SHA256.Create()
16+
Dim messageBytes As Byte() = Encoding.UTF8.GetBytes(messageString)
2617

2718
'Create the hash value from the array of bytes.
28-
compareHashValue = shHash.ComputeHash(messageBytes)
29-
30-
Dim same As Boolean = True
19+
Dim compareHashValue As Byte() = SHA256.HashData(messageBytes)
3120

3221
'Compare the values of the two byte arrays.
33-
Dim x As Integer
34-
For x = 0 To sentHashValue.Length - 1
35-
If sentHashValue(x) <> compareHashValue(x) Then
36-
same = False
37-
End If
38-
Next x
22+
Dim same As Boolean = sentHashValue.SequenceEqual(compareHashValue)
23+
3924
'Display whether or not the hash values are the same.
4025
If same Then
4126
Console.WriteLine("The hash codes match.")

0 commit comments

Comments
 (0)