2016-01-27 11:46:40 +00:00
|
|
|
package shadowsocks
|
|
|
|
|
|
|
|
import (
|
2016-02-23 17:16:13 +00:00
|
|
|
"crypto/cipher"
|
2016-01-28 11:33:58 +00:00
|
|
|
"crypto/md5"
|
2016-01-27 11:46:40 +00:00
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/common/crypto"
|
2016-02-03 11:18:28 +00:00
|
|
|
"github.com/v2ray/v2ray-core/common/protocol"
|
2016-01-27 11:46:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Cipher interface {
|
|
|
|
KeySize() int
|
|
|
|
IVSize() int
|
2016-02-23 17:16:13 +00:00
|
|
|
NewEncodingStream(key []byte, iv []byte) (cipher.Stream, error)
|
|
|
|
NewDecodingStream(key []byte, iv []byte) (cipher.Stream, error)
|
2016-01-27 11:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AesCfb struct {
|
|
|
|
KeyBytes int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AesCfb) KeySize() int {
|
|
|
|
return this.KeyBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AesCfb) IVSize() int {
|
|
|
|
return 16
|
|
|
|
}
|
|
|
|
|
2016-02-23 17:16:13 +00:00
|
|
|
func (this *AesCfb) NewEncodingStream(key []byte, iv []byte) (cipher.Stream, error) {
|
2016-02-25 20:50:10 +00:00
|
|
|
stream := crypto.NewAesEncryptionStream(key, iv)
|
2016-02-23 17:16:13 +00:00
|
|
|
return stream, nil
|
2016-01-27 11:46:40 +00:00
|
|
|
}
|
|
|
|
|
2016-02-23 17:16:13 +00:00
|
|
|
func (this *AesCfb) NewDecodingStream(key []byte, iv []byte) (cipher.Stream, error) {
|
2016-02-25 20:50:10 +00:00
|
|
|
stream := crypto.NewAesDecryptionStream(key, iv)
|
2016-02-23 17:16:13 +00:00
|
|
|
return stream, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChaCha20 struct {
|
|
|
|
IVBytes int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ChaCha20) KeySize() int {
|
|
|
|
return 32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ChaCha20) IVSize() int {
|
|
|
|
return this.IVBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ChaCha20) NewEncodingStream(key []byte, iv []byte) (cipher.Stream, error) {
|
|
|
|
return crypto.NewChaCha20Stream(key, iv), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ChaCha20) NewDecodingStream(key []byte, iv []byte) (cipher.Stream, error) {
|
|
|
|
return crypto.NewChaCha20Stream(key, iv), nil
|
2016-01-27 11:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2016-01-28 11:33:58 +00:00
|
|
|
Cipher Cipher
|
|
|
|
Key []byte
|
|
|
|
UDP bool
|
2016-02-03 11:18:28 +00:00
|
|
|
Level protocol.UserLevel
|
2016-02-28 13:50:30 +00:00
|
|
|
Email string
|
2016-01-28 11:33:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func PasswordToCipherKey(password string, keySize int) []byte {
|
|
|
|
pwdBytes := []byte(password)
|
|
|
|
key := make([]byte, 0, keySize)
|
|
|
|
|
|
|
|
md5Sum := md5.Sum(pwdBytes)
|
|
|
|
key = append(key, md5Sum[:]...)
|
|
|
|
|
|
|
|
for len(key) < keySize {
|
|
|
|
md5Hash := md5.New()
|
|
|
|
md5Hash.Write(md5Sum[:])
|
|
|
|
md5Hash.Write(pwdBytes)
|
|
|
|
md5Hash.Sum(md5Sum[:0])
|
|
|
|
|
|
|
|
key = append(key, md5Sum[:]...)
|
|
|
|
}
|
|
|
|
return key
|
2016-01-27 11:46:40 +00:00
|
|
|
}
|