SpawnDev.BlazorJS.Cryptography 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SpawnDev.BlazorJS.Cryptography --version 1.0.0                
NuGet\Install-Package SpawnDev.BlazorJS.Cryptography -Version 1.0.0                
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="SpawnDev.BlazorJS.Cryptography" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SpawnDev.BlazorJS.Cryptography --version 1.0.0                
#r "nuget: SpawnDev.BlazorJS.Cryptography, 1.0.0"                
#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 SpawnDev.BlazorJS.Cryptography as a Cake Addin
#addin nuget:?package=SpawnDev.BlazorJS.Cryptography&version=1.0.0

// Install SpawnDev.BlazorJS.Cryptography as a Cake Tool
#tool nuget:?package=SpawnDev.BlazorJS.Cryptography&version=1.0.0                

SpawnDev.BlazorJS.Cryptography

NuGet version

A cross platform cryptography library that supports encryption with AES-GCM, shared secret generation with ECDH, data signatures with ECDSA, and hashing with SHA on Windows, Linux, and Browser (Blazor WebAssembly) platforms.

This project aims to simplify common cryptography tasks with an API that is consistent on .Net Web API servers and in the web browser with Blazor WebAssembly.

PortableCrypto and the related classes wrap underlying cryptographic classes that are chosen based on the current platform. On Window and Linux, classes such as AesGcm, ECDiffieHellman, ECDsa, and SHA from Microsoft's System.Security.Cryptography library are used. When running under Blazor WebAssembly on the browser the browser Crypto and SubtleCrypto APIs are used. By wrapping these underlying libraries we can provide a consistent and reliable API regardless of the executing platform.

Getting started

Add the Nuget package

dotnet add package SpawnDev.BlazorJS.Cryptography

Add PortableCrypto service and SpawnDev.BlazorJS dependency to Program.cs


// Add BlazorJSRuntime service
builder.Services.AddBlazorJSRuntime();

// Add PortableCrypto service
builder.Services.AddSingleton<PortableCrypto>();

Inject PortableCrypto

[Inject] PortableCrypto PortableCrypto { get; set; }

PortableCrypto

  • The PortableCrypto service provides an API that can be used on the server and on the browser to provide cross platform compatible cryptographic methods.

PortableCrypto - SHA

Task<byte[]> Digest(string hashName, byte[] data)
  • Hash the specified data using the specified hash algorithm

PortableCrypto - ECDH

Task<PortableECDHKey> GenerateECDHKey(string namedCurve = NamedCurve.P521, bool extractable = true)
  • Generate a new ECDH crypto key
Task<byte[]> ExportPublicKeySpki(PortableECDHKey key)
  • Export the ECDH public key in Spki format
Task<byte[]> ExportPrivateKeyPkcs8(PortableECDHKey key)
  • Export the ECDH private key in Pkcs8 format
Task<PortableECDHKey> ImportECDHKey(byte[] publicKeySpki, string namedCurve = NamedCurve.P521, bool extractable = true)
  • Import the ECDH public key
Task<PortableECDHKey> ImportECDHKey(byte[] publicKeySpki, byte[] privateKeyPkcs8, string namedCurve = NamedCurve.P521, bool extractable = true)
  • Import the ECDH private key
Task<byte[]> DeriveBits(PortableECDHKey localPartyKey, PortableECDHKey otherPartyKey, int bitLength)
  • Create a shared secret that is cross-platform compatible
Task<byte[]> DeriveBits(PortableECDHKey localPartyKey, PortableECDHKey otherPartyKey)
  • Create a shared secret that is cross-platform compatible

PortableCrypto - ECDSA

Task<PortableECDSAKey> GenerateECDSAKey(string namedCurve = NamedCurve.P521, bool extractable = true)
  • Generate a new ECDSA key
Task<byte[]> ExportPublicKeySpki(PortableECDSAKey key)
  • Exports the ECDSA public key in Spki format
Task<byte[]> ExportPrivateKeyPkcs8(PortableECDSAKey key)
  • Exports the ECDSA private key in Pkcs8 format
Task<PortableECDSAKey> ImportECDSAKey(byte[] publicKeySpkiData, string namedCurve = NamedCurve.P521, bool extractable = true)
  • Import an ECDSA public key
Task<PortableECDSAKey> ImportECDSAKey(byte[] publicKeySpkiData, byte[] privateKeyPkcs8Data, string namedCurve = NamedCurve.P521, bool extractable = true)
  • Import an ECDSA public and private key
Task<bool> Verify(PortableECDSAKey key, byte[] data, byte[] signature, string hashName = HashName.SHA512)
  • Verify a data signature
Task<byte[]> Sign(PortableECDSAKey key, byte[] data, string hashName = HashName.SHA512)
  • Sign data using an ECDSA key
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 is compatible. 
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
2.0.5 36 11/22/2024
2.0.3 72 11/16/2024
2.0.2 72 11/15/2024
2.0.0 70 11/14/2024
1.0.0 75 11/12/2024