private void OnBaseAcceptorClientConnect(object sender, Socket acceptedSocket) { TCPPacketConnection connection = new TCPPacketConnection(acceptedSocket); connection.Logger = Logger; if (!connection.InitializeCrypto(CryptoFactory.CreateCrypto(Crypto, false), CryptoHandshakeTimeout)) { return; } SecureConnectionEstablished.Invoke(this, connection); }
protected override TCPPacketConnection InitializeConnection(Socket connectedSocket) { TCPPacketConnection connection = new TCPPacketConnection(connectedSocket); connection.Logger = Logger; Logger.Log(LogLevel.Information, MessageInitiatingHandshake); if (!connection.InitializeCrypto(CryptoFactory.CreateCrypto(Crypto, true), TimeoutCryptoHandshake)) { throw new ConnectException(MessageHandshakeFailed); } return(connection); }
public static Aes ReadMetadataAndInitializeAlgorithm(Stream inputStream, string password) { byte[] iv; using (BinaryReader br = new BinaryReader(inputStream, Encoding.UTF8, true)) { int ivLength = br.ReadInt32(); iv = new byte[ivLength]; int readBytes = br.Read(iv, 0, iv.Length); if (readBytes != iv.Length) { throw new InvalidDataException($"Unable to read IV: expected to read {iv.Length} bytes, but got {readBytes}"); } } return(CryptoFactory.CreateCrypto(password, iv)); }
private async Task StoreDataAsync(FileIdentifier id, Stream dataStream, UploadPassword passwordSetting, CancellationToken cancellationToken) { UploadProgress progress = this.GetProgressObject(id); // Copy with progress using (Stream outputStream = this._fileWriter.OpenWriteStream(this._fileStore.GetDataFile(id))) { if (passwordSetting.Enable == true && !String.IsNullOrEmpty(passwordSetting.Password)) { using (Aes crypto = CryptoFactory.CreateCrypto(passwordSetting.Password)) { ICryptoTransform encryptor = crypto.CreateEncryptor(); CryptoMetadata.WriteMetadata(outputStream, crypto); using (CryptoStream cryptoStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write, true)) { await CopyStreamWithProgress(cryptoStream); } } } else { await CopyStreamWithProgress(outputStream); } } async Task CopyStreamWithProgress(Stream outputStream) { using (Stream inputStream = dataStream) { int read; byte[] buffer = new byte[4096]; while ((read = await inputStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0) { progress.Current += read; await outputStream.WriteAsync(buffer, 0, read, cancellationToken).ConfigureAwait(false); } } } }