aboutsummaryrefslogtreecommitdiffstats
path: root/tests/slskd.Tests.Unit/Common/Cryptography/AesTests.cs
blob: cf236ee3e453e16158d655141bdac596ea5654f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
namespace slskd.Tests.Unit.Common.Cryptography
{
    using System.Text;
    using slskd.Cryptography;
    using Xunit;

    public class AesTests
    {
        [Fact]
        public void Generates_Random_Keys_Of_Expected_Length()
        {
            var key = Aes.GenerateRandomKey();
            Assert.Equal(48, key.Length);
        }

        [Fact]
        public void Encrypts_And_Decrypts()
        {
            var plainText = "hello, world!";
            var plainBytes = Encoding.UTF8.GetBytes(plainText);

            var key = Aes.GenerateRandomKey();

            var encryptedBytes = Aes.Encrypt(plainBytes, key);

            var decryptedBytes = Aes.Decrypt(encryptedBytes, key);

            var decryptedText = Encoding.UTF8.GetString(decryptedBytes);

            Assert.Equal(plainText, decryptedText);
        }
    }
}