|
@@ -7,13 +7,13 @@ import (
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-// CryptionReader is a general purpose reader that applies
|
|
|
|
|
-// block cipher on top of a regular reader.
|
|
|
|
|
|
|
+// CryptionReader is a general purpose reader that applies a stream cipher on top of a regular reader.
|
|
|
type CryptionReader struct {
|
|
type CryptionReader struct {
|
|
|
stream cipher.Stream
|
|
stream cipher.Stream
|
|
|
reader io.Reader
|
|
reader io.Reader
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// NewCryptionReader creates a new CryptionReader instance from given stream cipher and reader.
|
|
|
func NewCryptionReader(stream cipher.Stream, reader io.Reader) *CryptionReader {
|
|
func NewCryptionReader(stream cipher.Stream, reader io.Reader) *CryptionReader {
|
|
|
return &CryptionReader{
|
|
return &CryptionReader{
|
|
|
stream: stream,
|
|
stream: stream,
|
|
@@ -21,8 +21,7 @@ func NewCryptionReader(stream cipher.Stream, reader io.Reader) *CryptionReader {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Read reads blocks from underlying reader, the length of blocks must be
|
|
|
|
|
-// a multiply of BlockSize()
|
|
|
|
|
|
|
+// Read reads blocks from underlying reader, and crypt it. The content of blocks is modified in place.
|
|
|
func (reader CryptionReader) Read(blocks []byte) (int, error) {
|
|
func (reader CryptionReader) Read(blocks []byte) (int, error) {
|
|
|
nBytes, err := reader.reader.Read(blocks)
|
|
nBytes, err := reader.reader.Read(blocks)
|
|
|
if nBytes > 0 {
|
|
if nBytes > 0 {
|
|
@@ -34,13 +33,13 @@ func (reader CryptionReader) Read(blocks []byte) (int, error) {
|
|
|
return nBytes, err
|
|
return nBytes, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Cryption writer is a general purpose of byte stream writer that applies
|
|
|
|
|
-// block cipher on top of a regular writer.
|
|
|
|
|
|
|
+// Cryption writer is a general purpose of byte stream writer that applies a stream cipher on top of a regular writer.
|
|
|
type CryptionWriter struct {
|
|
type CryptionWriter struct {
|
|
|
stream cipher.Stream
|
|
stream cipher.Stream
|
|
|
writer io.Writer
|
|
writer io.Writer
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// NewCryptionWriter creates a new CryptionWriter from given stream cipher and writer.
|
|
|
func NewCryptionWriter(stream cipher.Stream, writer io.Writer) *CryptionWriter {
|
|
func NewCryptionWriter(stream cipher.Stream, writer io.Writer) *CryptionWriter {
|
|
|
return &CryptionWriter{
|
|
return &CryptionWriter{
|
|
|
stream: stream,
|
|
stream: stream,
|
|
@@ -48,12 +47,12 @@ func NewCryptionWriter(stream cipher.Stream, writer io.Writer) *CryptionWriter {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Crypt crypts the content of blocks without writing them into the underlying writer.
|
|
|
func (writer CryptionWriter) Crypt(blocks []byte) {
|
|
func (writer CryptionWriter) Crypt(blocks []byte) {
|
|
|
writer.stream.XORKeyStream(blocks, blocks)
|
|
writer.stream.XORKeyStream(blocks, blocks)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Write writes the give blocks to underlying writer. The length of the blocks
|
|
|
|
|
-// must be a multiply of BlockSize()
|
|
|
|
|
|
|
+// Write crypts the content of blocks in place, and then writes the give blocks to underlying writer.
|
|
|
func (writer CryptionWriter) Write(blocks []byte) (int, error) {
|
|
func (writer CryptionWriter) Write(blocks []byte) (int, error) {
|
|
|
writer.Crypt(blocks)
|
|
writer.Crypt(blocks)
|
|
|
return writer.writer.Write(blocks)
|
|
return writer.writer.Write(blocks)
|