public static IEnumerable <T> Merge <T>(
        IEnumerable <IEnumerable <T> > listOfLists,
        Func <T, T, int> comparison = null)
    {
        IComparer <T> cmp = comparison != null
                                ? Comparer <T> .Create(new Comparison <T>(comparison))
                                : Comparer <T> .Default;

        List <IEnumerator <T> > es = listOfLists
                                     .Select(l => l.GetEnumerator())
                                     .Where(e => e.MoveNext())
                                     .ToList();
        var bag = new OrderedBag <KeyEnumerator <T> >();

        es.ForEach(e => bag.Add(new KeyEnumerator <T>(e.Current, e, cmp)));
        while (bag.Count > 0)
        {
            KeyEnumerator <T> kv = bag.RemoveFirst();
            IEnumerator <T>   e  = kv.Enumerator;
            yield return(e.Current);

            if (e.MoveNext())
            {
                bag.Add(new KeyEnumerator <T>(e.Current, e, cmp));
            }
        }
    }
示例#2
0
        protected virtual bool Storage_Find(Stack stack)
        {
            var storageContext = stack.PopObject <StorageContext>();

            if (storageContext == null)
            {
                return(false);
            }
            if (!CheckStorageContext(storageContext))
            {
                return(false);
            }

            var prefix = stack.PopByteArray();

            byte[] prefixKey;

            using (var ms = new MemoryStream())
            {
                var index  = 0;
                var remain = prefix.Length;

                while (remain >= 16)
                {
                    ms.Write(prefix, index, 16);
                    ms.WriteByte(0);
                    index  += 16;
                    remain -= 16;
                }

                if (remain > 0)
                {
                    ms.Write(prefix, index, remain);
                }

                prefixKey = storageContext.ScriptHash.ToArray().Concat(ms.ToArray()).ToArray();
            }

            // TODO: Find a better way not to expose StackItem types or Create* methods
            var enumerator = Storages.Find(prefixKey)
                             .Where(p => p.Key.Key.Take(prefix.Length).SequenceEqual(prefix))
                             .Select(p => new KeyValuePair <StackItemBase, StackItemBase>(stack.CreateByteArray(p.Key.Key), stack.CreateByteArray(p.Value.Value)))
                             .GetEnumerator();
            var keyEnumerator = new KeyEnumerator(enumerator);

            stack.PushObject(keyEnumerator);
            _disposables.Add(keyEnumerator);

            return(true);
        }
示例#3
0
        public BitArray Decrypt(BitArray cipherText, BitArray inputKey)
        {
            ThrowHelper.CheckInputs(cipherText, inputKey, KeySize);

            var plainTextBlocks  = new List <BitArray>();
            var cipherTextBlocks = cipherText.SeparateBlocks();
            var keys             = KeyEnumerator.ExpandKey(inputKey, KeySize);

            keys.Reverse();

            foreach (var cipherTextBlock in cipherTextBlocks)
            {
                var plainTextBlock = cipherTextBlock.Copy();

                for (int i = 0; i < KeySize.NumRounds(); i++)
                {
                    var key = keys[i];

                    if (i == 0)
                    {
                        plainTextBlock = plainTextBlock
                                         .AddRoundKey(key)
                                         .InvShiftRows()
                                         .InvSubBytes();
                    }
                    else
                    {
                        plainTextBlock = plainTextBlock
                                         .AddRoundKey(key)
                                         .InvMixColumns()
                                         .InvShiftRows()
                                         .InvSubBytes();
                    }
                }

                plainTextBlocks.Add(plainTextBlock
                                    .AddRoundKey(inputKey));
            }

            return(plainTextBlocks.CombineBlocks());
        }
示例#4
0
        public BitArray Encrypt(BitArray plainText, BitArray inputKey)
        {
            ThrowHelper.CheckInputs(plainText, inputKey, KeySize);

            var cipherTextBlocks = new List <BitArray>();
            var plainTextBlocks  = plainText.SeparateBlocks();
            var keys             = KeyEnumerator.ExpandKey(inputKey, KeySize);

            foreach (var plainTextBlock in plainTextBlocks)
            {
                var cipherTextBlock = plainTextBlock
                                      .Copy().AddRoundKey(inputKey);

                for (int i = 0; i < KeySize.NumRounds(); i++)
                {
                    var key = keys[i];

                    if (i == plainTextBlocks.Count - 1)
                    {
                        cipherTextBlock = cipherTextBlock
                                          .SubBytes()
                                          .ShiftRows()
                                          .AddRoundKey(key);
                    }
                    else
                    {
                        cipherTextBlock = cipherTextBlock
                                          .SubBytes()
                                          .ShiftRows()
                                          .MixColumns()
                                          .AddRoundKey(key);
                    }
                }

                cipherTextBlocks.Add(cipherTextBlock);
            }

            return(cipherTextBlocks.CombineBlocks());
        }
示例#5
0
        public void ExpandKeyTest()
        {
            // Arrange
            var inputKey = new BitArray(128);

            for (int i = 0; i < inputKey.Length; i++)
            {
                inputKey[i] = true;
            }

            // Act
            var keys = KeyEnumerator.ExpandKey(inputKey, KeySize._128);

            // Assert TODO
            Assert.AreEqual(10, keys.Count);
            foreach (var key in keys)
            {
                Assert.AreEqual(128, key.Length);
                foreach (bool bit in key)
                {
                    Assert.IsTrue(bit);
                }
            }
        }
 public int CompareTo(KeyEnumerator <TKey> other)
 {
     return(KeyComparer.Compare(Key, other.Key));
 }
示例#7
0
 public KeyEnumerator(Dictionary <string, JSONNode> .Enumerator aDictEnum)
 {
     this = new KeyEnumerator(new Enumerator(aDictEnum));
 }
示例#8
0
 public KeyEnumerator(List <JSONNode> .Enumerator aArrayEnum)
 {
     this = new KeyEnumerator(new Enumerator(aArrayEnum));
 }