Files
admin/Azaion.Test/SecurityTest.cs
T
Alex Bezdieniezhnykh 121052a3ef Init commit
add security encryption and hashing: WIP
add endpoints: register user, get and save resources
add db main operations, User entity
2024-11-09 00:37:43 +02:00

41 lines
1.1 KiB
C#

using System.Text;
using Azaion.Services;
using FluentAssertions;
using Xunit;
namespace Azaion.Test;
public class SecurityTest
{
[Fact]
public async Task EncryptDecryptTest()
{
var testString = "Hello World Test dfvjkhsdbfvkljh sabdljsdafv asdv";
var username = "user@azaion.com";
var password = "testpw";
var key = Security.MakeEncryptionKey(username, password);
await using var encryptedStream = new MemoryStream();
await StringToStream(testString).Encrypt(encryptedStream, key);
await using var decryptedStream = new MemoryStream();
await encryptedStream.Decrypt(decryptedStream, key);
var str = StreamToString(decryptedStream);
str.Should().Be(testString);
}
private static string StreamToString(Stream stream)
{
stream.Position = 0;
using var reader = new StreamReader(stream, Encoding.UTF8);
return reader.ReadToEnd();
}
private static Stream StringToStream(string src)
{
var byteArray = Encoding.UTF8.GetBytes(src);
return new MemoryStream(byteArray);
}
}