Secret Key Cryptography _Authentication - Chewhern/ASodium GitHub Wiki

For detailed documentation, kindly refer official libsodium documentation.

Key generation

Initial function

public static Byte[] GenKey()

Example code

Byte[] SecretKeyAuthKey = SodiumSecretKeyAuth.GenKey();
MessageBox.Show(new System.Numerics.BigInteger(SecretKeyAuthKey).ToString());

MAC generation and verification

Initial functions

public static Byte[] Sign(Byte[] message, Byte[] Key,Boolean ClearKey=false)
public static void Verify(Byte[] Message, Byte[] MAC, Byte[] Key, Boolean ClearKey = false)

Helper function

public static Boolean VerifyMAC(Byte[] Message, Byte[] MAC, Byte[] Key, Boolean ClearKey = false)

Signing and verifying with initial function

Byte[] SecretKeyAuthKey = SodiumSecretKeyAuth.GenKey();
Byte[] RandomMessage = new Byte[128];
Byte[] MessageMAC = new Byte[] { };
RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
rngCsp.GetBytes(RandomMessage);
MessageMAC = SodiumSecretKeyAuth.Sign(RandomMessage, SecretKeyAuthKey);
//Perhaps in actual code.. there's a need to append the MessageMac
//with the actual message be it in the front or in the back.
try 
{
    SodiumSecretKeyAuth.Verify(RandomMessage, MessageMAC, SecretKeyAuthKey);
}
catch 
{
    MessageBox.Show("MAC does not match with original message");
}

Signing and verifying with helper function

Byte[] SecretKeyAuthKey = SodiumSecretKeyAuth.GenKey();
Byte[] RandomMessage = new Byte[128];
Byte[] MessageMAC = new Byte[] { };
RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
rngCsp.GetBytes(RandomMessage);
MessageMAC = SodiumSecretKeyAuth.Sign(RandomMessage, SecretKeyAuthKey);
//Perhaps in actual code.. there's a need to append the MessageMac
//with the actual message be it in the front or in the back.
Boolean IsSameMACComputed = SodiumSecretKeyAuth.Verify(RandomMessage,MessageMAC,SecretKeyAuthKey);
if(IsSameMACComputed==false)
{
    MessageBox.Show("MAC does not match with original message");
}