-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcrypto.go
133 lines (105 loc) · 2.27 KB
/
crypto.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
126
127
128
129
130
131
132
133
package ntlmssp
import (
"bytes"
"crypto/des"
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"crypto/rc4"
"encoding/binary"
"errors"
"hash/crc32"
"golang.org/x/crypto/md4"
)
func nonce(length int) ([]byte, error) {
result := make([]byte, length)
if _, err := rand.Read(result); err != nil {
return nil, err
}
return result, nil
}
func createDESKey(b []byte) ([]byte, error) {
if len(b) < 7 {
return nil, errors.New("need at least 7 bytes")
}
key := zeroBytes(8)
key[0] = b[0]
key[1] = b[0]<<7 | b[1]>>1
key[2] = b[1]<<6 | b[2]>>2
key[3] = b[2]<<5 | b[3]>>3
key[4] = b[3]<<4 | b[4]>>4
key[5] = b[4]<<3 | b[5]>>5
key[6] = b[5]<<2 | b[6]>>6
key[7] = b[6] << 1
// Calculate odd parity
for i, x := range key {
key[i] = (x & 0xfe) | ((((x >> 1) ^ (x >> 2) ^ (x >> 3) ^ (x >> 4) ^ (x >> 5) ^ (x >> 6) ^ (x >> 7)) ^ 0x01) & 0x01)
}
return key, nil
}
func encryptDES(k, d []byte) ([]byte, error) {
key, err := createDESKey(k)
if err != nil {
return nil, err
}
cipher, err := des.NewCipher(key)
if err != nil {
return nil, err
}
result := make([]byte, len(d))
cipher.Encrypt(result, d)
return result, nil
}
func encryptDESL(k, d []byte) ([]byte, error) {
b := bytes.Buffer{}
padded := zeroPad(k, 21)
for _, i := range []int{0, 7, 14} {
result, err := encryptDES(padded[i:], d)
if err != nil {
return nil, err
}
if _, err := b.Write(result); err != nil {
return nil, err
}
}
return b.Bytes(), nil
}
func initRC4(k []byte) (*rc4.Cipher, error) {
cipher, err := rc4.NewCipher(k)
if err != nil {
return nil, err
}
return cipher, nil
}
func encryptRC4(cipher *rc4.Cipher, d []byte) []byte {
result := make([]byte, len(d))
cipher.XORKeyStream(result, d)
return result
}
func encryptRC4K(k, d []byte) ([]byte, error) {
cipher, err := initRC4(k)
if err != nil {
return nil, err
}
return encryptRC4(cipher, d), nil
}
func hashMD4(b []byte) []byte {
md4 := md4.New()
md4.Write(b)
return md4.Sum(nil)
}
func hashMD5(b []byte) []byte {
md5 := md5.New()
md5.Write(b)
return md5.Sum(nil)
}
func hmacMD5(k, m []byte) []byte {
mac := hmac.New(md5.New, k)
mac.Write(m)
return mac.Sum(nil)
}
func hashCRC32(b []byte) []byte {
checksum := make([]byte, 4)
binary.LittleEndian.PutUint32(checksum, crc32.ChecksumIEEE(b))
return checksum
}