patbef-ServiceOutside/ServiceOutsideTests/Crypto/SHA512.cs

49 lines
2.1 KiB
C#
Raw Normal View History

2024-01-29 16:27:34 +01:00
using NUnit.Framework;
using System;
using System.Security.Cryptography;
using System.Text;
namespace ServiceOutsideTests.Crypto
{
public class SHA512
{
[Test]
public void Encrypt()
{
Assert.IsTrue(ServiceShared.Crypto.SHA512.Encrypt("dasd hsadhuiahdiashd uihas uidhasi hdiuashd uihasid asd asd sa") == "a93500a760ec2ad79d540bea6828fc8e740d7a9c63cd0b7911486949051df53a36438887827e05d3f23a57fc4c149b6f6323d63ffc92c63be35a06108b3d6155");
Assert.IsTrue(ServiceShared.Crypto.SHA512.Encrypt("Hello world") == "b7f783baed8297f0db917462184ff4f08e69c2d5e5f79a942600f9725f58ce1f29c18139bf80b06c0fff2bdd34738452ecf40c488c22a7e3d80cdf6f9c1c0d47");
for (int i = 0; i < 1000; i++)
{
int KeySize = new Random().Next(1, 4096);
byte[] random = new byte[KeySize];
System.Security.Cryptography.RandomNumberGenerator.Fill(random);
string key = Encoding.UTF8.GetString(random);
Assert.IsTrue(ServiceShared.Crypto.SHA512.Encrypt(key).Length == 128);
}
}
[Test]
public void HMAC()
{
string input = "Hello World";
string password = "Test";
string hmac = ServiceShared.Crypto.SHA512.HMAC(input, ServiceShared.Crypto.AES.GetKey(password));
Assert.IsTrue(ServiceShared.Crypto.SHA512.isValidAuthenticationCode(hmac, input, ServiceShared.Crypto.AES.GetKey(password)));
Assert.IsTrue(hmac.Length == 128);
}
[Test]
public void HKDFDeriveKey()
{
string sharedSecret = "naDHwkXN6lxwAPnrh/SoR9lszmgDpLNDz4evQy8EKxA=";
string sharedKey = "1XBlnJf6Cw7nHM1/pvCZV2aYSL+0I5EErozAGRSV3Gg=";
byte[] sharedSecretBytes = Convert.FromBase64String(sharedSecret);
byte[] hkdfDeriveKey = HKDF.DeriveKey(HashAlgorithmName.SHA512, sharedSecretBytes, 32);
string hkdfDeriveKeyBase64 = Convert.ToBase64String(hkdfDeriveKey);
Assert.IsTrue(hkdfDeriveKeyBase64 == sharedKey);
}
}
}