Skip to content

Commit

Permalink
Dev (#33)
Browse files Browse the repository at this point in the history
+ Added `Bootstrap.AddWan24Crypto`
  • Loading branch information
nd1012 authored Nov 11, 2023
1 parent 4268a1e commit 04df1a1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,26 @@ This library is available as

These extension NuGet packages are available:

- [wan24-Crypto-BC (adopts post quantum algorithms from Bouncy Castle)](https://www.nuget.org/packages/wan24-Crypto-BC/)
- [wan24-Crypto-BC (adopts some algorithms from Bouncy Castle)](https://www.nuget.org/packages/wan24-Crypto-BC/)
- [wan24-Crypto-NaCl (adopts the Argon2id KDF algorithm from NSec)](https://www.nuget.org/packages/wan24-Crypto-NaCl/)
- [wan24-Crypto-TPM (simplifies including TPM into your apps security)](https://www.nuget.org/packages/wan24-Crypto-TPM/)

## Usage

In case you don't use the `wan24-Core` bootstrapper logic, you need to
initialize the library first:

```cs
wan24.Crypto.Bootstrap.Boot();
```

In case you work with dependency injection (DI), you may want to add some
services:

```cs
builder.Services.AddWan24Crypto();
```

### Hashing

```cs
Expand Down
40 changes: 39 additions & 1 deletion src/wan24-Crypto/Bootstrap.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using wan24.Core;
using Microsoft.Extensions.DependencyInjection;
using wan24.Core;
using wan24.Crypto.Authentication;
using wan24.StreamSerializerExtensions;

//TODO .NET 8: SHA3
Expand Down Expand Up @@ -27,6 +29,42 @@ public static void Boot()
StreamSerializer.AsyncDeserializer[typeof(TimeoutToken)] = async (s, t, v, o, ct) => await s.ReadTimeoutTokenAsync(cancellationToken: ct).DynamicContext();
}

/// <summary>
/// Add all <c>wan24-Crypto</c> registered algorithms as DI service objects
/// </summary>
/// <param name="services">Services</param>
/// <returns>Services</returns>
public static IServiceCollection AddWan24Crypto(this IServiceCollection services)
{
foreach (IAsymmetricAlgorithm algo in AsymmetricHelper.Algorithms.Values)
services.AddSingleton(algo.GetType(), algo);
foreach (EncryptionAlgorithmBase algo in EncryptionHelper.Algorithms.Values)
services.AddSingleton(algo.GetType(), algo);
foreach (HashAlgorithmBase algo in HashHelper.Algorithms.Values)
services.AddSingleton(algo.GetType(), algo);
foreach (MacAlgorithmBase algo in MacHelper.Algorithms.Values)
services.AddSingleton(algo.GetType(), algo);
foreach (KdfAlgorithmBase algo in KdfHelper.Algorithms.Values)
services.AddSingleton(algo.GetType(), algo);
services.AddSingleton(EncryptionHelper.DefaultAlgorithm);
services.AddSingleton(HashHelper.DefaultAlgorithm);
services.AddSingleton(MacHelper.DefaultAlgorithm);
services.AddSingleton(KdfHelper.DefaultAlgorithm);
services.AddSingleton(serviceProvider => CryptoEnvironment.PKI ?? throw new InvalidOperationException("No PKI defined"));
services.AddSingleton(serviceProvider => CryptoEnvironment.PrivateKeysStore ?? throw new InvalidOperationException("No private keys store defined"));
services.AddSingleton(serviceProvider => CryptoEnvironment.RandomGenerator ?? throw new InvalidOperationException("No random data generator defined"));
services.AddSingleton(serviceProvider => CryptoEnvironment.PakeAuthClient ?? throw new InvalidOperationException("No fast PAKE authentication client defined"));
services.AddSingleton(serviceProvider => CryptoEnvironment.PakeAuthServer ?? throw new InvalidOperationException("No fast PAKE authentication server defined"));
services.AddSingleton(serviceProvider => CryptoEnvironment.AsymmetricKeyPool ?? throw new InvalidOperationException("No asymmetric key pool defined"));
services.AddSingleton(serviceProvider => CryptoEnvironment.PakeAuthRecordPool ?? throw new InvalidOperationException("No PAKE authentication record pool defined"));
services.AddSingleton(serviceProvider => RND.SeedConsumer ?? throw new InvalidOperationException("No seed consumer defined"));
services.AddSingleton(serviceProvider => RND.Generator ?? throw new InvalidOperationException("No RNG defined"));
services.AddTransient<CryptoOptions>();
services.AddTransient(serviceProvider => serviceProvider.GetRequiredService<IAsymmetricKeyPool>().GetKey());
services.AddTransient(serviceProvider => serviceProvider.GetRequiredService<IPakeAuthRecordPool>().GetOne());
return services;
}

/// <summary>
/// Clear a byte array (will with random data and then zero)
/// </summary>
Expand Down
17 changes: 16 additions & 1 deletion src/wan24-Crypto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,26 @@ This library is available as

These extension NuGet packages are available:

- [wan24-Crypto-BC (adopts post quantum algorithms from Bouncy Castle)](https://www.nuget.org/packages/wan24-Crypto-BC/)
- [wan24-Crypto-BC (adopts some algorithms from Bouncy Castle)](https://www.nuget.org/packages/wan24-Crypto-BC/)
- [wan24-Crypto-NaCl (adopts the Argon2id KDF algorithm from NSec)](https://www.nuget.org/packages/wan24-Crypto-NaCl/)
- [wan24-Crypto-TPM (simplifies including TPM into your apps security)](https://www.nuget.org/packages/wan24-Crypto-TPM/)

## Usage

In case you don't use the `wan24-Core` bootstrapper logic, you need to
initialize the library first:

```cs
wan24.Crypto.Bootstrap.Boot();
```

In case you work with dependency injection (DI), you may want to add some
services:

```cs
builder.Services.AddWan24Crypto();
```

### Hashing

```cs
Expand Down
8 changes: 4 additions & 4 deletions src/wan24-Crypto/wan24-Crypto.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<PackageId>wan24-Crypto</PackageId>
<Title>wan24-Crypto</Title>
<Version>1.26.0</Version>
<Version>1.26.1</Version>
<Authors>nd1012</Authors>
<Company>Andreas Zimmermann, wan24.de</Company>
<Product>wan24-Crypto</Product>
Expand All @@ -29,9 +29,9 @@

<ItemGroup>
<PackageReference Include="ObjectValidation" Version="1.13.0" />
<PackageReference Include="Stream-Serializer-Extensions" Version="2.11.2" Condition="'$(Configuration)' != 'Trunk'" />
<PackageReference Include="wan24-Compression" Version="1.20.1" Condition="'$(Configuration)' != 'Trunk'" />
<PackageReference Include="wan24-Core" Version="1.40.0" Condition="'$(Configuration)' != 'Trunk'" />
<PackageReference Include="Stream-Serializer-Extensions" Version="2.11.3" Condition="'$(Configuration)' != 'Trunk'" />
<PackageReference Include="wan24-Compression" Version="1.20.2" Condition="'$(Configuration)' != 'Trunk'" />
<PackageReference Include="wan24-Core" Version="1.42.0" Condition="'$(Configuration)' != 'Trunk'" />
<ProjectReference Include="..\..\..\wan24-Core\src\Wan24-Core\Wan24-Core.csproj" Condition="'$(Configuration)' == 'Trunk'" />
<ProjectReference Include="..\..\..\wan24-Compression\src\wan24-Compression\wan24-Compression.csproj" Condition="'$(Configuration)' == 'Trunk'" />
<ProjectReference Include="..\..\..\Stream-Serializer-Extensions\src\Stream-Serializer-Extensions\Stream-Serializer-Extensions.csproj" Condition="'$(Configuration)' == 'Trunk'" />
Expand Down

0 comments on commit 04df1a1

Please sign in to comment.