コード例 #1
0
        public void Given_CategoryExists_When_SearchForEntry_Then_EntryFound()
        {
            // Arrange
            const string categoryName = "The Simpsons";
            const string entryName    = "Bart";
            var          dat          = new EnvCryptDat()
            {
                Categories = new[]
                {
                    new Category()
                    {
                        Name    = categoryName,
                        Entries = new []
                        {
                            new Entry()
                            {
                                Name    = "bart",
                                KeyName = "bart"
                            },
                            new Entry()
                            {
                                Name    = entryName,
                                KeyName = entryName
                            }
                        }
                    }
                }
            };

            // Act
            Entry foundEntry;
            var   hasBeenFound = dat.SearchForEntry(categoryName, entryName, out foundEntry);

            // Assert
            hasBeenFound.Should().BeTrue("entry exists in the DAT POCO");
            foundEntry.Name.Should().Be(entryName);
            foundEntry.KeyName.Should().Be(entryName);
        }
コード例 #2
0
        public IList <EntriesDecrypterResult <TKey> > Decrypt(IList <TKey> usingKeys, EnvCryptDat inDat, IList <CategoryEntryPair> categoryEntryPairs, bool throwExceptionIfEntryNotFound = true, bool throwIfDecryptingKeyNotFound = true, bool throwIfKeyCannotDecrypt = true)
        {
            Contract.Requires <ArgumentNullException>(usingKeys != null, "usingKeys");
            Contract.Requires <ArgumentException>(usingKeys.Any(), "usingKeys");
            Contract.Requires <EnvCryptException>(Contract.ForAll(usingKeys, k => k != null), "all keys in the list can be null");
            Contract.Requires <ArgumentNullException>(inDat != null, "inDat");
            Contract.Requires <ArgumentNullException>(categoryEntryPairs != null, "entryDetails");
            Contract.Ensures(Contract.Result <IList <EntriesDecrypterResult <TKey> > >() != null);
            //
            var ret = new List <EntriesDecrypterResult <TKey> >();

            var keysToUse = new List <TKey>(usingKeys.Count);

            for (uint kI = 0; kI < usingKeys.Count; kI++)
            {
                var currentKey = usingKeys[(int)kI];
                if (!_keySuitabilityChecker.IsDecryptingKey(currentKey))
                {
                    if (throwIfKeyCannotDecrypt)
                    {
                        throw new EnvCryptException("impossible to decrypt using this {0} key. Name: {1}",
                                                    currentKey.Algorithm, currentKey.Name);
                    }
                }
                else
                {
                    keysToUse.Add(currentKey);
                }
            }

            if (!keysToUse.Any())
            {
                return(ret);
            }


            for (uint tI = 0; tI < categoryEntryPairs.Count; tI++)
            {
                var currentRequest = categoryEntryPairs[(int)tI];
                var catName        = currentRequest.Category;
                var entryName      = currentRequest.Entry;

                Entry foundEntry;
                if (inDat.SearchForEntry(catName, entryName, out foundEntry))
                {
                    EntriesDecrypterResult <TKey> toAdd = null;
                    for (uint kI = 0; kI < keysToUse.Count; kI++)
                    {
                        var currentKey = keysToUse[(int)kI];
                        if (currentKey.Name == foundEntry.KeyName &&
                            currentKey.GetHashCode() == foundEntry.KeyHash)
                        {
                            var encodedDecryptedData =
                                _segmentEncrypter.Decrypt(foundEntry.EncryptedValue, currentKey);
                            var decodedDecryptedData =
                                _userStringConverter.Decode(encodedDecryptedData);

                            toAdd = new EntriesDecrypterResult <TKey>()
                            {
                                CategoryEntryPair = currentRequest,
                                DecryptedValue    = decodedDecryptedData,
                                DecryptedUsingKey = currentKey
                            };

                            ret.Add(toAdd);
                            break;
                        }
                    }
                    if (toAdd == null)
                    {
                        // Haven't found the key to decrypt this entry
                        if (throwIfDecryptingKeyNotFound)
                        {
                            throw new EnvCryptException("cannot find suitable key to decrypt. Entry name: {0}  Category: {1}  Required Key Name: {2}  Required Key Hash: {3}", entryName, catName, foundEntry.Name, foundEntry.KeyHash);
                        }
                    }
                }
                else
                {
                    if (throwExceptionIfEntryNotFound)
                    {
                        throw new EnvCryptException("entry not found.  Entry name: {0}  Category: {1}", entryName, catName);
                    }
                }
            }

            return(ret);
        }