Exemple #1
0
        public async Task AddDiscoveredFile(Stream fileStream, string title, string extension, string originalLocation, string metadata, string sourceName)
        {
            using (var dbContext = new ApplicationDbContext(_dbContextOptions))
            {
                var source = await dbContext.Sources.Where(s => s.Name.ToLower() == sourceName.ToLower()).FirstOrDefaultAsync();

                if (source == null)
                {
                    source = new Source
                    {
                        Name = sourceName
                    };
                    await dbContext.AddAsync(source);

                    await dbContext.SaveChangesAsync();
                }

                string hash = CalculateMD5(fileStream);
                fileStream.Seek(0, SeekOrigin.Begin);
                var file = await dbContext.Files.Where(f => f.Hash == hash).FirstOrDefaultAsync();

                if (file != null)
                {
                    // a matching file was found, but maybe it's a hash collision
                    if (!StreamsAreEqual(fileStream, _blobStore.Get(file.Id)))
                    {
                        // the files are actually different, force recreation of the file
                        file = null;
                    }
                }

                if (file == null)
                {
                    file = new File
                    {
                        Hash      = hash,
                        Title     = title,
                        Extension = extension
                    };
                    await dbContext.AddAsync(file);

                    await dbContext.SaveChangesAsync();

                    await _blobStore.PutAsync(fileStream, file.Id);
                }

                var fileSource = new FileSource
                {
                    FileId    = file.Id,
                    Metadata  = metadata,
                    SourceUri = originalLocation,
                    SourceId  = source.Id,
                };
                await dbContext.AddAsync(fileSource);

                await dbContext.SaveChangesAsync();
            }
        }
Exemple #2
0
        public void Treasury_StoreDateStampedDocument_DocumentCorrect()
        {
            var docBytes = Convert.ToBase64String(new byte[0]);
            var resp     = _treasury.Apply(new RecordExpenditure(123, "Costco Shopping", "receipt.jpg", docBytes));

            var receipt = _blobStore.Get($"{Clock.UnixUtcNow.Millis}-receipt.jpg");

            Assert.IsTrue(receipt.IsPresent);
            Assert.AreEqual(docBytes, Convert.ToBase64String(receipt.Value));
        }
        public async Task <IKey> Get(Guid id)
        {
            var encoded = await _blobStore.Get(id.ToId() + ".key");

            var bytes = Convert.FromBase64String(encoded);
            var key   = new Key(new EncryptionPolicy(), bytes);

            Array.Clear(bytes, 0, bytes.Length);

            return(key);
        }
Exemple #4
0
        /****************************************************************************/
        public async Task <IKey> Get(Guid id)
        {
            var encoded = await _blobStore.Get(id.ToId());

            var encrypted = Convert.FromBase64String(encoded);
            var decrypted = await _keyEncryptor.Decrypt(encrypted);

            var policy = new EncryptionPolicy {
                Id = id
            };

            byte[] keyBytes   = null;
            long   expires    = 0L;
            string policyData = null;

            using (var memStream = new MemoryStream(decrypted))
            {
                using (var reader = new BinaryReader(memStream))
                {
                    policy.KeySize   = reader.ReadInt32();
                    policy.BlockSize = reader.ReadInt32();

                    keyBytes   = reader.ReadBytes(policy.KeySize / 8);
                    expires    = reader.ReadInt64();
                    policyData = reader.ReadString();
                }
            }

            var policyStr = policyData.Split(new string[] { ";" }, StringSplitOptions.None);

            policy.Expires   = new DateTime(expires);
            policy.Padding   = policyStr[0];
            policy.Mode      = policyStr[1];
            policy.Algorithm = policyStr[2];

            var key = new Key(policy, keyBytes);

            Array.Clear(keyBytes, 0, keyBytes.Length);

            return(key);
        }