From 6bb97daae64ce1a5d35773e2333678c2e7d004a2 Mon Sep 17 00:00:00 2001 From: Inga Lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Thu, 30 Jul 2020 22:49:04 +0300 Subject: [PATCH] sha1 -> sha256 --- EternalArrowBackup.sln | 4 ++-- .../EternalArrowBackup.Hasher.Sha256.csproj} | 0 .../Sha256ContentHasher.cs} | 8 ++++---- .../ClearText/EncryptorAndHasherTests.cs | 4 ++-- ....ContentTransformer.ClearText.Tests.csproj | 2 +- ...nalArrowBackup.Hasher.Sha256.Tests.csproj} | 2 +- Tests/Hasher/{SHA1 => Sha256}/HasherTests.cs | 20 +++++++++---------- 7 files changed, 20 insertions(+), 20 deletions(-) rename Source/Hasher/{SHA1/EternalArrowBackup.Hasher.SHA1.csproj => Sha256/EternalArrowBackup.Hasher.Sha256.csproj} (100%) rename Source/Hasher/{SHA1/SHA1ContentHasher.cs => Sha256/Sha256ContentHasher.cs} (62%) rename Tests/Hasher/{SHA1/EternalArrowBackup.Hasher.SHA1.Tests.csproj => Sha256/EternalArrowBackup.Hasher.Sha256.Tests.csproj} (81%) rename Tests/Hasher/{SHA1 => Sha256}/HasherTests.cs (70%) diff --git a/EternalArrowBackup.sln b/EternalArrowBackup.sln index 2e7ae11..cc1dc86 100644 --- a/EternalArrowBackup.sln +++ b/EternalArrowBackup.sln @@ -7,11 +7,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Source", "Source", "{A79A48 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{884B8E01-303A-40CF-8884-D62115F98683}" 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 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EternalArrowBackup.ContentTransformer.ClearText", "Source\ContentTransformer\ClearText\EternalArrowBackup.ContentTransformer.ClearText.csproj", "{E8D0BBF5-8DA6-49FA-AC86-9F766C203D99}" 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 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EternalArrowBackup.ContentTransformer.ClearText.Tests", "Tests\ContentTransformer\ClearText\EternalArrowBackup.ContentTransformer.ClearText.Tests.csproj", "{0F91FC45-54B3-4BC0-BB3D-83E963EBD00D}" EndProject diff --git a/Source/Hasher/SHA1/EternalArrowBackup.Hasher.SHA1.csproj b/Source/Hasher/Sha256/EternalArrowBackup.Hasher.Sha256.csproj similarity index 100% rename from Source/Hasher/SHA1/EternalArrowBackup.Hasher.SHA1.csproj rename to Source/Hasher/Sha256/EternalArrowBackup.Hasher.Sha256.csproj diff --git a/Source/Hasher/SHA1/SHA1ContentHasher.cs b/Source/Hasher/Sha256/Sha256ContentHasher.cs similarity index 62% rename from Source/Hasher/SHA1/SHA1ContentHasher.cs rename to Source/Hasher/Sha256/Sha256ContentHasher.cs index 14caacd..6bfb39f 100644 --- a/Source/Hasher/SHA1/SHA1ContentHasher.cs +++ b/Source/Hasher/Sha256/Sha256ContentHasher.cs @@ -1,19 +1,19 @@ -namespace EternalArrowBackup.Hasher.SHA1 +namespace EternalArrowBackup.Hasher.Sha256 { using System.IO; using System.Linq; using System.Threading.Tasks; using EternalArrowBackup.Hasher.Contracts; - public class Sha1ContentHasher : IContentHasher + public class Sha256ContentHasher : IContentHasher { public Task ComputeHash(Stream content) { 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"))); } }); diff --git a/Tests/ContentTransformer/ClearText/EncryptorAndHasherTests.cs b/Tests/ContentTransformer/ClearText/EncryptorAndHasherTests.cs index 5a2546e..b479d49 100644 --- a/Tests/ContentTransformer/ClearText/EncryptorAndHasherTests.cs +++ b/Tests/ContentTransformer/ClearText/EncryptorAndHasherTests.cs @@ -2,7 +2,7 @@ { using System.Text; using System.Threading.Tasks; - using EternalArrowBackup.Hasher.SHA1; + using EternalArrowBackup.Hasher.Sha256; using Xunit; public static class EncryptorAndHasherTests @@ -15,7 +15,7 @@ public static async Task TestCorrectDecryption(string message) { var messageBytes = Encoding.ASCII.GetBytes(message); - var hasher = new Sha1ContentHasher(); + var hasher = new Sha256ContentHasher(); var encryptor = new ClearTextContentEncryptor(hasher); var encrypted = await encryptor.TransformData(messageBytes); diff --git a/Tests/ContentTransformer/ClearText/EternalArrowBackup.ContentTransformer.ClearText.Tests.csproj b/Tests/ContentTransformer/ClearText/EternalArrowBackup.ContentTransformer.ClearText.Tests.csproj index 5e85b7c..b4f2241 100644 --- a/Tests/ContentTransformer/ClearText/EternalArrowBackup.ContentTransformer.ClearText.Tests.csproj +++ b/Tests/ContentTransformer/ClearText/EternalArrowBackup.ContentTransformer.ClearText.Tests.csproj @@ -13,7 +13,7 @@ - + diff --git a/Tests/Hasher/SHA1/EternalArrowBackup.Hasher.SHA1.Tests.csproj b/Tests/Hasher/Sha256/EternalArrowBackup.Hasher.Sha256.Tests.csproj similarity index 81% rename from Tests/Hasher/SHA1/EternalArrowBackup.Hasher.SHA1.Tests.csproj rename to Tests/Hasher/Sha256/EternalArrowBackup.Hasher.Sha256.Tests.csproj index 97772a4..d688613 100644 --- a/Tests/Hasher/SHA1/EternalArrowBackup.Hasher.SHA1.Tests.csproj +++ b/Tests/Hasher/Sha256/EternalArrowBackup.Hasher.Sha256.Tests.csproj @@ -11,7 +11,7 @@ - + diff --git a/Tests/Hasher/SHA1/HasherTests.cs b/Tests/Hasher/Sha256/HasherTests.cs similarity index 70% rename from Tests/Hasher/SHA1/HasherTests.cs rename to Tests/Hasher/Sha256/HasherTests.cs index 42c14c1..75287de 100644 --- a/Tests/Hasher/SHA1/HasherTests.cs +++ b/Tests/Hasher/Sha256/HasherTests.cs @@ -1,4 +1,4 @@ -namespace EternalArrowBackup.Hasher.SHA1 +namespace EternalArrowBackup.Hasher.Sha256 { using System; using System.Diagnostics; @@ -9,19 +9,20 @@ public static class HasherTests { - private const int MegabytesPerSecondLowSpeed = 200; - private const int MegabytesPerSecondHighSpeed = 1000; + // Single-threaded SHA256 performance on modern CPUs is around 200-500MB/s + 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 [Theory] [Trait("Category", "Simple")] - [InlineData("DA39A3EE 5E6B4B0D 3255BFEF 95601890 AFD80709", "")] - [InlineData("A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D", "abc")] - [InlineData("84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")] + [InlineData("E3B0C442 98FC1C14 9AFBF4C8 996FB924 27AE41E4 649B934C A495991B 7852B855", "")] + [InlineData("BA7816BF 8F01CFEA 414140DE 5DAE2223 B00361A3 96177A9C B410FF61 F20015AD", "abc")] + [InlineData("248D6A61 D20638B8 E5C02693 0C3E6039 A33CE459 64FF2167 F6ECEDD4 19DB06C1", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")] public static async Task TestHashes(string expectedHash, string inputMessage) { var expectedHashFormatted = expectedHash.Replace(" ", string.Empty).ToLowerInvariant(); - var hasher = new Sha1ContentHasher(); + var hasher = new Sha256ContentHasher(); var messageBytes = Encoding.ASCII.GetBytes(inputMessage); using (var stream = new MemoryStream(messageBytes)) { @@ -30,7 +31,6 @@ } } - // Single-threaded SHA1 performance on i5-6500 is around 500MB/s [Theory] [Trait("Category", "Integration")] [InlineData(0)] @@ -45,13 +45,13 @@ var messageBytes = new byte[length]; 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 expectedTimeHigh = 1 + length / (MegabytesPerSecondLowSpeed * 1000); - var hasher = new Sha1ContentHasher(); + var hasher = new Sha256ContentHasher(); using (var stream = new MemoryStream(messageBytes)) { await hasher.ComputeHash(stream);