< Summary

Information
Class: Common.Aes256<T>
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: 15
Uncovered lines: 0
Coverable lines: 15
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
get_Body()100%1100%
.ctor(...)100%1100%
op_Implicit(...)100%1100%
Decrypt(...)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)
 9    {
 10        using var aes = new AesGcm(key.Value);
 11
 12        var plaintextBytes = JsonSerializer.SerializeToUtf8Bytes(target);
 13        var body = new byte[plaintextBytes.Length];
 14        var tag = new byte[AesGcm.TagByteSizes.MaxSize];
 15        var iv = RandomNumberGenerator.GetBytes(AesGcm.NonceByteSizes.MaxSize);
 16
 17        aes.Encrypt(iv, plaintextBytes, body, tag);
 18
 19        return new Aes256<T>(body, iv, tag);
 20    }
 21}
 22
 6023public record Aes256<T>(byte[] Body, byte[] IV, byte[] Tag)
 824{
 825    public static implicit operator Aes256<T>(in (T Target, AesKey Key) v) =>
 126        Aes256.Encrypt(v.Target, v.Key);
 827
 828    public T Decrypt(in AesKey key)
 429    {
 430        using var aes = new AesGcm(key.Value);
 831
 432        var plaintextBytes = new byte[Body.Length];
 433        aes.Decrypt(IV, Body, Tag, plaintextBytes);
 834
 435        return JsonSerializer.Deserialize<T>(plaintextBytes)!;
 436    }
 837}