-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypto.go
67 lines (56 loc) · 1.2 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
package main
import (
"crypto/des"
"crypto/sha1"
"encoding/hex"
)
func hashPassw(password string) string {
h := sha1.New()
h.Write([]byte(password))
bs := hex.EncodeToString(h.Sum(nil))
return bs
}
func createDesKey(key []byte) []byte {
if len(key) >= 8 {
return key[:8]
}
length := len(key)
for i := length; i < 8; i++ {
key = append(key, key[i%length])
}
return key[:8]
}
func preparePlaintext(plaintext string) []byte {
plain := []byte(plaintext)
if len(plaintext) < 8 {
for i := 0; i < 8-len(plaintext); i++ {
plain = append([]byte{0}, plain...)
}
}
return plain
}
func encryptDES(key []byte, plaintext string) string {
// create cipher
key = createDesKey(key)
c, _ := des.NewCipher(key)
// allocate space for ciphered data
plain := preparePlaintext(plaintext)
out := make([]byte, len(plain))
// encrypt
c.Encrypt(out, plain)
// return hex string
return hex.EncodeToString(out)
}
func decryptDES(key []byte, ct string) string {
key = createDesKey(key)
ciphertext, _ := hex.DecodeString(ct)
c, _ := des.NewCipher(key)
pt := make([]byte, len(ciphertext))
c.Decrypt(pt, ciphertext)
for i, p := range pt {
if p != 0 {
return string(pt[i:])
}
}
return string(pt)
}