Skip to content

Commit

Permalink
add support and test for chacha20 encrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
defia committed Feb 3, 2015
1 parent 46c507b commit 45e1575
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 9 additions & 2 deletions shadowsocks/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package shadowsocks

import (
"bytes"
"golang.org/x/crypto/blowfish"
"golang.org/x/crypto/cast5"
"crypto/aes"
"crypto/cipher"
"crypto/des"
Expand All @@ -13,6 +11,10 @@ import (
"encoding/binary"
"errors"
"io"

"github.com/codahale/chacha20"
"golang.org/x/crypto/blowfish"
"golang.org/x/crypto/cast5"
)

var errEmptyPassword = errors.New("empty key")
Expand Down Expand Up @@ -137,6 +139,10 @@ func newRC4MD5Stream(key, iv []byte, _ DecOrEnc) (cipher.Stream, error) {
return rc4.NewCipher(rc4key)
}

func newChaCha20Stream(key, iv []byte, _ DecOrEnc) (cipher.Stream, error) {
return chacha20.New(key, iv)
}

type cipherInfo struct {
keyLen int
ivLen int
Expand All @@ -153,6 +159,7 @@ var cipherMethod = map[string]*cipherInfo{
"rc4-md5": {16, 16, newRC4MD5Stream},
"rc4": {16, 0, nil},
"table": {16, 0, nil},
"chacha20": {32, 8, newChaCha20Stream},
}

func CheckCipherMethod(method string) error {
Expand Down
9 changes: 9 additions & 0 deletions shadowsocks/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ func TestRC4MD5(t *testing.T) {
testBlockCipher(t, "rc4-md5")
}

func TestChaCha20(t *testing.T) {
testBlockCipher(t, "chacha20")
}

var cipherKey = make([]byte, 64)

func init() {
Expand Down Expand Up @@ -200,3 +204,8 @@ func BenchmarkRC4MD5Init(b *testing.B) {
ci := cipherMethod["rc4-md5"]
benchmarkCipherInit(b, ci)
}

func BenchmarkChaCha20Init(b *testing.B) {
ci := cipherMethod["chacha20"]
benchmarkCipherInit(b, ci)
}

0 comments on commit 45e1575

Please sign in to comment.