NCode.CryptoTransforms
1.0.1
Prefix Reserved
dotnet add package NCode.CryptoTransforms --version 1.0.1
NuGet\Install-Package NCode.CryptoTransforms -Version 1.0.1
<PackageReference Include="NCode.CryptoTransforms" Version="1.0.1" />
paket add NCode.CryptoTransforms --version 1.0.1
#r "nuget: NCode.CryptoTransforms, 1.0.1"
// Install NCode.CryptoTransforms as a Cake Addin #addin nuget:?package=NCode.CryptoTransforms&version=1.0.1 // Install NCode.CryptoTransforms as a Cake Tool #tool nuget:?package=NCode.CryptoTransforms&version=1.0.1
Overview
This library provides adapters for the missing hashing and base 64 algorithms in the .NET Standard frameworks.
Specifically this library provides implementations of ICryptoTransform
for HashAlgorithm
, ToBase64Transform
,
and FromBase64Transform
.
Problem Statement
Hashing
HashAlgorithm
has been available for quite some time as a convenient way to abstract various hashing algorithms such
as MD5
and SHA256
. It is especially useful in conjunction with CryptoStream
that allows you to calculate the hash
from a Stream
while processing its data.
Stream s = GetStreamFromSomewhere();
using (HashAlgorithm hasher = HashAlgorithm.Create("SHA256"))
{
using (CryptoStream cs = new CryptoStream(s, hasher, CryptoStreamMode.Read)
{
int byteCount;
byte[] data = new byte[4096];
while ((byteCount = cs.Read(data, 0, data.Length)) > 0)
{
// do something useful with the actual read data
}
byte[] hash = hasher.Hash;
// do something useful with the hash
}
}
Unfortunatly the definition of HashAlgorithm
is not consistent across the different .NET Core frameworks:
- .NET Core 1.0.0 - HashAlgorithm : IDisposable
- .NET Core 1.1.0 - HashAlgorithm : IDisposable
- .NET Core 2.0.0 - HashAlgorithm : ICryptoTransform
This means that HashAlgorithm
cannot be used with CryptoStream
unless targeting .NET Standard 2.0 which hasn't
received much adoption yet.
Base 64
ToBase64Transform : ICryptoTransform
FromBase64Transform : ICryptoTransform
Similarly, if you search for ToBase64Transform
and FromBase64Transform
using .NET API Browser you will find that
these implementations are only available in .NET Standard 2.0 which hasn't received much adoption yet.
This means that any developers wishing to target ealier versions of .NET Standard such as 1.3 cannot use these base 64
transforms with ICryptoTransform
or CryptoStream
.
Solution
This library provides the following features:
- An adapter implementation of
ICryptoTransform
for the hashing algorithms already available in .NET Standard - An adapter implementation of
ICryptoTransform
for implementations ofToBase64Transform
andFromBase64Transform
using the base 64 libraries already available in .NET Standard
Adapter Details
Hashing
The following interface and class are provided to represent a cryptographic hash algorithm.
/// <summary>
/// Represents a cryptographic hash algorithm.
/// </summary>
public interface IHashTransform : ICryptoTransform
{
/// <summary>
/// Gets the size, in bits, of the computed hash code.
/// </summary>
int HashSize { get; }
/// <summary>
/// Gets the value of the computed hash code.
/// </summary>
byte[] Hash { get; }
}
/// <summary>
/// Provides the implemenation for a cryptographic hash algorithm.
/// </summary>
public class HashTransform : IHashTransform
{
/// <summary>
/// Creates an instance of the specified implementation of a hash algorithm.
/// </summary>
/// <param name="hashName">The hash algorithm implementation to use.</param>
public HashTransform(string hashName) { /* ... */ }
// .NET Core implementation is provided by 'IncrementalHash'
// .NET Framework implementation is delegated to the existing 'HashAlgorithm' class
}
Base 64
public class ToBase64Transform : ICryptoTransform { /* */ }
public class FromBase64Transform : ICryptoTransform { /* */ }
These classes simply implement the ICryptoTransform
interface by using the already
existing Convert.FromBase64CharArray
and Convert.ToBase64CharArray
builtin methods.
Release Notes
- v1.0.0 - Initial release
- v1.0.1 - Refresh the build and add CI using GitHub actions
Feedback
Please provide any feedback, comments, or issues to this GitHub project here.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard1.3 is compatible. netstandard1.4 was computed. netstandard1.5 was computed. netstandard1.6 was computed. netstandard2.0 was computed. netstandard2.1 was computed. |
.NET Framework | net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Universal Windows Platform | uap was computed. uap10.0 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.5
- NCode.ArrayLeases (>= 1.0.1)
- System.Buffers (>= 4.5.1)
-
.NETStandard 1.3
- NCode.ArrayLeases (>= 1.0.1)
- NETStandard.Library (>= 1.6.1)
- System.Buffers (>= 4.5.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Built on 2023-07-23 16:49:06Z