Rainbow.CSharp.SDK.Cryptography 1.3.0

dotnet add package Rainbow.CSharp.SDK.Cryptography --version 1.3.0                
NuGet\Install-Package Rainbow.CSharp.SDK.Cryptography -Version 1.3.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="Rainbow.CSharp.SDK.Cryptography" Version="1.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Rainbow.CSharp.SDK.Cryptography --version 1.3.0                
#r "nuget: Rainbow.CSharp.SDK.Cryptography, 1.3.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 Rainbow.CSharp.SDK.Cryptography as a Cake Addin
#addin nuget:?package=Rainbow.CSharp.SDK.Cryptography&version=1.3.0

// Install Rainbow.CSharp.SDK.Cryptography as a Cake Tool
#tool nuget:?package=Rainbow.CSharp.SDK.Cryptography&version=1.3.0                

Provides ability to create objects implementing SASLMechanism.

These objects handle the SCRAM mechanism of SASL.

No strings are allocated during challenge. Only client-side mechanisms are provided.

Based on https://github.com/stazz/FluentCryptography and https://github.com/stazz/UtilPack

Code has been reworked:

  • to only support SCRAM-SHA-1, SCRAM-SHA-256 and SCRAM-SHA-512
  • to support .Net standard 2.0, 2.1, .Net 6.0 and .Net 8.0

Unnecessary code has been removed.

Example of use:

var credentials = new SASLCredentialsSCRAMForClient(
         userName,
         password // password may be clear-text password as string, or result of PBKDF2 iteration as byte array.
       );
       
String type = "SCRAM-SHA-512"; // To define which encryption must be used       
       
switch (type)
{

    case "SCRAM-SHA-1":
        var sha1 = new Rainbow.Cryptography.Digest.SHA128();
        client = sha1.CreateSASLClientSCRAM();
        break;

    case "SCRAM-SHA-256":
        var sha256 = new Rainbow.Cryptography.Digest.SHA256();
        client = sha256.CreateSASLClientSCRAM();
        break;

    case "SCRAM-SHA-512":
        var sha512 = new Rainbow.Cryptography.Digest.SHA512();
        client = sha512.CreateSASLClientSCRAM();
        break;
}

var encoding = new UTF8Encoding(false, false).CreateDefaultEncodingInfo();
var writeArray = new ResizableArray<Byte>();

var firstMessageToServer = Convert.ToBase64String( ComputeInitialResponse()); 

// => Need to send the first message to the server and read it's answer

var firstMessageFromServer = "..."; // <= Here it's the content of the server reponse

byte[] data = Convert.FromBase64String(firstMessageFromServer);
var secondMessageToServer = Convert.ToBase64String( ComputeFinalResponse(data));

// => Need to send the second message to the server and read it's answer

var secondMessageFromServer = "..."; // <= Here it's the content of the server reponse

data = Convert.FromBase64String(secondMessageFromServer);
VerifyServerSignature(data); // => If no exception, the authentication process is a success

private byte[] ComputeInitialResponse()
{

    var challengeArguments = credentials.CreateChallengeArguments(
      null, // Initial phase does not read anything
      -1,
      -1,
      writeArray,
      0,
      encoding
      );

    try
    {
        var task = client.ChallengeOrThrowOnErrorAsync(challengeArguments);
        (var bytesWritten, var challengeResult) = task.Result;

        if (bytesWritten > 0)
        {
            byte[] result = new byte[bytesWritten];
            Array.Copy(writeArray.Array, result, bytesWritten);
            return result;
        }
    }
    catch
    {

    }
    return new byte[0];
}

private byte[] ComputeFinalResponse(byte[] challenge)
{
    var challengeArguments = credentials.CreateChallengeArguments(
      challenge,
      0,
      challenge.Length,
      writeArray,
      0,
      encoding
      );

    try
    {
        var task = client.ChallengeOrThrowOnErrorAsync(challengeArguments);
        (var bytesWritten, var challengeResult) = task.Result;

        if (bytesWritten > 0)
        {
            byte[] result = new byte[bytesWritten];
            Array.Copy(writeArray.Array, result, bytesWritten);
            return result;
        }
    }
    catch
    {

    }
    return new byte[0];
}

private byte[] VerifyServerSignature(byte[] challenge)
{
    var challengeArguments = credentials.CreateChallengeArguments(
      challenge,
      0,
      challenge.Length,
      writeArray,
      0,
      encoding
      );

    var task = client.ChallengeOrThrowOnErrorAsync(challengeArguments);
    (var bytesWritten, var challengeResult) = task.Result;

    if(challengeResult == SASLChallengeResult.Completed)
        return new byte[0];

    throw new Exception("Server signature has not been verified with success");

}       
       
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 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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework 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 tizen40 was computed.  tizen60 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 (1)

Showing the top 1 NuGet packages that depend on Rainbow.CSharp.SDK.Cryptography:

Package Downloads
Sharp.Ws.Xmpp

Based on project Sharp.Xmpp, this package allows XMPP features using Web socket Several extensions have been added to support Rainbow features (https://openrainbow.com)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.0 272 7/3/2024
1.2.0 994 3/19/2024
1.1.0 1,048 1/9/2024
1.0.0 138 12/22/2023
0.0.1 107 12/21/2023