1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 09:36:34 -05:00

format code

This commit is contained in:
V2Ray 2015-09-08 18:21:33 +02:00
parent 86944af2ff
commit 099015021e
4 changed files with 39 additions and 39 deletions

View File

@ -1,65 +1,65 @@
package io package io
import ( import (
"crypto/cipher" "crypto/cipher"
"io" "io"
) )
// CryptionReader is a general purpose reader that applies // CryptionReader is a general purpose reader that applies
// block cipher on top of a regular reader. // block cipher on top of a regular reader.
type CryptionReader struct { type CryptionReader struct {
mode cipher.BlockMode mode cipher.BlockMode
reader io.Reader reader io.Reader
} }
func NewCryptionReader(mode cipher.BlockMode, reader io.Reader) *CryptionReader { func NewCryptionReader(mode cipher.BlockMode, reader io.Reader) *CryptionReader {
this := new(CryptionReader) this := new(CryptionReader)
this.mode = mode this.mode = mode
this.reader = reader this.reader = reader
return this return this
} }
// Read reads blocks from underlying reader, the length of blocks must be // Read reads blocks from underlying reader, the length of blocks must be
// a multiply of BlockSize() // a multiply of BlockSize()
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 err != nil && err != io.EOF { if err != nil && err != io.EOF {
return nBytes, err return nBytes, err
} }
if nBytes < len(blocks) { if nBytes < len(blocks) {
for i, _ := range blocks[nBytes:] { for i, _ := range blocks[nBytes:] {
blocks[i] = 0 blocks[i] = 0
} }
} }
reader.mode.CryptBlocks(blocks, blocks) reader.mode.CryptBlocks(blocks, blocks)
return nBytes, err return nBytes, err
} }
func (reader CryptionReader) BlockSize() int { 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 // Cryption writer is a general purpose of byte stream writer that applies
// block cipher on top of a regular writer. // block cipher on top of a regular writer.
type CryptionWriter struct { type CryptionWriter struct {
mode cipher.BlockMode mode cipher.BlockMode
writer io.Writer writer io.Writer
} }
func NewCryptionWriter(mode cipher.BlockMode, writer io.Writer) *CryptionWriter { func NewCryptionWriter(mode cipher.BlockMode, writer io.Writer) *CryptionWriter {
this := new(CryptionWriter) this := new(CryptionWriter)
this.mode = mode this.mode = mode
this.writer = writer this.writer = writer
return this return this
} }
// Write writes the give blocks to underlying writer. The length of the blocks // Write writes the give blocks to underlying writer. The length of the blocks
// must be a multiply of BlockSize() // must be a multiply of BlockSize()
func (writer CryptionWriter) Write(blocks []byte) (int, error) { func (writer CryptionWriter) Write(blocks []byte) (int, error) {
writer.mode.CryptBlocks(blocks, blocks) writer.mode.CryptBlocks(blocks, blocks)
return writer.writer.Write(blocks) return writer.writer.Write(blocks)
} }
func (writer CryptionWriter) BlockSize() int { func (writer CryptionWriter) BlockSize() int {
return writer.mode.BlockSize() return writer.mode.BlockSize()
} }

View File

@ -6,8 +6,8 @@ import (
"crypto/cipher" "crypto/cipher"
"fmt" "fmt"
"io" "io"
v2io "github.com/v2ray/v2ray-core/io" v2io "github.com/v2ray/v2ray-core/io"
) )
const ( const (
@ -17,7 +17,7 @@ const (
// DecryptionReader is a byte stream reader to decrypt AES-128 CBC (for now) // DecryptionReader is a byte stream reader to decrypt AES-128 CBC (for now)
// encrypted content. // encrypted content.
type DecryptionReader struct { type DecryptionReader struct {
reader *v2io.CryptionReader reader *v2io.CryptionReader
buffer *bytes.Buffer buffer *bytes.Buffer
} }
@ -29,7 +29,7 @@ func NewDecryptionReader(reader io.Reader, key []byte, iv []byte) (*DecryptionRe
if err != nil { if err != nil {
return nil, err return nil, err
} }
aesBlockMode := cipher.NewCBCDecrypter(aesCipher, iv) aesBlockMode := cipher.NewCBCDecrypter(aesCipher, iv)
decryptionReader.reader = v2io.NewCryptionReader(aesBlockMode, reader) decryptionReader.reader = v2io.NewCryptionReader(aesBlockMode, reader)
decryptionReader.buffer = bytes.NewBuffer(make([]byte, 0, 2*blockSize)) decryptionReader.buffer = bytes.NewBuffer(make([]byte, 0, 2*blockSize))
return decryptionReader, nil return decryptionReader, nil

View File

@ -3,7 +3,7 @@ package vmess
import ( import (
"bytes" "bytes"
"crypto/aes" "crypto/aes"
"crypto/cipher" "crypto/cipher"
"crypto/rand" "crypto/rand"
mrand "math/rand" mrand "math/rand"
"testing" "testing"
@ -27,17 +27,17 @@ func TestNormalReading(t *testing.T) {
keySize := 16 keySize := 16
key := make([]byte, keySize) key := make([]byte, keySize)
randomBytes(key, t) randomBytes(key, t)
iv := make([]byte, keySize) iv := make([]byte, keySize)
randomBytes(iv, t) randomBytes(iv, t)
aesBlock, err := aes.NewCipher(key) aesBlock, err := aes.NewCipher(key)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
aesMode := cipher.NewCBCEncrypter(aesBlock, iv) aesMode := cipher.NewCBCEncrypter(aesBlock, iv)
ciphertext := make([]byte, testSize) ciphertext := make([]byte, testSize)
aesMode.CryptBlocks(ciphertext, plaintext) aesMode.CryptBlocks(ciphertext, plaintext)
ciphertextcopy := make([]byte, testSize) ciphertextcopy := make([]byte, testSize)
copy(ciphertextcopy, ciphertext) copy(ciphertextcopy, ciphertext)

View File

@ -14,7 +14,7 @@ type VMessInput struct {
userHash [16]byte userHash [16]byte
respKey [16]byte respKey [16]byte
iv [16]byte iv [16]byte
respHead [4]byte respHead [4]byte
command byte command byte
port uint16 port uint16
target [256]byte target [256]byte