Git Product home page Git Product logo

cryptotransforms's Introduction

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.

cryptotransforms's People

Contributors

polewskm avatar jtone123 avatar

Stargazers

Patrik Gebrian avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

jtone123

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.