-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCryptoUtilsTests.cs
More file actions
68 lines (56 loc) · 2.27 KB
/
Copy pathCryptoUtilsTests.cs
File metadata and controls
68 lines (56 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Security.Cryptography;
using System.Text;
using NUnit.Framework;
namespace Gaev.Blog;
public class CryptoUtilsTests
{
public string ComputeSha256(MemoryStream stream)
{
using var sha = SHA256.Create();
return string.Join("", sha.ComputeHash(stream).Select(b => b.ToString("x2")));
}
public string ComputeSha256Fixed(MemoryStream stream)
{
stream.Seek(0, SeekOrigin.Begin);
using var sha = SHA256.Create();
return string.Join("", sha.ComputeHash(stream).Select(b => b.ToString("x2")));
}
[TestCase("John Doe", "6cea57c2fb6cbc2a40411135005760f241fffc3e5e67ab99882726431037f908")]
[TestCase("C# developer", "c9298659b4622ec5881c09fc510f23fcfbe75159d13f64b388b74c4d060d65d7")]
public void It_should_compute_SHA256_for_stream(string payload, string expected)
{
// Given
var binary = Encoding.UTF8.GetBytes(payload);
var stream = new MemoryStream(binary);
// When
var actual = ComputeSha256(stream);
// Then
Assert.That(actual, Is.EqualTo(expected));
}
[TestCase("John Doe", "6cea57c2fb6cbc2a40411135005760f241fffc3e5e67ab99882726431037f908")]
[TestCase("C# developer", "c9298659b4622ec5881c09fc510f23fcfbe75159d13f64b388b74c4d060d65d7")]
public void It_should_compute_SHA256_for_stream_broken(string payload, string expected)
{
// Given
var binary = Encoding.UTF8.GetBytes(payload);
var stream = new MemoryStream();
stream.Write(binary);
// When
var actual = ComputeSha256(stream);
// Then
Assert.That(actual, Is.EqualTo(expected));
}
[TestCase("John Doe", "6cea57c2fb6cbc2a40411135005760f241fffc3e5e67ab99882726431037f908")]
[TestCase("C# developer", "c9298659b4622ec5881c09fc510f23fcfbe75159d13f64b388b74c4d060d65d7")]
public void It_should_compute_SHA256_for_stream_fixed(string payload, string expected)
{
// Given
var binary = Encoding.UTF8.GetBytes(payload);
var stream = new MemoryStream();
stream.Write(binary);
// When
var actual = ComputeSha256Fixed(stream);
// Then
Assert.That(actual, Is.EqualTo(expected));
}
}