public ChaCha20Stream(Stream underlying, byte[] key) : base(underlying) { _key = key; _chachaEncrypt = new ChaCha20(_key, _nonceLen); Init(Send, Read); }
int Read(Stream underlying, byte[] buf, int pos, int cnt) { var r = underlying.Read(_readBuffer, 0, Math.Min(cnt, _readBuffer.Length)); if (r == 0) { return(0); } var ro = 0; if (!_icReceived) { var remoteNonce = new byte[_nonceLen]; Array.Copy(_readBuffer, 0, remoteNonce, 0, _nonceLen); _icReceived = true; r -= _nonceLen; ro += _nonceLen; _chachaDecrypt = new ChaCha20(_key, remoteNonce); } _chachaDecrypt.Crypt(_readBuffer, ro, buf, pos, r); // Trace.WriteLine("DEC << " + Utils.Utf8.GetString(buf, pos, Math.Max(r, 40))); return(r); }
public ChaCha20Stream(Stream underlying, string password) : this(underlying, ChaCha20.Kdf(password)) { }