-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
config.go
125 lines (103 loc) · 2.63 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package shadowsocks
import (
"bytes"
"crypto/cipher"
"crypto/md5"
"v2ray.com/core/common/crypto"
"v2ray.com/core/common/protocol"
)
type ShadowsocksAccount struct {
Cipher Cipher
Key []byte
OneTimeAuth Account_OneTimeAuth
}
func (v *ShadowsocksAccount) Equals(another protocol.Account) bool {
if account, ok := another.(*ShadowsocksAccount); ok {
return bytes.Equal(v.Key, account.Key)
}
return false
}
func (v *Account) GetCipher() (Cipher, error) {
switch v.CipherType {
case CipherType_AES_128_CFB:
return &AesCfb{KeyBytes: 16}, nil
case CipherType_AES_256_CFB:
return &AesCfb{KeyBytes: 32}, nil
case CipherType_CHACHA20:
return &ChaCha20{IVBytes: 8}, nil
case CipherType_CHACHA20_IETF:
return &ChaCha20{IVBytes: 12}, nil
default:
return nil, newError("Unsupported cipher.")
}
}
func (v *Account) AsAccount() (protocol.Account, error) {
cipher, err := v.GetCipher()
if err != nil {
return nil, newError("failed to get cipher").Base(err)
}
return &ShadowsocksAccount{
Cipher: cipher,
Key: v.GetCipherKey(),
OneTimeAuth: v.Ota,
}, nil
}
func (v *Account) GetCipherKey() []byte {
ct, err := v.GetCipher()
if err != nil {
return nil
}
return PasswordToCipherKey(v.Password, ct.KeySize())
}
type Cipher interface {
KeySize() int
IVSize() int
NewEncodingStream(key []byte, iv []byte) (cipher.Stream, error)
NewDecodingStream(key []byte, iv []byte) (cipher.Stream, error)
}
type AesCfb struct {
KeyBytes int
}
func (v *AesCfb) KeySize() int {
return v.KeyBytes
}
func (v *AesCfb) IVSize() int {
return 16
}
func (v *AesCfb) NewEncodingStream(key []byte, iv []byte) (cipher.Stream, error) {
stream := crypto.NewAesEncryptionStream(key, iv)
return stream, nil
}
func (v *AesCfb) NewDecodingStream(key []byte, iv []byte) (cipher.Stream, error) {
stream := crypto.NewAesDecryptionStream(key, iv)
return stream, nil
}
type ChaCha20 struct {
IVBytes int
}
func (v *ChaCha20) KeySize() int {
return 32
}
func (v *ChaCha20) IVSize() int {
return v.IVBytes
}
func (v *ChaCha20) NewEncodingStream(key []byte, iv []byte) (cipher.Stream, error) {
return crypto.NewChaCha20Stream(key, iv), nil
}
func (v *ChaCha20) NewDecodingStream(key []byte, iv []byte) (cipher.Stream, error) {
return crypto.NewChaCha20Stream(key, iv), nil
}
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
}