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                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NCode.CryptoTransforms" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NCode.CryptoTransforms --version 1.0.1                
#r "nuget: NCode.CryptoTransforms, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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                

ci

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:

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 of ToBase64Transform and FromBase64Transform 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 246 7/23/2023
1.0.0 14,236 6/4/2017

Built on 2023-07-23 16:49:06Z