< Summary

Information
Class: Common.Aes256
Assembly: Common
File(s): /home/runner/work/poc-aes-type/poc-aes-type/src/Common/Aes256.cs
Tag: 6_2147224979
Line coverage
100%
Covered lines: 9
Uncovered lines: 0
Coverable lines: 9
Total lines: 37
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Coverage History

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
Encrypt(...)100%1100%

File(s)

/home/runner/work/poc-aes-type/poc-aes-type/src/Common/Aes256.cs

#LineLine coverage
 1using System.Security.Cryptography;
 2using System.Text.Json;
 3
 4namespace Common;
 5
 6public static class Aes256
 7{
 8    public static Aes256<T> Encrypt<T>(in T target, in AesKey key)
 49    {
 410        using var aes = new AesGcm(key.Value);
 11
 412        var plaintextBytes = JsonSerializer.SerializeToUtf8Bytes(target);
 413        var body = new byte[plaintextBytes.Length];
 414        var tag = new byte[AesGcm.TagByteSizes.MaxSize];
 415        var iv = RandomNumberGenerator.GetBytes(AesGcm.NonceByteSizes.MaxSize);
 16
 417        aes.Encrypt(iv, plaintextBytes, body, tag);
 18
 419        return new Aes256<T>(body, iv, tag);
 420    }
 21}
 22
 23public record Aes256<T>(byte[] Body, byte[] IV, byte[] Tag)
 24{
 25    public static implicit operator Aes256<T>(in (T Target, AesKey Key) v) =>
 26        Aes256.Encrypt(v.Target, v.Key);
 27
 28    public T Decrypt(in AesKey key)
 29    {
 30        using var aes = new AesGcm(key.Value);
 31
 32        var plaintextBytes = new byte[Body.Length];
 33        aes.Decrypt(IV, Body, Tag, plaintextBytes);
 34
 35        return JsonSerializer.Deserialize<T>(plaintextBytes)!;
 36    }
 37}

Methods/Properties

Encrypt(T&,Common.AesKey&)