1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 01:27:03 -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
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()
}

View File

@ -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

View File

@ -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)

View File

@ -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