Helpers - Chewhern/ASodium GitHub Wiki

Note: Official explanation please do visit libsodium gitbook for more information.

Constant time test for equality

Initial Function

void Sodium_Memory_Compare(IntPtr ByteArray1IntPtr, IntPtr ByteArray2IntPtr, int BytesArrayMutualSize)

Example coding

Byte[] AuthenticationTagByte = new Byte[16];
Byte[] SampleAuthenticationTagByte = new Byte[16];
RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
rngCsp.GetBytes(SampleAuthenticationTagByte);
rngCsp.GetBytes(AuthenticationTagByte);
IntPtr BytesArrayIntPtr1 = Marshal.AllocHGlobal(16);
IntPtr BytesArrayIntPtr2 = Marshal.AllocHGlobal(16);
//For this demonstration we need to assume that the source of
//authentication tag byte gets from others...            
//Whereby sample authentication tag byte calculated through program..
Marshal.Copy(AuthenticationTagByte, 0, BytesArrayIntPtr1, 16);
Marshal.Copy(SampleAuthenticationTagByte, 0, BytesArrayIntPtr2, 16);
try 
{
    SodiumHelper.Sodium_Memory_Compare(BytesArrayIntPtr1, BytesArrayIntPtr2, 16);
}
catch 
{
    //This example code was on .Net Framework 4.7.2
    //or .Net 5 Winforms or above
    MessageBox.Show("Bytes Data Array 1 does not match with Bytes Data Array 2");
}

Hexadecimal encoding/decoding

Binary to hexadecimal and vice versa

Initial Functions

string BinaryToHex(Byte[] data)
Byte[] HexToBinary(String hex)

Example Coding

Byte[] RandomByte = new Byte[32];
Byte[] ResultByte = new Byte[32];
String ResultString = "";
RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
rngCsp.GetBytes(RandomByte);
ResultString=SodiumHelper.BinaryToHex(RandomByte);
ResultByte = SodiumHelper.HexToBinary(ResultString);
//This example code was on .Net Framework 4.7.2
//or .Net 5 Winforms or above
MessageBox.Show(ResultByte.SequenceEqual(RandomByte).ToString());

Base64 encoding/decoding

Initial Functions

String BinaryToBase64(byte[] data, Base64Variant variant = Base64Variant.Original)
Byte[] Base64ToBinary(string base64, string ignoredChars, Base64Variant variant = Base64Variant.Original)

Example Coding

Byte[] RandomByte = new Byte[32];
Byte[] ResultByte = new Byte[32];
String ResultString = "";
RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
rngCsp.GetBytes(RandomByte);
ResultString = SodiumHelper.BinaryToBase64(RandomByte);
ResultByte = SodiumHelper.Base64ToBinary(ResultString,null);
//This example code was on .Net Framework 4.7.2
//or .Net 5 Winforms or above
MessageBox.Show(ResultByte.SequenceEqual(RandomByte).ToString());

Incrementing large numbers (Don't know how to test)

Initial Function

Byte[] Sodium_Increment(Byte[] UnsignedNumberInBytesFormat)

Example Coding

MessageBox.Show("To use Sodium Increment, one must have a CPU architecture of AMD64 ASM");
Byte[] RandomByte = new Byte[32];
Byte[] ResultByte = new Byte[32];
RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
rngCsp.GetBytes(RandomByte);
ResultByte = SodiumHelper.Sodium_Increment(RandomByte);
//This example code was on .Net Framework 4.7.2
//or .Net 5 Winforms or above
MessageBox.Show(ResultByte.SequenceEqual(RandomByte).ToString());

Adding large numbers

Initial Function

Byte[] Sodium_Add(Byte[] UnsignedNumber1,Byte[] UnsignedNumber2)

Example Code

Byte[] RandomByte = new Byte[32];
Byte[] RandomByte2 = new Byte[32];
RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
rngCsp.GetBytes(RandomByte);
rngCsp.GetBytes(RandomByte2);
Byte[] ResultByte = new Byte[32];
ResultByte = SodiumHelper.Sodium_Add(RandomByte, RandomByte2);
//This example code was on .Net Framework 4.7.2
//or .Net 5 Winforms or above
MessageBox.Show("The addition result was "+new System.Numerics.BigInteger(ResultByte).ToString());

Subtracting large numbers

Initial Function

Byte[] Sodium_Sub(Byte[] UnsignedNumber1,Byte[] UnsignedNumber2)

Example Code

Byte[] RandomByte = new Byte[32];
Byte[] RandomByte2 = new Byte[32];
RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
rngCsp.GetBytes(RandomByte);
rngCsp.GetBytes(RandomByte2);
Byte[] ResultByte = new Byte[32];
ResultByte = SodiumHelper.Sodium_Sub(RandomByte, RandomByte2);
//This example code was on .Net Framework 4.7.2
//or .Net 5 Winforms or above
MessageBox.Show("The subtraction result was " + new System.Numerics.BigInteger(ResultByte).ToString());

Comparing large numbers

Initial Function

int Sodium_Compare(IntPtr NumberInByteArray1IntPtr, IntPtr NumberInByteArray2IntPtr, int BytesArrayMutualSize)

Example Code

Byte[] RandomByte = new Byte[32];
Byte[] RandomByte2 = new Byte[32];
int Result = 0;
RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
rngCsp.GetBytes(RandomByte);
rngCsp.GetBytes(RandomByte2);
IntPtr myIntPtr1 = Marshal.AllocHGlobal(32);
IntPtr myIntPtr2 = Marshal.AllocHGlobal(32);
Marshal.Copy(RandomByte, 0, myIntPtr1, 32);
Marshal.Copy(RandomByte2, 0, myIntPtr2, 32);
Result = SodiumHelper.Sodium_Compare(myIntPtr1, myIntPtr2, 32);
if (Result == -1) 
{
    //This example code was on .Net Framework 4.7.2
    //or .Net 5 Winforms or above
    MessageBox.Show("First number is smaller than second number");
}
else if(Result == 0) 
{
    //This example code was on .Net Framework 4.7.2
    //or .Net 5 Winforms or above
    MessageBox.Show("First number is equals to second number");
}
else 
{
    //This example code was on .Net Framework 4.7.2
    //or .Net 5 Winforms or above
    MessageBox.Show("First number is greater than second number");
}

Testing for all zeros

Initial Function

int Sodium_Is_Zero(Byte[] Data)

Example Code

Byte[] RandomByte = new Byte[32];
Byte[] ZeroByte = new Byte[32];
int Result = 0;
RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
rngCsp.GetBytes(RandomByte);
Result = SodiumHelper.Sodium_Is_Zero(RandomByte);
if (Result == 1) 
{
    //This example code was on .Net Framework 4.7.2
    //or .Net 5 Winforms or above
    MessageBox.Show("First byte array contains only zero in its elements");
}
else 
{
    //This example code was on .Net Framework 4.7.2
    //or .Net 5 Winforms or above
    MessageBox.Show("First byte array does not contain only zero in its elements");
}
Result = SodiumHelper.Sodium_Is_Zero(ZeroByte);
if (Result == 1)
{
    //This example code was on .Net Framework 4.7.2
    //or .Net 5 Winforms or above
    MessageBox.Show("Second byte array contains only zero in its elements");
}
else
{
    //This example code was on .Net Framework 4.7.2
    //or .Net 5 Winforms or above
    MessageBox.Show("Second byte array does not contain only zero in its elements");
}

Clearing the stack (Not sure how it works and to test)

Initial Function

void Sodium_StackZero(int BytesArrayLength)

Example Code

SodiumHelper.Sodium_StackZero(32);