sha1 -> sha256

master
Inga 🏳‍🌈 4 years ago
parent 243d9d5977
commit 6bb97daae6
  1. 4
      EternalArrowBackup.sln
  2. 0
      Source/Hasher/Sha256/EternalArrowBackup.Hasher.Sha256.csproj
  3. 8
      Source/Hasher/Sha256/Sha256ContentHasher.cs
  4. 4
      Tests/ContentTransformer/ClearText/EncryptorAndHasherTests.cs
  5. 2
      Tests/ContentTransformer/ClearText/EternalArrowBackup.ContentTransformer.ClearText.Tests.csproj
  6. 2
      Tests/Hasher/Sha256/EternalArrowBackup.Hasher.Sha256.Tests.csproj
  7. 20
      Tests/Hasher/Sha256/HasherTests.cs

@ -7,11 +7,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Source", "Source", "{A79A48
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{884B8E01-303A-40CF-8884-D62115F98683}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{884B8E01-303A-40CF-8884-D62115F98683}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EternalArrowBackup.Hasher.SHA1", "Source\Hasher\SHA1\EternalArrowBackup.Hasher.SHA1.csproj", "{623151DA-A4E0-4A7F-B600-A51ABCDE7038}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EternalArrowBackup.Hasher.Sha256", "Source\Hasher\Sha256\EternalArrowBackup.Hasher.Sha256.csproj", "{623151DA-A4E0-4A7F-B600-A51ABCDE7038}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EternalArrowBackup.ContentTransformer.ClearText", "Source\ContentTransformer\ClearText\EternalArrowBackup.ContentTransformer.ClearText.csproj", "{E8D0BBF5-8DA6-49FA-AC86-9F766C203D99}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EternalArrowBackup.ContentTransformer.ClearText", "Source\ContentTransformer\ClearText\EternalArrowBackup.ContentTransformer.ClearText.csproj", "{E8D0BBF5-8DA6-49FA-AC86-9F766C203D99}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EternalArrowBackup.Hasher.SHA1.Tests", "Tests\Hasher\SHA1\EternalArrowBackup.Hasher.SHA1.Tests.csproj", "{92AF4CAA-64AF-4A43-A637-CD10FFEB2FEC}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EternalArrowBackup.Hasher.Sha256.Tests", "Tests\Hasher\Sha256\EternalArrowBackup.Hasher.Sha256.Tests.csproj", "{92AF4CAA-64AF-4A43-A637-CD10FFEB2FEC}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EternalArrowBackup.ContentTransformer.ClearText.Tests", "Tests\ContentTransformer\ClearText\EternalArrowBackup.ContentTransformer.ClearText.Tests.csproj", "{0F91FC45-54B3-4BC0-BB3D-83E963EBD00D}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EternalArrowBackup.ContentTransformer.ClearText.Tests", "Tests\ContentTransformer\ClearText\EternalArrowBackup.ContentTransformer.ClearText.Tests.csproj", "{0F91FC45-54B3-4BC0-BB3D-83E963EBD00D}"
EndProject EndProject

@ -1,19 +1,19 @@
namespace EternalArrowBackup.Hasher.SHA1 namespace EternalArrowBackup.Hasher.Sha256
{ {
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using EternalArrowBackup.Hasher.Contracts; using EternalArrowBackup.Hasher.Contracts;
public class Sha1ContentHasher : IContentHasher public class Sha256ContentHasher : IContentHasher
{ {
public Task<string> ComputeHash(Stream content) public Task<string> ComputeHash(Stream content)
{ {
return Task.Run(() => return Task.Run(() =>
{ {
using (var sha1 = System.Security.Cryptography.SHA1.Create()) using (var sha256 = System.Security.Cryptography.SHA256.Create())
{ {
var hash = sha1.ComputeHash(content); var hash = sha256.ComputeHash(content);
return string.Concat(hash.Select(b => b.ToString("x2"))); return string.Concat(hash.Select(b => b.ToString("x2")));
} }
}); });

@ -2,7 +2,7 @@
{ {
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using EternalArrowBackup.Hasher.SHA1; using EternalArrowBackup.Hasher.Sha256;
using Xunit; using Xunit;
public static class EncryptorAndHasherTests public static class EncryptorAndHasherTests
@ -15,7 +15,7 @@
public static async Task TestCorrectDecryption(string message) public static async Task TestCorrectDecryption(string message)
{ {
var messageBytes = Encoding.ASCII.GetBytes(message); var messageBytes = Encoding.ASCII.GetBytes(message);
var hasher = new Sha1ContentHasher(); var hasher = new Sha256ContentHasher();
var encryptor = new ClearTextContentEncryptor(hasher); var encryptor = new ClearTextContentEncryptor(hasher);
var encrypted = await encryptor.TransformData(messageBytes); var encrypted = await encryptor.TransformData(messageBytes);

@ -13,7 +13,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\Source\ContentTransformer\ClearText\EternalArrowBackup.ContentTransformer.ClearText.csproj" /> <ProjectReference Include="..\..\..\Source\ContentTransformer\ClearText\EternalArrowBackup.ContentTransformer.ClearText.csproj" />
<ProjectReference Include="..\..\..\Source\Hasher\Contracts\EternalArrowBackup.Hasher.Contracts.csproj" /> <ProjectReference Include="..\..\..\Source\Hasher\Contracts\EternalArrowBackup.Hasher.Contracts.csproj" />
<ProjectReference Include="..\..\..\Source\Hasher\SHA1\EternalArrowBackup.Hasher.SHA1.csproj" /> <ProjectReference Include="..\..\..\Source\Hasher\Sha256\EternalArrowBackup.Hasher.Sha256.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -11,7 +11,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\Source\Hasher\SHA1\EternalArrowBackup.Hasher.SHA1.csproj" /> <ProjectReference Include="..\..\..\Source\Hasher\Sha256\EternalArrowBackup.Hasher.Sha256.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -1,4 +1,4 @@
namespace EternalArrowBackup.Hasher.SHA1 namespace EternalArrowBackup.Hasher.Sha256
{ {
using System; using System;
using System.Diagnostics; using System.Diagnostics;
@ -9,19 +9,20 @@
public static class HasherTests public static class HasherTests
{ {
private const int MegabytesPerSecondLowSpeed = 200; // Single-threaded SHA256 performance on modern CPUs is around 200-500MB/s
private const int MegabytesPerSecondHighSpeed = 1000; private const int MegabytesPerSecondLowSpeed = 20;
private const int MegabytesPerSecondHighSpeed = 5000;
// Test vectors taken from http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf // Test vectors taken from http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf
[Theory] [Theory]
[Trait("Category", "Simple")] [Trait("Category", "Simple")]
[InlineData("DA39A3EE 5E6B4B0D 3255BFEF 95601890 AFD80709", "")] [InlineData("E3B0C442 98FC1C14 9AFBF4C8 996FB924 27AE41E4 649B934C A495991B 7852B855", "")]
[InlineData("A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D", "abc")] [InlineData("BA7816BF 8F01CFEA 414140DE 5DAE2223 B00361A3 96177A9C B410FF61 F20015AD", "abc")]
[InlineData("84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")] [InlineData("248D6A61 D20638B8 E5C02693 0C3E6039 A33CE459 64FF2167 F6ECEDD4 19DB06C1", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")]
public static async Task TestHashes(string expectedHash, string inputMessage) public static async Task TestHashes(string expectedHash, string inputMessage)
{ {
var expectedHashFormatted = expectedHash.Replace(" ", string.Empty).ToLowerInvariant(); var expectedHashFormatted = expectedHash.Replace(" ", string.Empty).ToLowerInvariant();
var hasher = new Sha1ContentHasher(); var hasher = new Sha256ContentHasher();
var messageBytes = Encoding.ASCII.GetBytes(inputMessage); var messageBytes = Encoding.ASCII.GetBytes(inputMessage);
using (var stream = new MemoryStream(messageBytes)) using (var stream = new MemoryStream(messageBytes))
{ {
@ -30,7 +31,6 @@
} }
} }
// Single-threaded SHA1 performance on i5-6500 is around 500MB/s
[Theory] [Theory]
[Trait("Category", "Integration")] [Trait("Category", "Integration")]
[InlineData(0)] [InlineData(0)]
@ -45,13 +45,13 @@
var messageBytes = new byte[length]; var messageBytes = new byte[length];
for (var i = 0; i < length; i++) for (var i = 0; i < length; i++)
{ {
messageBytes[i] = (byte)((int)(Math.E * length + Math.PI * i) % 256); messageBytes[i] = (byte)((int)(Math.E * i * i + Math.PI * i) % 256);
} }
var expectedTimeLow = length / (MegabytesPerSecondHighSpeed * 1000); var expectedTimeLow = length / (MegabytesPerSecondHighSpeed * 1000);
var expectedTimeHigh = 1 + length / (MegabytesPerSecondLowSpeed * 1000); var expectedTimeHigh = 1 + length / (MegabytesPerSecondLowSpeed * 1000);
var hasher = new Sha1ContentHasher(); var hasher = new Sha256ContentHasher();
using (var stream = new MemoryStream(messageBytes)) using (var stream = new MemoryStream(messageBytes))
{ {
await hasher.ComputeHash(stream); await hasher.ComputeHash(stream);
Loading…
Cancel
Save