59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package union_pay
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"strconv"
|
|
)
|
|
|
|
func Sha(version, appId, bizMethod, reId, body string) string {
|
|
s := fmt.Sprintf("version=%s&appId=%s&bizMethod=%s&reqId=%s&body=%s", version, appId, bizMethod, reId, body)
|
|
return encodeBySHA256([]byte(s))
|
|
}
|
|
|
|
func encodeBySHA256(str []byte) string {
|
|
hash := sha256.Sum256(str)
|
|
return getFormattedText(hash[:])
|
|
}
|
|
|
|
func getFormattedText(bytes []byte) string {
|
|
HexDigits := "0123456789abcdef"
|
|
|
|
l := len(bytes)
|
|
buf := make([]byte, l*2)
|
|
for j := 0; j < l; j++ {
|
|
buf[j*2] = HexDigits[(bytes[j]>>4)&0x0f]
|
|
buf[j*2+1] = HexDigits[bytes[j]&0x0f]
|
|
}
|
|
|
|
return string(buf)
|
|
}
|
|
|
|
func ConvertToInt(value interface{}) int {
|
|
switch v := value.(type) {
|
|
case string:
|
|
if intValue, err := strconv.Atoi(v); err == nil {
|
|
return intValue
|
|
}
|
|
case int:
|
|
return v
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// HexToBytes 将16进制的字符数组转换成byte数组
|
|
func HexToBytes(hex []byte) []byte {
|
|
length := len(hex) / 2
|
|
raw := make([]byte, length)
|
|
for i := 0; i < length; i++ {
|
|
high, _ := strconv.ParseInt(string(hex[i*2]), 16, 0)
|
|
low, _ := strconv.ParseInt(string(hex[i*2+1]), 16, 0)
|
|
value := int(high<<4) | int(low)
|
|
if value > 127 {
|
|
value -= 256
|
|
}
|
|
raw[i] = byte(value)
|
|
}
|
|
return raw
|
|
}
|