You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package cryptokit
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"reflect"
|
|
)
|
|
|
|
type DocEncrypt struct {
|
|
RandStr string `map:"ranstr"`
|
|
TimeStamp string `map:"timestamp"`
|
|
SignId string `map:"signid"`
|
|
}
|
|
|
|
func NewDocEncrypt() *DocEncrypt {
|
|
docEncrypt := new(DocEncrypt)
|
|
docEncrypt.RandStr = RandomStr(true, 10, 32)
|
|
docEncrypt.TimeStamp = GetTimeStamp()
|
|
firstMD5 := md5Sum(docEncrypt.RandStr + docEncrypt.TimeStamp)
|
|
calculatedSign := md5Sum(firstMD5 + docEncrypt.RandStr)
|
|
docEncrypt.SignId = calculatedSign
|
|
return docEncrypt
|
|
}
|
|
|
|
func (th *DocEncrypt) GenReqParam() map[string]string {
|
|
return th.structToMap()
|
|
}
|
|
|
|
func (th *DocEncrypt) structToMap() map[string]string {
|
|
result := make(map[string]string)
|
|
t := reflect.TypeOf(*th)
|
|
v := reflect.ValueOf(*th)
|
|
for i := 0; i < t.NumField(); i++ {
|
|
field := t.Field(i)
|
|
value := v.Field(i)
|
|
// Use the tag value as the key in the map, if it exists; otherwise, use the field name.
|
|
tag := field.Tag.Get("map")
|
|
if tag == "" {
|
|
tag = field.Name
|
|
}
|
|
result[tag] = value.String()
|
|
}
|
|
return result
|
|
}
|
|
|
|
func md5Sum(data string) string {
|
|
hash := md5.Sum([]byte(data))
|
|
return hex.EncodeToString(hash[:])
|
|
}
|