From 79171f105a0ddcd00f75547ea09e03b4736a9936 Mon Sep 17 00:00:00 2001 From: v2ray Date: Thu, 25 Feb 2016 21:53:21 +0100 Subject: [PATCH] move CryptionReader and Writer into a separate file --- common/crypto/aes.go | 37 ------------------------------------- common/crypto/io.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 common/crypto/io.go diff --git a/common/crypto/aes.go b/common/crypto/aes.go index 3625c0c68..767c8e3a0 100644 --- a/common/crypto/aes.go +++ b/common/crypto/aes.go @@ -15,40 +15,3 @@ func NewAesEncryptionStream(key []byte, iv []byte) cipher.Stream { aesBlock, _ := aes.NewCipher(key) return cipher.NewCFBEncrypter(aesBlock, iv) } - -type cryptionReader struct { - stream cipher.Stream - reader io.Reader -} - -func NewCryptionReader(stream cipher.Stream, reader io.Reader) io.Reader { - return &cryptionReader{ - stream: stream, - reader: reader, - } -} - -func (this *cryptionReader) Read(data []byte) (int, error) { - nBytes, err := this.reader.Read(data) - if nBytes > 0 { - this.stream.XORKeyStream(data[:nBytes], data[:nBytes]) - } - return nBytes, err -} - -type cryptionWriter struct { - stream cipher.Stream - writer io.Writer -} - -func NewCryptionWriter(stream cipher.Stream, writer io.Writer) io.Writer { - return &cryptionWriter{ - stream: stream, - writer: writer, - } -} - -func (this *cryptionWriter) Write(data []byte) (int, error) { - this.stream.XORKeyStream(data, data) - return this.writer.Write(data) -} diff --git a/common/crypto/io.go b/common/crypto/io.go new file mode 100644 index 000000000..887532556 --- /dev/null +++ b/common/crypto/io.go @@ -0,0 +1,43 @@ +package crypto + +import ( + "crypto/cipher" + "io" +) + +type cryptionReader struct { + stream cipher.Stream + reader io.Reader +} + +func NewCryptionReader(stream cipher.Stream, reader io.Reader) io.Reader { + return &cryptionReader{ + stream: stream, + reader: reader, + } +} + +func (this *cryptionReader) Read(data []byte) (int, error) { + nBytes, err := this.reader.Read(data) + if nBytes > 0 { + this.stream.XORKeyStream(data[:nBytes], data[:nBytes]) + } + return nBytes, err +} + +type cryptionWriter struct { + stream cipher.Stream + writer io.Writer +} + +func NewCryptionWriter(stream cipher.Stream, writer io.Writer) io.Writer { + return &cryptionWriter{ + stream: stream, + writer: writer, + } +} + +func (this *cryptionWriter) Write(data []byte) (int, error) { + this.stream.XORKeyStream(data, data) + return this.writer.Write(data) +}