/// <summary> /// Decrypts the array block. /// </summary> /// <param name="data">The data.</param> /// <returns></returns> public byte[] DecryptArrayBlock(byte[] data) { var bBloblSize = new byte[Marshal.SizeOf(typeof(int))]; byte[] bEncryptedNonce; byte[] bEncryptedData; using (var ms = new MemoryStream(data)) { ms.Read(bBloblSize, 0, bBloblSize.Length); bEncryptedNonce = new byte[BitConverter.ToInt32(bBloblSize, 0)]; ms.Read(bEncryptedNonce, 0, bEncryptedNonce.Length); ms.Read(bBloblSize, 0, bBloblSize.Length); bEncryptedData = new byte[BitConverter.ToInt32(bBloblSize, 0)]; ms.Read(bEncryptedData, 0, bEncryptedData.Length); } NonceContainer nonceContainer; using (var ff = new FastBinaryFormatter()) { nonceContainer = ff.Deserialize <NonceContainer>(DecryptBytes(bEncryptedNonce)); } return(CryptoServices.AesDecrypt(bEncryptedData, nonceContainer.Nonce)); }
public void SerializaAndDeserializeAllObjectTypes() { INamedEntity[] serializedObject = BuildAlbumArray(); var formatter = new FastBinaryFormatter(); var surrogateSelector = new SurrogateSelector(); surrogateSelector.AddSurrogate(typeof(Genre), new StreamingContext(), new GenreSerializationSurrogate()); formatter.SurrogateSelector.ChainSelector(surrogateSelector); using (var stream = new MemoryStream()) { formatter.Serialize(stream, serializedObject); stream.Seek(0, SeekOrigin.Begin); var deserializedObject = (INamedEntity[])formatter.Deserialize(stream); Assert.AreEqual(stream.Position, stream.Length); foreach (Album album in deserializedObject) { var albumGenre = album.Genre; if (albumGenre != null) { Assert.AreSame(albumGenre, Genre.Get(albumGenre.Key)); } Assert.AreEqual(album.Edition.MajorNumber, 1); Assert.AreEqual(album.Edition.ReleaseDate, new DateTime(2017, 11, 06)); } } }
/// <summary> /// Encrypts the array block. /// </summary> /// <param name="data">The data.</param> /// <returns></returns> public byte[] EncryptArrayBlock(byte[] data) { var nonceContainer = new NonceContainer(CryptoServices.CreateNonce(128)); byte[] bEncryptedNonce; using (var ff = new FastBinaryFormatter()) { bEncryptedNonce = EncryptBytes(ff.Serialize(nonceContainer)); } var bEncryptedData = CryptoServices.AesEncrypt(data, nonceContainer.Nonce); using (var ms = new MemoryStream()) { var bBlobSize = BitConverter.GetBytes(bEncryptedNonce.Length); ms.Write(bBlobSize, 0, bBlobSize.Length); ms.Write(bEncryptedNonce, 0, bEncryptedNonce.Length); bBlobSize = BitConverter.GetBytes(bEncryptedData.Length); ms.Write(bBlobSize, 0, bBlobSize.Length); ms.Write(bEncryptedData, 0, bEncryptedData.Length); return(ms.ToArray()); } }
public T GetExtraData <T>(FastBinaryFormatter ff) where T : IFastBinarySerializable { if (FExtraData == null) { return(default(T)); } return(FExtraData.GetObject <T>(ff)); }
public void DiscoverMissingSerializableAttribute() { var serializedObject = this; var formatter = new FastBinaryFormatter(); using (var stream = new MemoryStream()) { formatter.Serialize(stream, serializedObject); } }
public void SetExtraData(FastBinaryFormatter ff, IFastBinarySerializable data) { FExtraData = null; FExtraDataTypeHash = 0; if (data != null) { FExtraData = new FastSerializerObjectData(ff, data); FExtraDataTypeHash = (Int32)data.GetType().Name.BKDRHash(); } }
public void TestDateTime() { var now = DateTime.Now; var formatter = new FastBinaryFormatter(); using (var stream = new MemoryStream()) { formatter.Serialize(stream, now); stream.Seek(0, SeekOrigin.Begin); var deserializedObject = (DateTime)formatter.Deserialize(stream); Assert.AreEqual(deserializedObject, now); Assert.AreEqual(stream.Position, stream.Length); } }
public T GetDetails <T>(FastBinaryFormatter ff) where T : IFastBinarySerializable { T details; if (FDetails != null) { details = FDetails.GetObject <T>(ff); } else { details = default(T); } return(details); }
private void RewriteFileIgnoreRequestor(RIFileHeader newHeader, FastBinaryFormatter ff, UInt32 sessionID, UInt32 requestID) { RIFileHeader tmpHeader = null; String tmpFile = String.Format("{0}.tmp", FFilePath); using (FileStream tmpStream = FileStreamAccess.OpenFileStreamForWriting(tmpFile, FileMode.Create)) { // original file: set file pointer to top FFileStream.Seek(0, SeekOrigin.Begin); ff.Serialize(FFileStream, FFileHeader); // original file: move file pointer to the end of the file header FFileStream.Seek(0, SeekOrigin.Begin); ff.Deserialize <RIFileHeader>(FFileStream); // temp file: move file pointer to the end of the file header with new header info tmpHeader = newHeader; tmpHeader.FInitDateTime = DateTime.MinValue; tmpHeader.FFirstDateTime = DateTime.MinValue; tmpHeader.FLastDateTime = DateTime.MinValue; tmpHeader.FNextSequenceId = FFileHeader.FNextSequenceId; tmpHeader.FMessageCount = 0; ff.Serialize(tmpStream, tmpHeader); // start copying original messages to temp file for (Int32 i = 0; i < FFileHeader.FMessageCount; i++) { ReflectInsightPackage message = ff.Deserialize <ReflectInsightPackage>(FFileStream); if (message.FRequestID == requestID && message.FSessionID == sessionID) { continue; } if (tmpHeader.FInitDateTime == DateTime.MinValue) { tmpHeader.FInitDateTime = message.FDateTime; tmpHeader.FFirstDateTime = message.FDateTime; tmpHeader.FLastDateTime = message.FDateTime; } tmpHeader.FMessageCount++; tmpHeader.FLastDateTime = message.FDateTime; ff.Serialize(tmpStream, message); } // update the FileHeader with the temp file header and write it out // with latest info. FFileHeader = tmpHeader; tmpStream.Seek(0, SeekOrigin.Begin); ff.Serialize(tmpStream, FFileHeader); } if (tmpHeader != null) { // delete the original file first, then copy temp to original DeleteFile(false); try { File.Move(tmpFile, FFilePath); } finally { OpenFileStream(); } } }
} // depending on the request type, this blob can either be a MessageRequest or MessageSequence object public MessageHeader(FastBinaryFormatter ff, MessageRequestType requestType, IFastBinarySerializable request) { RequestType = requestType; Request = new FastSerializerObjectData(ff, request); }