You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jcook-sdk/util/security/sha1.go

38 lines
619 B

package security
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"sort"
"strings"
)
func map2str(data map[string]string, sep string) string {
var temp []string
for key, _ := range data {
temp = append(temp, key)
}
sort.Strings(temp)
var tt []string
for _, v := range temp {
tt = append(tt, fmt.Sprintf("%s=%s", v, data[v]))
}
return strings.Join(tt, sep)
}
func Sha1OrderlyWithBase64(secret string, data map[string]string) string {
h := hmac.New(sha1.New, []byte(secret))
str := map2str(data, "&")
h.Write([]byte(str))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}