From 099015021eadd545809022b8e287bb8d345fc88a Mon Sep 17 00:00:00 2001 From: V2Ray Date: Tue, 8 Sep 2015 18:21:33 +0200 Subject: [PATCH] format code --- io/encryption.go | 58 +++++++++++++++---------------- io/vmess/decryptionreader.go | 8 ++--- io/vmess/decryptionreader_test.go | 10 +++--- io/vmess/vmess.go | 2 +- 4 files changed, 39 insertions(+), 39 deletions(-) diff --git a/io/encryption.go b/io/encryption.go index 3fe178489..75aa52c43 100644 --- a/io/encryption.go +++ b/io/encryption.go @@ -1,65 +1,65 @@ package io import ( - "crypto/cipher" - "io" + "crypto/cipher" + "io" ) // CryptionReader is a general purpose reader that applies // block cipher on top of a regular reader. type CryptionReader struct { - mode cipher.BlockMode - reader io.Reader + mode cipher.BlockMode + reader io.Reader } func NewCryptionReader(mode cipher.BlockMode, reader io.Reader) *CryptionReader { - this := new(CryptionReader) - this.mode = mode - this.reader = reader - return this + this := new(CryptionReader) + this.mode = mode + this.reader = reader + return this } // Read reads blocks from underlying reader, the length of blocks must be // a multiply of BlockSize() func (reader CryptionReader) Read(blocks []byte) (int, error) { - nBytes, err := reader.reader.Read(blocks) - if err != nil && err != io.EOF { - return nBytes, err - } - if nBytes < len(blocks) { - for i, _ := range blocks[nBytes:] { - blocks[i] = 0 - } - } - reader.mode.CryptBlocks(blocks, blocks) - return nBytes, err + nBytes, err := reader.reader.Read(blocks) + if err != nil && err != io.EOF { + return nBytes, err + } + if nBytes < len(blocks) { + for i, _ := range blocks[nBytes:] { + blocks[i] = 0 + } + } + reader.mode.CryptBlocks(blocks, blocks) + return nBytes, err } func (reader CryptionReader) BlockSize() int { - return reader.mode.BlockSize() + return reader.mode.BlockSize() } // Cryption writer is a general purpose of byte stream writer that applies // block cipher on top of a regular writer. type CryptionWriter struct { - mode cipher.BlockMode - writer io.Writer + mode cipher.BlockMode + writer io.Writer } func NewCryptionWriter(mode cipher.BlockMode, writer io.Writer) *CryptionWriter { - this := new(CryptionWriter) - this.mode = mode - this.writer = writer - return this + this := new(CryptionWriter) + this.mode = mode + this.writer = writer + return this } // Write writes the give blocks to underlying writer. The length of the blocks // must be a multiply of BlockSize() func (writer CryptionWriter) Write(blocks []byte) (int, error) { - writer.mode.CryptBlocks(blocks, blocks) - return writer.writer.Write(blocks) + writer.mode.CryptBlocks(blocks, blocks) + return writer.writer.Write(blocks) } func (writer CryptionWriter) BlockSize() int { - return writer.mode.BlockSize() + return writer.mode.BlockSize() } diff --git a/io/vmess/decryptionreader.go b/io/vmess/decryptionreader.go index 69932c1a5..2ee0a6543 100644 --- a/io/vmess/decryptionreader.go +++ b/io/vmess/decryptionreader.go @@ -6,8 +6,8 @@ import ( "crypto/cipher" "fmt" "io" - - v2io "github.com/v2ray/v2ray-core/io" + + v2io "github.com/v2ray/v2ray-core/io" ) const ( @@ -17,7 +17,7 @@ const ( // DecryptionReader is a byte stream reader to decrypt AES-128 CBC (for now) // encrypted content. type DecryptionReader struct { - reader *v2io.CryptionReader + reader *v2io.CryptionReader buffer *bytes.Buffer } @@ -29,7 +29,7 @@ func NewDecryptionReader(reader io.Reader, key []byte, iv []byte) (*DecryptionRe if err != nil { return nil, err } - aesBlockMode := cipher.NewCBCDecrypter(aesCipher, iv) + aesBlockMode := cipher.NewCBCDecrypter(aesCipher, iv) decryptionReader.reader = v2io.NewCryptionReader(aesBlockMode, reader) decryptionReader.buffer = bytes.NewBuffer(make([]byte, 0, 2*blockSize)) return decryptionReader, nil diff --git a/io/vmess/decryptionreader_test.go b/io/vmess/decryptionreader_test.go index 340b549cb..feec3035c 100644 --- a/io/vmess/decryptionreader_test.go +++ b/io/vmess/decryptionreader_test.go @@ -3,7 +3,7 @@ package vmess import ( "bytes" "crypto/aes" - "crypto/cipher" + "crypto/cipher" "crypto/rand" mrand "math/rand" "testing" @@ -27,17 +27,17 @@ func TestNormalReading(t *testing.T) { keySize := 16 key := make([]byte, keySize) randomBytes(key, t) - iv := make([]byte, keySize) - randomBytes(iv, t) + iv := make([]byte, keySize) + randomBytes(iv, t) aesBlock, err := aes.NewCipher(key) if err != nil { t.Fatal(err) } - aesMode := cipher.NewCBCEncrypter(aesBlock, iv) + aesMode := cipher.NewCBCEncrypter(aesBlock, iv) ciphertext := make([]byte, testSize) - aesMode.CryptBlocks(ciphertext, plaintext) + aesMode.CryptBlocks(ciphertext, plaintext) ciphertextcopy := make([]byte, testSize) copy(ciphertextcopy, ciphertext) diff --git a/io/vmess/vmess.go b/io/vmess/vmess.go index eb46c54e5..d597b19a8 100644 --- a/io/vmess/vmess.go +++ b/io/vmess/vmess.go @@ -14,7 +14,7 @@ type VMessInput struct { userHash [16]byte respKey [16]byte iv [16]byte - respHead [4]byte + respHead [4]byte command byte port uint16 target [256]byte