Example #1
0
 public async Task <Document> Deprotect(Document value, ProtectionKey key, CancellationToken cancellationToken = default)
 {
     try
     {
         if (!await IsProtected(value, cancellationToken))
         {
             return(value);
         }
         var bs = Convert.FromBase64String(value.Raw);
         // var ky = Encoding.UTF8.GetBytes(key.Password);
         // await using var ms = new MemoryStream(AesDecrypt(bs, ky));
         await using var ms = new MemoryStream(bs);
         return(await JsonSerializer.DeserializeAsync <Document>(ms, cancellationToken : cancellationToken)
                ?? throw new NullReferenceException("Null"));
     }
     catch (Exception ex)
     {
         throw new ProtectionException("Deprotect failed.", ex);
     }
 }
Example #2
0
        public async Task <Document> Protect(Document value, ProtectionKey key, CancellationToken cancellationToken = default)
        {
            try
            {
                byte[] bs;
                await using (var ms = new MemoryStream())
                {
                    await JsonSerializer.SerializeAsync(ms, value, cancellationToken : cancellationToken);

                    bs = ms.ToArray();
                }

                // var ky = Encoding.UTF8.GetBytes(key.Password); AesEncrypt(bs, ky)
                return(new Document {
                    Tag = _protectFlag, Raw = Convert.ToBase64String(bs)
                });
            }
            catch (Exception ex)
            {
                throw new ProtectionException("Protect failed.", ex);
            }
        }