API invocation method
...
Signature algorithm
Signature demonstration
Go language demo
1min
Go
1package main
2
3import (
4 "crypto/hmac"
5 "crypto/sha256"
6 "encoding/hex"
7 "fmt"
8 "time"
9)
10
11func sha256hex(s string) string {
12 b := sha256.Sum256([]byte(s))
13 return hex.EncodeToString(b[:])
14}
15
16func hmacsha256(s, key string) string {
17 hashed := hmac.New(sha256.New, []byte(key))
18 hashed.Write([]byte(s))
19 return string(hashed.Sum(nil))
20}
21
22func main() {
23 accessKey := "AKIDz8krbsJ5yKBZQpn74WFkmLPx3*******"
24 secretKey := "Gu5t9xGARNpq86cd98joQYCN3*******"
25 host := "api-testing.klavi.ai"
26 algorithm := "K1-HMAC-SHA256"
27 service := "cbms"
28
29 region := "ap-brazil"
30 var timestamp int64 = time.Now().Unix()
31
32 // step 1: build canonical request string
33 // httpRequestMethod := "POST"
34 // canonicalURI := "/"
35 httpRequestMethod := "GET"
36 canonicalURI := "/link/external/v2/getInstitutionList"
37 canonicalQueryString := ""
38 // canonicalHeaders := "content-type:application/json; charset=utf-8\n" + "host:" + host + "\n"
39 canonicalHeaders := "content-type:application/x-www-form-urlencoded\n" + "host:" + host + "\n"
40 signedHeaders := "content-type;host"
41 payload := ""
42 hashedRequestPayload := sha256hex(payload)
43 canonicalRequest := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s",
44 httpRequestMethod,
45 canonicalURI,
46 canonicalQueryString,
47 canonicalHeaders,
48 signedHeaders,
49 hashedRequestPayload)
50 fmt.Printf("canonicalRequest: %s\n\n",canonicalRequest)
51
52 // step 2: build string to sign
53 date := time.Unix(timestamp, 0).UTC().Format("2006-01-02")
54 credentialScope := fmt.Sprintf("%s/%s/k1_request", date, service)
55 hashedCanonicalRequest := sha256hex(canonicalRequest)
56 string2sign := fmt.Sprintf("%s\n%d\n%s\n%s",
57 algorithm,
58 timestamp,
59 credentialScope,
60 hashedCanonicalRequest)
61 fmt.Printf("string2sign: %s\n",string2sign)
62
63 // step 3: sign string
64 secretDate := hmacsha256(date, "K1"+secretKey)
65 secretService := hmacsha256(service, secretDate)
66 secretSigning := hmacsha256("k1_request", secretService)
67 signature := hex.EncodeToString([]byte(hmacsha256(string2sign, secretSigning)))
68 fmt.Printf("signature: %s\n\n",signature)
69
70 // step 4: build authorization
71 authorization := fmt.Sprintf("%s Credential=%s/%s, SignedHeaders=%s, Signature=%s",
72 algorithm,
73 accessKey,
74 credentialScope,
75 signedHeaders,
76 signature)
77 fmt.Printf("authorization: %s\n\n",authorization)
78
79 // GET
80 getCurl := fmt.Sprintf(`curl -s -L -X GET http://%s%s \
81 -H 'Authorization: %s'\
82 -H 'Content-type: application/x-www-form-urlencoded' \
83 -H 'Host: %s'\
84 -H 'X-Timestamp: %d' \
85 -H 'X-Region: %s'`,host,canonicalURI,authorization,host,timestamp,region)
86 fmt.Println(getCurl)
87
88 // POST
89 //postCurl := fmt.Sprintf(`curl -X POST https://%s\
90 //-H "Authorization: %s"\
91 //-H "Content-Type: application/json; charset=utf-8"\
92 //-H "Host: %s"\
93 //-H "X-Timestamp: %d"\
94 //-H "X-Region: %s"\
95 //-d '%s'`, host, authorization, host, timestamp, region, payload)
96 //fmt.Println(postCurl)
97}
💡 Note:
The GET method has been implemented in the code, and the commented code is the POST method.
Updated 06 Dec 2023
Did this page help you?