diff --git a/SystemExtensions.NetStandard.Security/Hashing/BaseHMACService.cs b/SystemExtensions.NetStandard.Security/Hashing/BaseHMACService.cs new file mode 100644 index 0000000..bba34d5 --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/BaseHMACService.cs @@ -0,0 +1,64 @@ +using System.IO; +using System.Security.Cryptography; +using System.Threading.Tasks; + +namespace System.Security.Hashing; +public abstract class BaseHMACService : IHMACService + where T : HMAC +{ + protected abstract T GetHashAlgorithm(byte[] key); + + public async Task Hash(string key, string raw) + { + using var rawStream = BytesToStream(StringToBytes(raw)); + var keyBytes = StringToBytes(key); + using var hashedStream = await this.HashInternal(keyBytes, rawStream).ConfigureAwait(false); + return StreamToString(hashedStream); + } + public async Task Hash(byte[] key, byte[] raw) + { + using var rawStream = BytesToStream(raw); + using var hashedStream = await this.HashInternal(key, rawStream).ConfigureAwait(false); + return StreamToBytes(hashedStream); + } + public async Task Hash(byte[] key, Stream raw) + { + return await this.HashInternal(key, raw); + } + + private static byte[] StreamToBytes(Stream value) + { + value.Position = 0; + var buffer = new byte[1024]; + using var ms = new MemoryStream(); + var read = -1; + do + { + read = value.Read(buffer, 0, buffer.Length); + ms.Write(buffer, 0, read); + } while (read > 0); + return ms.ToArray(); + } + private static string StreamToString(Stream value) + { + return Convert.ToBase64String(StreamToBytes(value)); + } + private static byte[] StringToBytes(string value) + { + return Convert.FromBase64String(value); + } + private static Stream BytesToStream(byte[] value) + { + return new MemoryStream(value); + } + private async Task HashInternal(byte[] key, Stream raw) + { + if (raw is null) + { + throw new ArgumentNullException(nameof(raw)); + } + + using var algo = this.GetHashAlgorithm(key); + return await Task.Run(() => new MemoryStream(algo.ComputeHash(raw))); + } +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/BaseHashingService.cs b/SystemExtensions.NetStandard.Security/Hashing/BaseHashingService.cs new file mode 100644 index 0000000..c4c1fd2 --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/BaseHashingService.cs @@ -0,0 +1,63 @@ +using System.IO; +using System.Security.Cryptography; +using System.Threading.Tasks; + +namespace System.Security.Hashing; +public abstract class BaseHashingService + where T : HashAlgorithm +{ + protected abstract T GetHashAlgorithm(); + + public async Task Hash(string raw) + { + using var rawStream = BytesToStream(StringToBytes(raw)); + using var hashedStream = await this.HashInternal(rawStream).ConfigureAwait(false); + return StreamToString(hashedStream); + } + public async Task Hash(byte[] raw) + { + using var rawStream = BytesToStream(raw); + using var hashedStream = await this.HashInternal(rawStream).ConfigureAwait(false); + return StreamToBytes(hashedStream); + } + public async Task Hash(Stream raw) + { + return await this.HashInternal(raw); + } + + private static byte[] StreamToBytes(Stream value) + { + value.Position = 0; + var buffer = new byte[1024]; + using var ms = new MemoryStream(); + var read = -1; + do + { + read = value.Read(buffer, 0, buffer.Length); + ms.Write(buffer, 0, read); + } while (read > 0); + return ms.ToArray(); + } + private static string StreamToString(Stream value) + { + return Convert.ToBase64String(StreamToBytes(value)); + } + private static byte[] StringToBytes(string value) + { + return Convert.FromBase64String(value); + } + private static Stream BytesToStream(byte[] value) + { + return new MemoryStream(value); + } + private async Task HashInternal(Stream raw) + { + if (raw is null) + { + throw new ArgumentNullException(nameof(raw)); + } + + using var algo = this.GetHashAlgorithm(); + return await Task.Run(() => new MemoryStream(algo.ComputeHash(raw))); + } +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/HMACMD5Service.cs b/SystemExtensions.NetStandard.Security/Hashing/HMACMD5Service.cs new file mode 100644 index 0000000..44dbb23 --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/HMACMD5Service.cs @@ -0,0 +1,10 @@ +using System.Security.Cryptography; + +namespace System.Security.Hashing; +public sealed class HMACMD5Service : BaseHMACService, IHMACMD5Service +{ + protected override HMACMD5 GetHashAlgorithm(byte[] key) + { + return new HMACMD5(key); + } +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/HMACSha1Service.cs b/SystemExtensions.NetStandard.Security/Hashing/HMACSha1Service.cs new file mode 100644 index 0000000..536f910 --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/HMACSha1Service.cs @@ -0,0 +1,10 @@ +using System.Security.Cryptography; + +namespace System.Security.Hashing; +public sealed class HMACSha1Service : BaseHMACService, IHMACSha1Service +{ + protected override HMACSHA1 GetHashAlgorithm(byte[] key) + { + return new HMACSHA1(key); + } +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/HMACSha256Service.cs b/SystemExtensions.NetStandard.Security/Hashing/HMACSha256Service.cs new file mode 100644 index 0000000..d37a966 --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/HMACSha256Service.cs @@ -0,0 +1,10 @@ +using System.Security.Cryptography; + +namespace System.Security.Hashing; +public sealed class HMACSha256Service : BaseHMACService, IHMACSha256Service +{ + protected override HMACSHA256 GetHashAlgorithm(byte[] key) + { + return new HMACSHA256(key); + } +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/HMACSha384Service.cs b/SystemExtensions.NetStandard.Security/Hashing/HMACSha384Service.cs new file mode 100644 index 0000000..975fb9b --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/HMACSha384Service.cs @@ -0,0 +1,10 @@ +using System.Security.Cryptography; + +namespace System.Security.Hashing; +public sealed class HMACSha384Service : BaseHMACService, IHMACSha384Service +{ + protected override HMACSHA384 GetHashAlgorithm(byte[] key) + { + return new HMACSHA384(key); + } +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/HMACSha512Service.cs b/SystemExtensions.NetStandard.Security/Hashing/HMACSha512Service.cs new file mode 100644 index 0000000..e98fcef --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/HMACSha512Service.cs @@ -0,0 +1,10 @@ +using System.Security.Cryptography; + +namespace System.Security.Hashing; +public sealed class HMACSha512Service : BaseHMACService, IHMACSha512Service +{ + protected override HMACSHA512 GetHashAlgorithm(byte[] key) + { + return new HMACSHA512(key); + } +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/IHMACMD5Service.cs b/SystemExtensions.NetStandard.Security/Hashing/IHMACMD5Service.cs new file mode 100644 index 0000000..e6d428c --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/IHMACMD5Service.cs @@ -0,0 +1,4 @@ +namespace System.Security.Hashing; +public interface IHMACMD5Service : IHMACService +{ +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/IHMACService.cs b/SystemExtensions.NetStandard.Security/Hashing/IHMACService.cs new file mode 100644 index 0000000..24cc58f --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/IHMACService.cs @@ -0,0 +1,10 @@ +using System.IO; +using System.Threading.Tasks; + +namespace System.Security.Hashing; +public interface IHMACService +{ + Task Hash(string key, string raw); + Task Hash(byte[] key, byte[] raw); + Task Hash(byte[] key, Stream raw); +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/IHMACSha1Service.cs b/SystemExtensions.NetStandard.Security/Hashing/IHMACSha1Service.cs new file mode 100644 index 0000000..35fee7e --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/IHMACSha1Service.cs @@ -0,0 +1,4 @@ +namespace System.Security.Hashing; +public interface IHMACSha1Service : IHMACService +{ +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/IHMACSha256Service.cs b/SystemExtensions.NetStandard.Security/Hashing/IHMACSha256Service.cs new file mode 100644 index 0000000..1826c0d --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/IHMACSha256Service.cs @@ -0,0 +1,4 @@ +namespace System.Security.Hashing; +public interface IHMACSha256Service : IHMACService +{ +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/IHMACSha384Service.cs b/SystemExtensions.NetStandard.Security/Hashing/IHMACSha384Service.cs new file mode 100644 index 0000000..48b43a6 --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/IHMACSha384Service.cs @@ -0,0 +1,4 @@ +namespace System.Security.Hashing; +public interface IHMACSha384Service : IHMACService +{ +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/IHMACSha512Service.cs b/SystemExtensions.NetStandard.Security/Hashing/IHMACSha512Service.cs new file mode 100644 index 0000000..84fe3d7 --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/IHMACSha512Service.cs @@ -0,0 +1,4 @@ +namespace System.Security.Hashing; +public interface IHMACSha512Service : IHMACService +{ +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/IMD5HashingService.cs b/SystemExtensions.NetStandard.Security/Hashing/IMD5HashingService.cs new file mode 100644 index 0000000..969557d --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/IMD5HashingService.cs @@ -0,0 +1,6 @@ +using System.Security.Hashing; + +namespace System.Security.Hashing; +public interface IMD5HashingService : IHashingService +{ +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/ISha1HashingService.cs b/SystemExtensions.NetStandard.Security/Hashing/ISha1HashingService.cs new file mode 100644 index 0000000..95bbbaa --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/ISha1HashingService.cs @@ -0,0 +1,6 @@ +using System.Security.Hashing; + +namespace System.Security.Hashing; +public interface ISha1HashingService : IHashingService +{ +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/ISha256HashingService.cs b/SystemExtensions.NetStandard.Security/Hashing/ISha256HashingService.cs new file mode 100644 index 0000000..81ea597 --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/ISha256HashingService.cs @@ -0,0 +1,6 @@ +using System.Security.Hashing; + +namespace System.Security.Hashing; +public interface ISha256HashingService : IHashingService +{ +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/ISha384HashingService.cs b/SystemExtensions.NetStandard.Security/Hashing/ISha384HashingService.cs new file mode 100644 index 0000000..941c271 --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/ISha384HashingService.cs @@ -0,0 +1,6 @@ +using System.Security.Hashing; + +namespace System.Security.Hashing; +public interface ISha384HashingService : IHashingService +{ +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/ISha512HashingService.cs b/SystemExtensions.NetStandard.Security/Hashing/ISha512HashingService.cs new file mode 100644 index 0000000..832d772 --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/ISha512HashingService.cs @@ -0,0 +1,6 @@ +using System.Security.Hashing; + +namespace System.Security.Hashing; +public interface ISha512HashingService : IHashingService +{ +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/MD5HashingService.cs b/SystemExtensions.NetStandard.Security/Hashing/MD5HashingService.cs new file mode 100644 index 0000000..7d5cf65 --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/MD5HashingService.cs @@ -0,0 +1,10 @@ +using System.Security.Cryptography; + +namespace System.Security.Hashing; +public sealed class MD5HashingService : BaseHashingService, IMD5HashingService +{ + protected override MD5 GetHashAlgorithm() + { + return MD5.Create(); + } +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/Sha1HashingService.cs b/SystemExtensions.NetStandard.Security/Hashing/Sha1HashingService.cs new file mode 100644 index 0000000..e1d7e6a --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/Sha1HashingService.cs @@ -0,0 +1,7 @@ +using System.Security.Cryptography; + +namespace System.Security.Hashing; +public sealed class Sha1HashingService : BaseHashingService, ISha1HashingService +{ + protected override SHA1 GetHashAlgorithm() => SHA1.Create(); +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/Sha256HashingService.cs b/SystemExtensions.NetStandard.Security/Hashing/Sha256HashingService.cs index 8c72dc4..aaead1c 100644 --- a/SystemExtensions.NetStandard.Security/Hashing/Sha256HashingService.cs +++ b/SystemExtensions.NetStandard.Security/Hashing/Sha256HashingService.cs @@ -1,61 +1,9 @@ -using System.IO; +using System.Hashing; using System.Security.Cryptography; -using System.Threading.Tasks; namespace System.Security.Hashing; -public sealed class Sha256HashingService : IHashingService +public sealed class Sha256HashingService : BaseHashingService, ISha256HashingService { - public async Task Hash(string raw) - { - using var rawStream = BytesToStream(StringToBytes(raw)); - using var hashedStream = await HashInternal(rawStream).ConfigureAwait(false); - return StreamToString(hashedStream); - } - public async Task Hash(byte[] raw) - { - using var rawStream = BytesToStream(raw); - using var hashedStream = await HashInternal(rawStream).ConfigureAwait(false); - return StreamToBytes(hashedStream); - } - public async Task Hash(Stream raw) - { - return await HashInternal(raw); - } - - private static byte[] StreamToBytes(Stream value) - { - value.Position = 0; - var buffer = new byte[1024]; - using var ms = new MemoryStream(); - var read = -1; - do - { - read = value.Read(buffer, 0, buffer.Length); - ms.Write(buffer, 0, read); - } while (read > 0); - return ms.ToArray(); - } - private static string StreamToString(Stream value) - { - return Convert.ToBase64String(StreamToBytes(value)); - } - private static byte[] StringToBytes(string value) - { - return Convert.FromBase64String(value); - } - private static Stream BytesToStream(byte[] value) - { - return new MemoryStream(value); - } - private static async Task HashInternal(Stream raw) - { - if (raw is null) - { - throw new ArgumentNullException(nameof(raw)); - } - - using var sha256 = SHA256.Create(); - return await Task.Run(() => new MemoryStream(sha256.ComputeHash(raw))); - } + protected override SHA256 GetHashAlgorithm() => SHA256.Create(); } diff --git a/SystemExtensions.NetStandard.Security/Hashing/Sha384HashingService.cs b/SystemExtensions.NetStandard.Security/Hashing/Sha384HashingService.cs new file mode 100644 index 0000000..b0b1256 --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/Sha384HashingService.cs @@ -0,0 +1,10 @@ +using System.Security.Cryptography; + +namespace System.Security.Hashing; +public sealed class Sha384HashingService : BaseHashingService, ISha384HashingService +{ + protected override SHA384 GetHashAlgorithm() + { + return SHA384.Create(); + } +} diff --git a/SystemExtensions.NetStandard.Security/Hashing/Sha512HashingService.cs b/SystemExtensions.NetStandard.Security/Hashing/Sha512HashingService.cs new file mode 100644 index 0000000..7793be5 --- /dev/null +++ b/SystemExtensions.NetStandard.Security/Hashing/Sha512HashingService.cs @@ -0,0 +1,7 @@ +using System.Security.Cryptography; + +namespace System.Security.Hashing; +public sealed class Sha512HashingService : BaseHashingService, ISha512HashingService +{ + protected override SHA512 GetHashAlgorithm() => SHA512.Create(); +} diff --git a/SystemExtensions.NetStandard.Security/SystemExtensions.NetStandard.Security.csproj b/SystemExtensions.NetStandard.Security/SystemExtensions.NetStandard.Security.csproj index d0131fd..768e870 100644 --- a/SystemExtensions.NetStandard.Security/SystemExtensions.NetStandard.Security.csproj +++ b/SystemExtensions.NetStandard.Security/SystemExtensions.NetStandard.Security.csproj @@ -7,7 +7,7 @@ LICENSE true System - 1.2.5 + 1.3.0 Alexandru Macocian https://github.com/AlexMacocian/SystemExtensions Security extensions for the System namespace