Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AttachmentCacheItem.cs
1using System;
2using System.IO;
4using System.Threading.Tasks;
7
9{
13 [CollectionName("AttachmentCache")]
14 [TypeName(TypeNameSerialization.None)]
15 [Index("AttachmentId")]
16 [Index("Expires")]
18 {
22 [ObjectId]
23 public string ObjectId { get; set; }
24
28 public string AttachmentId { get; set; }
29
33 public DateTime Created { get; set; }
34
38 public DateTime Expires { get; set; }
39
43 public string ContentType { get; set; }
44
49 [DefaultValueNull]
50 public string LocalAttachmentId { get; set; }
51
55 public string FileName { get; set; }
56
60 public long Size { get; set; }
61
65 public byte[] Salt { get; set; }
66
73 public async Task<byte[]> LoadContent()
74 {
75 using FileStream AttachmentFile = File.OpenRead(this.FileName);
76 Aes Aes = Aes.Create();
77
78 Aes.BlockSize = 128;
79 Aes.KeySize = 256;
80 Aes.Mode = CipherMode.CBC;
81 Aes.Padding = PaddingMode.Zeros;
82
83 byte[] Key = new byte[32];
84 byte[] IV = new byte[16];
85
86 Buffer.BlockCopy(this.Salt, 0, Key, 0, 32);
87 Buffer.BlockCopy(this.Salt, 32, IV, 0, 16);
88
89 using ICryptoTransform Decryptor = Aes.CreateDecryptor(Key, IV);
90 using CryptoStream DecryptedAttachmentFile = new CryptoStream(AttachmentFile, Decryptor, CryptoStreamMode.Read);
91
92 if (this.Size > int.MaxValue)
93 throw new InvalidOperationException("Attachment size exceeds maximum array size.");
94
95 return await DecryptedAttachmentFile.ReadAllAsync((int)this.Size);
96 }
97 }
98}
TypeNameSerialization
How the type name should be serialized.