-
Notifications
You must be signed in to change notification settings - Fork 5
/
encryption.go
53 lines (45 loc) · 1.15 KB
/
encryption.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
package ravepay
import (
"bytes"
"crypto/des"
"crypto/md5"
"encoding/base64"
"fmt"
"io"
"log"
"strings"
)
func getEncryptionKey(seckey string) string {
adjustedSeckey := strings.Replace(seckey, "FLWSECK-", "", 1)
if len(adjustedSeckey) < 12 {
return ""
}
adjustedSeckeyFirst12 := adjustedSeckey[:12]
h := md5.New()
io.WriteString(h, seckey)
keyMD5 := fmt.Sprintf("%x", h.Sum(nil))
keyMD5Last12 := keyMD5[len(keyMD5)-12:]
return adjustedSeckeyFirst12 + keyMD5Last12
}
// https://github.com/golang/go/issues/5597
func tripleDESEncrypt(payload, key []byte) string {
block, err := des.NewTripleDESCipher(key)
if err != nil {
log.Println("couldn't create 3DESC cipher: ", err)
return ""
}
bs := block.BlockSize()
if numStrandedBytes := len(payload) % bs; numStrandedBytes != 0 {
paddingAmt := bs - numStrandedBytes
padding := bytes.Repeat([]byte{byte(paddingAmt)}, paddingAmt)
payload = append(payload, padding...)
}
cipher := make([]byte, len(payload))
cipherDup := cipher
for len(payload) > 0 {
block.Encrypt(cipher, payload)
payload = payload[bs:]
cipher = cipher[bs:]
}
return base64.StdEncoding.EncodeToString(cipherDup)
}