liuxiaolong
2020-06-18 f45452ec05c97c26a8558f3e6398888ace9a7c5c
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
package util
 
import (
    "crypto/hmac"
    "crypto/md5"
    "crypto/sha256"
    "encoding/base64"
    "unsafe"
)
func ComputeHmacSha256Base64(message string, secret string) string {
    key := []byte(secret)
    h := hmac.New(sha256.New, key)
    h.Write([]byte(message))
    abs := h.Sum(nil)
 
    return base64.StdEncoding.EncodeToString(abs)
}
 
func Hik_ComputeSignatureBase64(appKey string, appSecret string, httpMethod string, accept string, contentType string, date string, url string) string {
    httpHeaders := httpMethod +"\n" + accept + "\n" + contentType + "\n"+ date +"\n"
    customHeaders := "x-ca-key:"+ appKey +"\n"
    msg := httpHeaders + customHeaders + url
    return ComputeHmacSha256Base64(msg, appSecret)
}
 
type SliceMock struct {
    addr uintptr
    len int
    cap int
}
 
func GetIBytes(v interface{}) []byte {
    len := unsafe.Sizeof(v)
    dataBytes := &SliceMock{
        addr: uintptr(unsafe.Pointer(&v)),
        cap: int(len),
        len: int(len),
    }
    b := *(*[]byte)(unsafe.Pointer(dataBytes))
    return b
}
 
func MD5Base64(s []byte) string {
    sum := md5.Sum(s)
    return base64.StdEncoding.EncodeToString(sum[:])
}