39 lines
838 B
Go
39 lines
838 B
Go
package paymentutil
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/md5"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"net/url"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
func MD5Canonical(values map[string]any, secret string) string {
|
|
keys := make([]string, 0, len(values))
|
|
for key, value := range values {
|
|
if key != "sign" && value != nil && fmt.Sprint(value) != "" {
|
|
keys = append(keys, key)
|
|
}
|
|
}
|
|
sort.Strings(keys)
|
|
query := url.Values{}
|
|
for _, key := range keys {
|
|
query.Set(key, fmt.Sprint(values[key]))
|
|
}
|
|
sum := md5.Sum([]byte(query.Encode() + "&key=" + secret))
|
|
return strings.ToUpper(hex.EncodeToString(sum[:]))
|
|
}
|
|
|
|
func HMACSHA256Hex(raw []byte, secret string, upper bool) string {
|
|
h := hmac.New(sha256.New, []byte(secret))
|
|
_, _ = h.Write(raw)
|
|
result := hex.EncodeToString(h.Sum(nil))
|
|
if upper {
|
|
return strings.ToUpper(result)
|
|
}
|
|
return result
|
|
}
|