Esempio n. 1
0
 public void ReadEntryValue(BinaryReader reader, Aes256Encryptor encryptor, out TEntryValue value, out uint metadata)
 {
     byte[] bytes  = reader.ReadBytes((int)Size);
     byte[] bytes2 = encryptor.Decrypt(bytes);
     value    = Deserialize(bytes2);
     metadata = 0u;
 }
Esempio n. 2
0
        public void ReadEntryValue(BinaryReader reader, Aes256Encryptor encryptor, out string value, out uint metadata)
        {
            bool flag = reader.ReadBoolean();

            value    = ((!flag) ? reader.ReadString() : null);
            metadata = 0u;
        }
        public void WriteEntryValue(TEntryValue entryValue, BinaryWriter writer, Aes256Encryptor encryptor)
        {
            serializationStream.Position = 0L;
            indexValueType.WriteEntryValue(entryValue, serializationWriter, encryptor);
            byte[] document = encryptor.Encrypt(serializationStream.ToArray());
            uint   value    = packedFile.Insert(document);

            writer.Write(value);
        }
 public void ReadEntryValue(BinaryReader reader, Aes256Encryptor encryptor, out TEntryValue value, out uint metadata)
 {
     metadata = reader.ReadUInt32();
     byte[] bytes = packedFile.Find(metadata);
     byte[] array = encryptor.Decrypt(bytes);
     serializationStream.Position = 0L;
     serializationStream.Write(array, 0, array.Length);
     serializationStream.Position = 0L;
     indexValueType.ReadEntryValue(serializationReader, encryptor, out value, out var _);
 }
Esempio n. 5
0
 public void WriteEntryValue(string entryValue, BinaryWriter writer, Aes256Encryptor encryptor)
 {
     if (entryValue == null)
     {
         writer.Write(value: true);
         return;
     }
     writer.Write(value: false);
     writer.Write(entryValue);
 }
Esempio n. 6
0
 public void ChangeEncryptor(Aes256Encryptor encryptor)
 {
     foreach (KeyValuePair <string, Index <bool> > boolIndex in boolIndexes)
     {
         boolIndex.Value.ChangeEncryptor(encryptor);
     }
     foreach (KeyValuePair <string, Index <sbyte> > sbyteIndex in sbyteIndexes)
     {
         sbyteIndex.Value.ChangeEncryptor(encryptor);
     }
     foreach (KeyValuePair <string, Index <byte> > byteIndex in byteIndexes)
     {
         byteIndex.Value.ChangeEncryptor(encryptor);
     }
     foreach (KeyValuePair <string, Index <short> > shortIndex in shortIndexes)
     {
         shortIndex.Value.ChangeEncryptor(encryptor);
     }
     foreach (KeyValuePair <string, Index <ushort> > ushortIndex in ushortIndexes)
     {
         ushortIndex.Value.ChangeEncryptor(encryptor);
     }
     foreach (KeyValuePair <string, Index <int> > intIndex in intIndexes)
     {
         intIndex.Value.ChangeEncryptor(encryptor);
     }
     foreach (KeyValuePair <string, Index <uint> > uintIndex in uintIndexes)
     {
         uintIndex.Value.ChangeEncryptor(encryptor);
     }
     foreach (KeyValuePair <string, Index <long> > longIndex in longIndexes)
     {
         longIndex.Value.ChangeEncryptor(encryptor);
     }
     foreach (KeyValuePair <string, Index <ulong> > ulongIndex in ulongIndexes)
     {
         ulongIndex.Value.ChangeEncryptor(encryptor);
     }
     foreach (KeyValuePair <string, Index <float> > floatIndex in floatIndexes)
     {
         floatIndex.Value.ChangeEncryptor(encryptor);
     }
     foreach (KeyValuePair <string, Index <double> > doubleIndex in doubleIndexes)
     {
         doubleIndex.Value.ChangeEncryptor(encryptor);
     }
     foreach (KeyValuePair <string, Index <char> > charIndex in charIndexes)
     {
         charIndex.Value.ChangeEncryptor(encryptor);
     }
     foreach (KeyValuePair <string, Index <string> > stringIndex in stringIndexes)
     {
         stringIndex.Value.ChangeEncryptor(encryptor);
     }
 }
 public bool ChangeEncryptor(Aes256Encryptor oldEncryptor, Aes256Encryptor newEncryptor)
 {
     foreach (KeyValuePair <uint, byte[]> document2 in packedFile.Documents)
     {
         byte[] value    = document2.Value;
         byte[] bytes    = oldEncryptor.Decrypt(value);
         byte[] document = newEncryptor.Encrypt(bytes);
         packedFile.Update(document2.Key, document);
     }
     return(false);
 }
Esempio n. 8
0
 public void ChangeEncryptor(Aes256Encryptor encryptor)
 {
     if (valueType.ChangeEncryptor(this.encryptor, encryptor))
     {
         uint num = 1u;
         fileStream.Position = num;
         for (uint num2 = 0u; num2 < Count; num2++)
         {
             ReadEntry(this.encryptor, out var entryId, out var entryValue, out var entryMetadata);
             SerializeUpdatedEntry(entryId, entryValue, entryMetadata, encryptor);
             journalWriter.WriteWriteEntry(path, num, serializedBytes);
             num += entrySize;
         }
     }
     this.encryptor = encryptor;
 }
 public void ChangeKey(byte[] key)
 {
     lock (transactionLockObject)
     {
         if (isDisposed)
         {
             throw new ObjectDisposedException("Can't use ChangeKey() after Dispose() or Delete()");
         }
         if (isFieldIndexesInUse)
         {
             throw new InvalidOperationException("Can't use ChangeKey() before disposing the enumerable from a FindDocumentIds call");
         }
         if (DebugLogHandler != null)
         {
             DebugLogHandler("ChangeKey: key=" + ((key != null) ? Convert.ToBase64String(key) : "null"));
         }
         EnsureValidKey(key);
         byte[]          initializationVector = CryptoRandomNumberGenerator.GenerateBytes(16u);
         Aes256Encryptor aes256Encryptor      = new Aes256Encryptor(key, initializationVector);
         journalWriter.Start();
         try
         {
             foreach (KeyValuePair <uint, byte[]> document2 in packedFile.Documents)
             {
                 if (document2.Key != metadataDocumentId)
                 {
                     byte[]    value    = document2.Value;
                     TDocument val      = DecryptAndDeserialize(value, document2.Key);
                     byte[]    document = SerializeAndEncrypt(val, aes256Encryptor);
                     packedFile.Update(val.Id, document);
                 }
             }
             WriteMetadataDocument(initializationVector);
             encryptor = aes256Encryptor;
             fieldIndexes.ChangeEncryptor(encryptor);
             journalWriter.Finish();
             journalPlayer.Play();
         }
         catch (Exception)
         {
             journalWriter.Discard();
             throw;
         }
     }
 }
 public DocumentCollection(PackedFile packedFile, IndexFactory indexFactory, byte[] key, JournalPlayer journalPlayer, JournalWriter journalWriter)
 {
     transactionLockObject = new object();
     EnsureValidKey(key);
     documentType = typeof(TDocument);
     try
     {
         journalPlayer.Play();
         SerializerReflectionCache.AddTypes(typeof(MetadataDocument), documentType);
         this.packedFile    = packedFile;
         this.journalPlayer = journalPlayer;
         this.journalWriter = journalWriter;
         byte[] initializationVector;
         if (packedFile.IsEmpty)
         {
             initializationVector = CryptoRandomNumberGenerator.GenerateBytes(16u);
             journalWriter.Start();
             WriteMetadataDocument(initializationVector);
             journalWriter.Finish();
             journalPlayer.Play();
         }
         else
         {
             KeyValuePair <uint, byte[]> keyValuePair = packedFile.Documents.First();
             metadataDocumentId = keyValuePair.Key;
             MetadataDocument metadataDocument = BinarySerializer.Deserialize <MetadataDocument>(keyValuePair.Value);
             initializationVector = metadataDocument.InitializationVector;
         }
         encryptor    = new Aes256Encryptor(key, initializationVector);
         fieldIndexes = new FieldIndexes <TDocument>(indexFactory, encryptor);
     }
     catch (Exception)
     {
         journalWriter.Discard();
         throw;
     }
 }
Esempio n. 11
0
        public Index(string path, IFixedSizeIndexValueType <TEntryValue> valueType, Aes256Encryptor encryptor, IFileSystem fileSystem, JournalWriter journalWriter)
        {
            this.path          = path;
            this.valueType     = valueType;
            this.encryptor     = encryptor;
            this.fileSystem    = fileSystem;
            this.journalWriter = journalWriter;
            uint size = valueType.Size;

            entrySize        = 4 + size;
            fileStream       = fileSystem.OpenFileStream(path);
            serializedBytes  = new byte[entrySize];
            serializerStream = new MemoryStream(serializedBytes);
            serializerWriter = new BinaryWriter(serializerStream);
            if (fileStream.Length >= 1)
            {
                int num = fileStream.ReadByte();
                if (num != 0)
                {
                    throw new CorruptionException("Unsupported format version: " + num);
                }
                uint num2 = (uint)(fileStream.Length - 1);
                Count = num2 / entrySize;
                if (num2 % entrySize != 0)
                {
                    uint entryPosition = GetEntryPosition(Count);
                    fileStream.SetLength(entryPosition);
                }
            }
            else
            {
                fileStream.SetLength(1L);
                fileStream.WriteByte(0);
            }
            fileStreamReader = new BinaryReader(fileStream);
        }
Esempio n. 12
0
 private void SerializeUpdatedEntry(uint entryId, TEntryValue value, uint entryMetadata, Aes256Encryptor encryptor)
 {
     serializerStream.Position = 0L;
     serializerWriter.Write(entryId);
     valueType.UpdateEntryValue(value, entryMetadata, serializerWriter, encryptor);
 }
Esempio n. 13
0
 private void SerializeNewEntry(uint entryId, TEntryValue value, Aes256Encryptor encryptor)
 {
     serializerStream.Position = 0L;
     serializerWriter.Write(entryId);
     valueType.WriteEntryValue(value, serializerWriter, encryptor);
 }
Esempio n. 14
0
 private void ReadEntry(Aes256Encryptor encryptor, out uint entryId, out TEntryValue entryValue, out uint entryMetadata)
 {
     entryId = fileStreamReader.ReadUInt32();
     valueType.ReadEntryValue(fileStreamReader, encryptor, out entryValue, out entryMetadata);
 }
Esempio n. 15
0
 public bool ChangeEncryptor(Aes256Encryptor oldEncryptor, Aes256Encryptor newEncryptor)
 {
     return(true);
 }
Esempio n. 16
0
 public FieldIndexes(IndexFactory indexFactory, Aes256Encryptor encryptor)
 {
     typeReflection = DocumentReflectionCache.GetTypeReflection <TDocument>();
     fieldTypes     = new Dictionary <string, Type>();
     fieldIndexes   = new Dictionary <string, object>();
     boolIndexes    = new Dictionary <string, Index <bool> >();
     sbyteIndexes   = new Dictionary <string, Index <sbyte> >();
     byteIndexes    = new Dictionary <string, Index <byte> >();
     shortIndexes   = new Dictionary <string, Index <short> >();
     ushortIndexes  = new Dictionary <string, Index <ushort> >();
     intIndexes     = new Dictionary <string, Index <int> >();
     uintIndexes    = new Dictionary <string, Index <uint> >();
     longIndexes    = new Dictionary <string, Index <long> >();
     ulongIndexes   = new Dictionary <string, Index <ulong> >();
     floatIndexes   = new Dictionary <string, Index <float> >();
     doubleIndexes  = new Dictionary <string, Index <double> >();
     charIndexes    = new Dictionary <string, Index <char> >();
     stringIndexes  = new Dictionary <string, Index <string> >();
     DocumentFieldReflection[] fieldReflections = typeReflection.FieldReflections;
     foreach (DocumentFieldReflection documentFieldReflection in fieldReflections)
     {
         FieldInfo fieldInfo = documentFieldReflection.FieldInfo;
         string    name      = fieldInfo.Name;
         Type      fieldType = fieldInfo.FieldType;
         fieldTypes.Add(name, fieldType);
         if (fieldType == typeof(bool))
         {
             Index <bool> value = indexFactory.Create <bool>(name, encryptor);
             boolIndexes.Add(name, value);
             fieldIndexes.Add(name, value);
             continue;
         }
         if (fieldType == typeof(sbyte))
         {
             Index <sbyte> value2 = indexFactory.Create <sbyte>(name, encryptor);
             sbyteIndexes.Add(name, value2);
             fieldIndexes.Add(name, value2);
             continue;
         }
         if (fieldType == typeof(byte))
         {
             Index <byte> value3 = indexFactory.Create <byte>(name, encryptor);
             byteIndexes.Add(name, value3);
             fieldIndexes.Add(name, value3);
             continue;
         }
         if (fieldType == typeof(short))
         {
             Index <short> value4 = indexFactory.Create <short>(name, encryptor);
             shortIndexes.Add(name, value4);
             fieldIndexes.Add(name, value4);
             continue;
         }
         if (fieldType == typeof(ushort))
         {
             Index <ushort> value5 = indexFactory.Create <ushort>(name, encryptor);
             ushortIndexes.Add(name, value5);
             fieldIndexes.Add(name, value5);
             continue;
         }
         if (fieldType == typeof(int))
         {
             Index <int> value6 = indexFactory.Create <int>(name, encryptor);
             intIndexes.Add(name, value6);
             fieldIndexes.Add(name, value6);
             continue;
         }
         if (fieldType == typeof(uint))
         {
             Index <uint> value7 = indexFactory.Create <uint>(name, encryptor);
             uintIndexes.Add(name, value7);
             fieldIndexes.Add(name, value7);
             continue;
         }
         if (fieldType == typeof(long))
         {
             Index <long> value8 = indexFactory.Create <long>(name, encryptor);
             longIndexes.Add(name, value8);
             fieldIndexes.Add(name, value8);
             continue;
         }
         if (fieldType == typeof(ulong))
         {
             Index <ulong> value9 = indexFactory.Create <ulong>(name, encryptor);
             ulongIndexes.Add(name, value9);
             fieldIndexes.Add(name, value9);
             continue;
         }
         if (fieldType == typeof(float))
         {
             Index <float> value10 = indexFactory.Create <float>(name, encryptor);
             floatIndexes.Add(name, value10);
             fieldIndexes.Add(name, value10);
             continue;
         }
         if (fieldType == typeof(double))
         {
             Index <double> value11 = indexFactory.Create <double>(name, encryptor);
             doubleIndexes.Add(name, value11);
             fieldIndexes.Add(name, value11);
             continue;
         }
         if (fieldType == typeof(char))
         {
             Index <char> value12 = indexFactory.Create <char>(name, encryptor);
             charIndexes.Add(name, value12);
             fieldIndexes.Add(name, value12);
             continue;
         }
         if (fieldType == typeof(string))
         {
             Index <string> value13 = indexFactory.Create <string>(name, encryptor);
             stringIndexes.Add(name, value13);
             fieldIndexes.Add(name, value13);
             continue;
         }
         throw new FormatException("Unhandled field " + name + " with type " + fieldType);
     }
 }
 private byte[] SerializeAndEncrypt(TDocument document, Aes256Encryptor encryptor)
 {
     byte[] bytes = BinarySerializer.Serialize(document, documentType);
     return(encryptor.Encrypt(bytes));
 }
Esempio n. 18
0
        public Index <TValue> Create <TValue>(string fieldName, Aes256Encryptor encryptor) where TValue : IComparable <TValue>
        {
            string path           = HashedPathGenerator.GetPath(dir, fieldName);
            Type   typeFromHandle = typeof(TValue);

            if (typeFromHandle == typeof(bool))
            {
                BoolIndexValueType valueType = new BoolIndexValueType();
                return((Index <TValue>)(object) new Index <bool>(path, valueType, encryptor, fileSystem, journalWriter));
            }
            if (typeFromHandle == typeof(sbyte))
            {
                SByteIndexValueType valueType2 = new SByteIndexValueType();
                return((Index <TValue>)(object) new Index <sbyte>(path, valueType2, encryptor, fileSystem, journalWriter));
            }
            if (typeFromHandle == typeof(byte))
            {
                ByteIndexValueType valueType3 = new ByteIndexValueType();
                return((Index <TValue>)(object) new Index <byte>(path, valueType3, encryptor, fileSystem, journalWriter));
            }
            if (typeFromHandle == typeof(short))
            {
                ShortIndexValueType valueType4 = new ShortIndexValueType();
                return((Index <TValue>)(object) new Index <short>(path, valueType4, encryptor, fileSystem, journalWriter));
            }
            if (typeFromHandle == typeof(ushort))
            {
                UShortIndexValueType valueType5 = new UShortIndexValueType();
                return((Index <TValue>)(object) new Index <ushort>(path, valueType5, encryptor, fileSystem, journalWriter));
            }
            if (typeFromHandle == typeof(int))
            {
                IntIndexValueType valueType6 = new IntIndexValueType();
                return((Index <TValue>)(object) new Index <int>(path, valueType6, encryptor, fileSystem, journalWriter));
            }
            if (typeFromHandle == typeof(uint))
            {
                UIntIndexValueType valueType7 = new UIntIndexValueType();
                return((Index <TValue>)(object) new Index <uint>(path, valueType7, encryptor, fileSystem, journalWriter));
            }
            if (typeFromHandle == typeof(long))
            {
                LongIndexValueType valueType8 = new LongIndexValueType();
                return((Index <TValue>)(object) new Index <long>(path, valueType8, encryptor, fileSystem, journalWriter));
            }
            if (typeFromHandle == typeof(ulong))
            {
                ULongIndexValueType valueType9 = new ULongIndexValueType();
                return((Index <TValue>)(object) new Index <ulong>(path, valueType9, encryptor, fileSystem, journalWriter));
            }
            if (typeFromHandle == typeof(float))
            {
                FloatIndexValueType valueType10 = new FloatIndexValueType();
                return((Index <TValue>)(object) new Index <float>(path, valueType10, encryptor, fileSystem, journalWriter));
            }
            if (typeFromHandle == typeof(double))
            {
                DoubleIndexValueType valueType11 = new DoubleIndexValueType();
                return((Index <TValue>)(object) new Index <double>(path, valueType11, encryptor, fileSystem, journalWriter));
            }
            if (typeFromHandle == typeof(char))
            {
                CharIndexValueType valueType12 = new CharIndexValueType();
                return((Index <TValue>)(object) new Index <char>(path, valueType12, encryptor, fileSystem, journalWriter));
            }
            if (typeFromHandle == typeof(string))
            {
                StringIndexValueType indexValueType = new StringIndexValueType();
                string     path2      = HashedPathGenerator.GetPath(dir, fieldName + "_strings");
                string     path3      = HashedPathGenerator.GetPath(dir, fieldName + "_stringsMeta");
                PackedFile packedFile = new PackedFile(path2, path3, journalWriter, fileSystem);
                PackedFileIndexValueType <string> valueType13 = new PackedFileIndexValueType <string>(indexValueType, packedFile);
                return((Index <TValue>)(object) new Index <string>(path, valueType13, encryptor, fileSystem, journalWriter));
            }
            throw new FormatException("Unhandled indexed field type: " + typeFromHandle);
        }
Esempio n. 19
0
 public void UpdateEntryValue(TEntryValue entryValue, uint metadata, BinaryWriter writer, Aes256Encryptor encryptor)
 {
     byte[] bytes  = Serialize(entryValue);
     byte[] buffer = encryptor.Encrypt(bytes);
     writer.Write(buffer);
 }