Example #1
0
        public static void ApplyEncryption(FilterBase filterBase, byte[] key, string parameter = "")
        {
            IEnumerable <string> types;

            if (parameter.IsNullOrEmpty())
            {
                types = new[] { EncryptionAesOfb128 }
            }
            ;
            else
            {
                types = parameter.Split(',').Select(x => x.Trim());
            }
            foreach (var type in types)
            {
                if (string.IsNullOrEmpty(type))
                {
                    continue;
                }
                switch (type)
                {
                case EncryptionAesOfb128:
                    if (key.Length > 16)
                    {
                        key = key.Take(16).ToArray();
                    }
                    filterBase.ApplyAesStreamFilter(key);
                    break;

                case EncryptionChacha20Ietf:
                    if (key.Length > 32)
                    {
                        key = key.Take(32).ToArray();
                    }
                    filterBase.ApplyFilterFromEncryptor(ChaCha20IetfEncryptor.Create, key);
                    break;

                case EncryptionSpeck0:
                    filterBase.ApplyFilterFromEncryptor(Speck.Ctr128128.Create, key);
                    break;

                case EncryptionSpeck064:
                    filterBase.ApplyFilterFromEncryptor(Speck.Ctr64128.Create, key);
                    break;

                case CompressionLz4_0:
                    filterBase.ApplyFilterFromFilterCreator(LZ4pn.LZ4Filter.GetFilter);
                    break;

                case HashCrc32c:
                    Crc32c.AddToFilterBase(filterBase, 0);
                    break;

                case EncryptionNone:
                    // no encryption
                    break;

                default:
                    throw new Exception($"unknown encryption '{type}'");
                }
            }
        }