More hashing services and HMAC services

This commit is contained in:
2024-09-08 17:08:22 +02:00
parent 535a2cedc0
commit def0258695
24 changed files with 275 additions and 56 deletions
@@ -0,0 +1,64 @@
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
namespace System.Security.Hashing;
public abstract class BaseHMACService<T> : IHMACService
where T : HMAC
{
protected abstract T GetHashAlgorithm(byte[] key);
public async Task<string> 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<byte[]> 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<Stream> 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<Stream> 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)));
}
}
@@ -0,0 +1,63 @@
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;
namespace System.Security.Hashing;
public abstract class BaseHashingService<T>
where T : HashAlgorithm
{
protected abstract T GetHashAlgorithm();
public async Task<string> Hash(string raw)
{
using var rawStream = BytesToStream(StringToBytes(raw));
using var hashedStream = await this.HashInternal(rawStream).ConfigureAwait(false);
return StreamToString(hashedStream);
}
public async Task<byte[]> Hash(byte[] raw)
{
using var rawStream = BytesToStream(raw);
using var hashedStream = await this.HashInternal(rawStream).ConfigureAwait(false);
return StreamToBytes(hashedStream);
}
public async Task<Stream> 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<Stream> 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)));
}
}
@@ -0,0 +1,10 @@
using System.Security.Cryptography;
namespace System.Security.Hashing;
public sealed class HMACMD5Service : BaseHMACService<HMACMD5>, IHMACMD5Service
{
protected override HMACMD5 GetHashAlgorithm(byte[] key)
{
return new HMACMD5(key);
}
}
@@ -0,0 +1,10 @@
using System.Security.Cryptography;
namespace System.Security.Hashing;
public sealed class HMACSha1Service : BaseHMACService<HMACSHA1>, IHMACSha1Service
{
protected override HMACSHA1 GetHashAlgorithm(byte[] key)
{
return new HMACSHA1(key);
}
}
@@ -0,0 +1,10 @@
using System.Security.Cryptography;
namespace System.Security.Hashing;
public sealed class HMACSha256Service : BaseHMACService<HMACSHA256>, IHMACSha256Service
{
protected override HMACSHA256 GetHashAlgorithm(byte[] key)
{
return new HMACSHA256(key);
}
}
@@ -0,0 +1,10 @@
using System.Security.Cryptography;
namespace System.Security.Hashing;
public sealed class HMACSha384Service : BaseHMACService<HMACSHA384>, IHMACSha384Service
{
protected override HMACSHA384 GetHashAlgorithm(byte[] key)
{
return new HMACSHA384(key);
}
}
@@ -0,0 +1,10 @@
using System.Security.Cryptography;
namespace System.Security.Hashing;
public sealed class HMACSha512Service : BaseHMACService<HMACSHA512>, IHMACSha512Service
{
protected override HMACSHA512 GetHashAlgorithm(byte[] key)
{
return new HMACSHA512(key);
}
}
@@ -0,0 +1,4 @@
namespace System.Security.Hashing;
public interface IHMACMD5Service : IHMACService
{
}
@@ -0,0 +1,10 @@
using System.IO;
using System.Threading.Tasks;
namespace System.Security.Hashing;
public interface IHMACService
{
Task<string> Hash(string key, string raw);
Task<byte[]> Hash(byte[] key, byte[] raw);
Task<Stream> Hash(byte[] key, Stream raw);
}
@@ -0,0 +1,4 @@
namespace System.Security.Hashing;
public interface IHMACSha1Service : IHMACService
{
}
@@ -0,0 +1,4 @@
namespace System.Security.Hashing;
public interface IHMACSha256Service : IHMACService
{
}
@@ -0,0 +1,4 @@
namespace System.Security.Hashing;
public interface IHMACSha384Service : IHMACService
{
}
@@ -0,0 +1,4 @@
namespace System.Security.Hashing;
public interface IHMACSha512Service : IHMACService
{
}
@@ -0,0 +1,6 @@
using System.Security.Hashing;
namespace System.Security.Hashing;
public interface IMD5HashingService : IHashingService
{
}
@@ -0,0 +1,6 @@
using System.Security.Hashing;
namespace System.Security.Hashing;
public interface ISha1HashingService : IHashingService
{
}
@@ -0,0 +1,6 @@
using System.Security.Hashing;
namespace System.Security.Hashing;
public interface ISha256HashingService : IHashingService
{
}
@@ -0,0 +1,6 @@
using System.Security.Hashing;
namespace System.Security.Hashing;
public interface ISha384HashingService : IHashingService
{
}
@@ -0,0 +1,6 @@
using System.Security.Hashing;
namespace System.Security.Hashing;
public interface ISha512HashingService : IHashingService
{
}
@@ -0,0 +1,10 @@
using System.Security.Cryptography;
namespace System.Security.Hashing;
public sealed class MD5HashingService : BaseHashingService<MD5>, IMD5HashingService
{
protected override MD5 GetHashAlgorithm()
{
return MD5.Create();
}
}
@@ -0,0 +1,7 @@
using System.Security.Cryptography;
namespace System.Security.Hashing;
public sealed class Sha1HashingService : BaseHashingService<SHA1>, ISha1HashingService
{
protected override SHA1 GetHashAlgorithm() => SHA1.Create();
}
@@ -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<SHA256>, ISha256HashingService
{
public async Task<string> Hash(string raw)
{
using var rawStream = BytesToStream(StringToBytes(raw));
using var hashedStream = await HashInternal(rawStream).ConfigureAwait(false);
return StreamToString(hashedStream);
}
public async Task<byte[]> Hash(byte[] raw)
{
using var rawStream = BytesToStream(raw);
using var hashedStream = await HashInternal(rawStream).ConfigureAwait(false);
return StreamToBytes(hashedStream);
}
public async Task<Stream> 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<Stream> 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();
}
@@ -0,0 +1,10 @@
using System.Security.Cryptography;
namespace System.Security.Hashing;
public sealed class Sha384HashingService : BaseHashingService<SHA384>, ISha384HashingService
{
protected override SHA384 GetHashAlgorithm()
{
return SHA384.Create();
}
}
@@ -0,0 +1,7 @@
using System.Security.Cryptography;
namespace System.Security.Hashing;
public sealed class Sha512HashingService : BaseHashingService<SHA512>, ISha512HashingService
{
protected override SHA512 GetHashAlgorithm() => SHA512.Create();
}
@@ -7,7 +7,7 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<RootNamespace>System</RootNamespace>
<Version>1.2.5</Version>
<Version>1.3.0</Version>
<Authors>Alexandru Macocian</Authors>
<RepositoryUrl>https://github.com/AlexMacocian/SystemExtensions</RepositoryUrl>
<Description>Security extensions for the System namespace</Description>