Beispiel #1
0
        public EventSerializer(ITypeNameMapper?typeNameMapper = null,
                               ITypeConvertorGenerator?typeConvertorGenerator = null, ISymmetricCipher?symmetricCipher = null)
        {
            TypeNameMapper     = typeNameMapper ?? new FullNameTypeMapper();
            ConvertorGenerator = typeConvertorGenerator ?? DefaultTypeConvertorGenerator.Instance;
            _symmetricCipher   = symmetricCipher ?? new InvalidSymmetricCipher();
            _id2Info.Add(null); // 0 = null
            _id2Info.Add(null); // 1 = back reference
            foreach (var predefinedType in BasicSerializersFactory.TypeDescriptors)
            {
                var infoForType = new SerializerTypeInfo
                {
                    Id         = _id2Info.Count,
                    Descriptor = predefinedType
                };
                _typeOrDescriptor2Info[predefinedType] = infoForType;
                _id2Info.Add(infoForType);
                _typeOrDescriptor2Info.TryAdd(predefinedType.GetPreferedType() !, infoForType);
                var descriptorMultipleNativeTypes = predefinedType as ITypeDescriptorMultipleNativeTypes;
                if (descriptorMultipleNativeTypes == null)
                {
                    continue;
                }
                foreach (var type in descriptorMultipleNativeTypes.GetNativeTypes())
                {
                    _typeOrDescriptor2Info[type] = infoForType;
                }
            }

            while (_id2Info.Count < ReservedBuildinTypes)
            {
                _id2Info.Add(null);
            }
        }
Beispiel #2
0
        public void Open(IKeyValueDB keyValueDB, bool dispose, DBOptions options)
        {
            _keyValueDB = keyValueDB ?? throw new ArgumentNullException(nameof(keyValueDB));
            _dispose    = dispose;
            _type2Name  = options.CustomType2NameRegistry ?? new Type2NameRegistry();
            _polymorphicTypesRegistry = new PolymorphicTypesRegistry();
            AutoRegisterTypes         = options.AutoRegisterType;
            ActualOptions             = options;
            SymmetricCipher           = options.SymmetricCipher ?? new InvalidSymmetricCipher();

            _tableInfoResolver     = new TableInfoResolver(keyValueDB, this);
            _tablesInfo            = new TablesInfo(_tableInfoResolver);
            _relationsInfoResolver = new RelationInfoResolver(this);
            _relationsInfo         = new RelationsInfo(_relationsInfoResolver);

            using var tr = _keyValueDB.StartTransaction();
            _lastObjId   = (long)tr.GetUlong(0);
            _lastDictId  = tr.GetUlong(1);
            if (_lastObjId == 0)
            {
                if (tr.FindLastKey(AllObjectsPrefix))
                {
                    _lastObjId = (long)PackUnpack.UnpackVUInt(tr.GetKey().Slice(AllObjectsPrefixLen));
                }
            }
            _tablesInfo.LoadTables(LoadTablesEnum(tr));
            _relationsInfo.LoadRelations(LoadRelationNamesEnum(tr));
            if (_lastDictId == 0)
            {
                if (tr.FindExactKey(LastDictIdKey))
                {
                    _lastDictId = PackUnpack.UnpackVUInt(tr.GetValue());
                }
            }
        }
Beispiel #3
0
 public TypeSerializers(ITypeNameMapper?typeNameMapper = null, TypeSerializersOptions?options = null)
 {
     ConvertorGenerator = DefaultTypeConvertorGenerator.Instance;
     SetTypeNameMapper(typeNameMapper);
     ForgotAllTypesAndSerializers();
     _newSimpleSaverAction            = NewSimpleSaver;
     _newComplexSaverAction           = NewComplexSaver;
     _newDescriptorSaverFactoryAction = NewDescriptorSaverFactory;
     _loaderFactoryAction             = LoaderFactory;
     _buildFromTypeAction             = BuildFromType;
     _options         = options ?? TypeSerializersOptions.Default;
     _symmetricCipher = _options.SymmetricCipher ?? new InvalidSymmetricCipher();
 }
        public List <byte[]> Decrypt(ISymmetricCipher symmetricCipher, List <byte[]> cipherText)
        {
            var decryptedMessage = new byte[cipherText.Count][];

            Parallel.For(0, cipherText.Count,
                         blockNumber =>
            {
                var currentBlock = blockNumber > 0 ? cipherText[blockNumber - 1] : InitializationVector;
                var decryptdData = symmetricCipher.Encrypt(currentBlock);
                var openText     = CipherUtils.XorByteArrays(decryptdData, cipherText[blockNumber]);
                decryptedMessage[blockNumber] = openText;
            });

            return(decryptedMessage.ToList());
        }
        public List <byte[]> Encrypt(ISymmetricCipher symmetricCipher, List <byte[]> openText)
        {
            var c = InitializationVector;
            var encryptedMessage = new List <byte[]>();

            foreach (var messageBlock in openText)
            {
                var encryptedBlock = symmetricCipher.Encrypt(c);
                var xoredBlock     = CipherUtils.XorByteArrays(encryptedBlock, messageBlock);
                c = xoredBlock;
                encryptedMessage.Add(xoredBlock);
            }

            return(encryptedMessage);
        }
        private List <byte[]> EncryptionConvertion(ISymmetricCipher symmetricCipher, List <byte[]> cipherText)
        {
            var c = InitializationVector;

            var encryptedMessage = new byte[cipherText.Count][];

            for (int i = 0; i < encryptedMessage.Length; i++)
            {
                encryptedMessage[i] = symmetricCipher.Encrypt(c);
                c = encryptedMessage[i];
            }

            Parallel.For(0, encryptedMessage.Length,
                         blockNumber => encryptedMessage[blockNumber] = CipherUtils.XorByteArrays(cipherText[blockNumber], encryptedMessage[blockNumber]));

            return(encryptedMessage.ToList());
        }
        private List <byte[]> EncryptionConvertion(ISymmetricCipher symmetricCipher, List <byte[]> data, CipherAction cipherAction)
        {
            var encryptedMessageblocks = new byte[data.Count][];

            Func <byte[], byte[]> cipherConvertion = cipherAction switch
            {
                CipherAction.Encrypt => symmetricCipher.Encrypt,
                CipherAction.Decrypt => symmetricCipher.Decrypt
            };

            var parallelForResult = Parallel.For(0, data.Count, blockNumber =>
            {
                encryptedMessageblocks[blockNumber] = cipherConvertion(data[blockNumber]);
            });

            return(encryptedMessageblocks.ToList());
        }
    }
Beispiel #8
0
        public EncryptionAlgorithmJob(ISymmetricCipher symmetricCipher)
        {
            Actions = new Action[]
            {
                Encrypt,
                Decrypt,
                GenerateRandomKey
            };
            var paddingService = new PaddingService();
            var strategies     = new Dictionary <SymmetricCipherMode, ICipherStrategy>()
            {
                [SymmetricCipherMode.ElectronicCodeBook] = new ElectronicCodeBookStrategy()
            };

            var symmetricCipherManager = new SymmetricCipherManager(symmetricCipher, paddingService, strategies);

            _symmetricSystem = new SymmetricSystem(symmetricCipherManager);
        }
Beispiel #9
0
        public void Open(IKeyValueDB keyValueDB, bool dispose, DBOptions options)
        {
            if (keyValueDB == null)
            {
                throw new ArgumentNullException(nameof(keyValueDB));
            }
            _keyValueDB = keyValueDB;
            _dispose    = dispose;
            _type2Name  = options.CustomType2NameRegistry ?? new Type2NameRegistry();
            _polymorphicTypesRegistry = new PolymorphicTypesRegistry();
            AutoRegisterTypes         = options.AutoRegisterType;
            ActualOptions             = options;
            _symmetricCipher          = options.SymmetricCipher ?? new InvalidSymmetricCipher();

            _tableInfoResolver     = new TableInfoResolver(keyValueDB, this);
            _tablesInfo            = new TablesInfo(_tableInfoResolver);
            _relationsInfoResolver = new RelationInfoResolver(this);
            _relationsInfo         = new RelationsInfo(_relationsInfoResolver);

            using (var tr = _keyValueDB.StartTransaction())
            {
                _lastObjId  = (long)tr.GetUlong(0);
                _lastDictId = tr.GetUlong(1);
                if (_lastObjId == 0)
                {
                    tr.SetKeyPrefix(AllObjectsPrefix);
                    if (tr.FindLastKey())
                    {
                        _lastObjId = (long)new KeyValueDBKeyReader(tr).ReadVUInt64();
                    }
                }
                _tablesInfo.LoadTables(LoadTablesEnum(tr));
                _relationsInfo.LoadRelations(LoadRelationNamesEnum(tr));
                if (_lastDictId == 0)
                {
                    tr.SetKeyPrefix(null);
                    if (tr.FindExactKey(LastDictIdKey))
                    {
                        _lastDictId = new ByteArrayReader(tr.GetValueAsByteArray()).ReadVUInt64();
                    }
                }
            }
        }
Beispiel #10
0
 public DBOptions WithSymmetricCipher(ISymmetricCipher cipher)
 {
     SymmetricCipher = cipher;
     return(this);
 }
Beispiel #11
0
 public SecureConnection(IConnection connection, IAsymmetricCipher asymmetricCipher, ISymmetricCipher symmetricCipher)
 {
     this.Connection            = connection;
     this.SymmetricCipher       = symmetricCipher;
     this.AsymmetricCipher      = asymmetricCipher;
     connection.OnDataReceived += OnEncryptedMessageReceived;
     connection.OnDisconnected += () => OnDisconnected?.Invoke();
 }
 public List <byte[]> Decrypt(ISymmetricCipher symmetricCipher, List <byte[]> cipherText) =>
 EncryptionConvertion(symmetricCipher, cipherText, CipherAction.Decrypt);
 public List <byte[]> Encrypt(ISymmetricCipher symmetricCipher, List <byte[]> openText) =>
 EncryptionConvertion(symmetricCipher, openText, CipherAction.Encrypt);
Beispiel #14
0
 public TypeSerializersMapping(TypeSerializers typeSerializers)
 {
     _typeSerializers = typeSerializers;
     _symmetricCipher = _typeSerializers.GetSymmetricCipher();
     AddBuildInTypes();
 }
Beispiel #15
0
 public SecureClientConnection(IConnection connection, IAsymmetricCipher asymmetricCipher, ISymmetricCipher symmetricCipher)
     : base(connection, asymmetricCipher, symmetricCipher)
 {
 }