-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
84 lines (66 loc) · 1.68 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
package shadowsocks
import (
"crypto/cipher"
"crypto/md5"
"github.com/v2ray/v2ray-core/common/crypto"
"github.com/v2ray/v2ray-core/common/protocol"
)
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 (this *AesCfb) KeySize() int {
return this.KeyBytes
}
func (this *AesCfb) IVSize() int {
return 16
}
func (this *AesCfb) NewEncodingStream(key []byte, iv []byte) (cipher.Stream, error) {
stream := crypto.NewAesEncryptionStream(key, iv)
return stream, nil
}
func (this *AesCfb) NewDecodingStream(key []byte, iv []byte) (cipher.Stream, error) {
stream := crypto.NewAesDecryptionStream(key, iv)
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
}
type Config struct {
Cipher Cipher
Key []byte
UDP bool
Level protocol.UserLevel
Email string
}
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
}