-
Notifications
You must be signed in to change notification settings - Fork 4
/
checksum.go
135 lines (127 loc) · 3.06 KB
/
checksum.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
134
135
package paytm
import (
"strings"
"sort"
"math/rand"
"time"
"crypto/sha256"
"fmt"
"encoding/base64"
)
func GetChecksumFromArray(paramsMap map[string]string) (checksum string, err error) {
var keys = make([]string, 0, 0)
for k, v := range paramsMap {
if v != "" {
keys = append(keys, k)
}
}
sort.Strings(keys)
var arrayList = make([]string, 0, 0)
for _, key := range keys {
if value, ok := paramsMap[key]; ok && value != "" {
arrayList = append(arrayList, value)
}
}
arrayStr := getArray2Str(arrayList)
salt := generateSalt(4)
finalString := arrayStr + "|" + salt
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(finalString)))
hashString := hash + salt
crypt, err := Encrypt([]byte(hashString))
if err != nil {
return
}
checksum = base64.StdEncoding.EncodeToString(crypt)
return
}
func VerifyCheckum(paramsMap map[string]string, checksum string) (ok bool) {
delete(paramsMap, "CHECKSUMHASH")
var keys = make([]string, 0, 0)
for k, v := range paramsMap {
if v != "" {
keys = append(keys, k)
}
}
sort.Strings(keys)
var arrayList = make([]string, 0, 0)
for _, key := range keys {
if value, ok := paramsMap[key]; ok && value != "" {
arrayList = append(arrayList, value)
}
}
arrayStr := getArray2StrForVerify(arrayList)
cs, err := base64.StdEncoding.DecodeString(checksum)
if err != nil {
fmt.Printf("base64 DecodeString err [%v]\n", err)
return
}
paytmHash, err := Decrypt(cs)
if err != nil {
fmt.Printf("Decrypt err [%v]\n", err)
return
}
paytmHashStr := string(paytmHash)
salt := paytmHashStr[len(paytmHashStr)-4:]
finalString := arrayStr + "|" + salt
h := sha256.New()
h.Write([]byte(finalString))
finalStringHash := fmt.Sprintf("%x", h.Sum(nil))
websiteHashStr := finalStringHash + salt
if websiteHashStr == paytmHashStr {
return true
}
return false
}
func Encrypt(input []byte) (output []byte, err error) {
iv := "@@@@&&&&####$$$$"
crypter, _ := NewCrypter([]byte(PaytmMerchantKey), []byte(iv))
output, err = crypter.Encrypt(input)
return
}
func Decrypt(input []byte) (output []byte, err error) {
iv := "@@@@&&&&####$$$$"
crypter, err := NewCrypter([]byte(PaytmMerchantKey), []byte(iv))
output, err = crypter.Decrypt(input)
return
}
func getArray2Str(arrayList []string) (str string) {
findme := "REFUND"
findmepipe := "|"
flag := 1
for _, v := range arrayList {
pos := strings.Index(v, findme)
pospipe := strings.Index(v, findmepipe)
if pos != -1 || pospipe != -1 {
continue
}
if flag > 0 {
str += strings.TrimSpace(v)
flag = 0
} else {
str += "|" + strings.TrimSpace(v)
}
}
return
}
func getArray2StrForVerify(arrayList []string) (str string) {
flag := 1
for _, v := range arrayList {
if flag > 0 {
str += strings.TrimSpace(v)
flag = 0
} else {
str += "|" + strings.TrimSpace(v)
}
}
return
}
func generateSalt(length int) (salt string) {
rand.Seed(time.Now().UnixNano())
data := "AbcDE123IJKLMN67QRSTUVWXYZ"
data += "aBCdefghijklmn123opq45rs67tuv89wxyz"
data += "0FGH45OP89"
for i := 0; i < length; i++ {
salt += string(data[int(rand.Int()%len(data))])
}
return
}