io.go 813 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package crypto
  2. import (
  3. "crypto/cipher"
  4. "io"
  5. )
  6. type cryptionReader struct {
  7. stream cipher.Stream
  8. reader io.Reader
  9. }
  10. func NewCryptionReader(stream cipher.Stream, reader io.Reader) io.Reader {
  11. return &cryptionReader{
  12. stream: stream,
  13. reader: reader,
  14. }
  15. }
  16. func (this *cryptionReader) Read(data []byte) (int, error) {
  17. nBytes, err := this.reader.Read(data)
  18. if nBytes > 0 {
  19. this.stream.XORKeyStream(data[:nBytes], data[:nBytes])
  20. }
  21. return nBytes, err
  22. }
  23. type cryptionWriter struct {
  24. stream cipher.Stream
  25. writer io.Writer
  26. }
  27. func NewCryptionWriter(stream cipher.Stream, writer io.Writer) io.Writer {
  28. return &cryptionWriter{
  29. stream: stream,
  30. writer: writer,
  31. }
  32. }
  33. func (this *cryptionWriter) Write(data []byte) (int, error) {
  34. this.stream.XORKeyStream(data, data)
  35. return this.writer.Write(data)
  36. }