I am trying to generate a hmac for API authentication, but the documentation is on Go, I need to convert it to php way, I need the result to be the same as Go result.
On the Docs said: To build the string to sign, combine the HTTP method, request content type, date time, request URI, and the payload hash with the newline in between each parameter.
Go Way:
func ComputeHMAC(date string) string
request := []string{"POST","application/json",'Thu, 17 Jan 2019 02:45:06
GMT',"/ayden/init",'qz0HpayQzMDnBfJMfUB5zJGU62nX2Uef66m6YIpDAWA='}
request1 := strings.Join(request, "\n")
request2 := []string{request1,"\n"}
stringToSign := strings.Join(request2, "")
shaSignature := computeHmac256(stringToSign, "secret")
}
func computeHmac256(message string, secret string) string {
key := []byte(secret)
h := hmac.New(sha256.New, key)
h.Write([]byte(message))
result := h.Sum(nil)
return base64.StdEncoding.EncodeToString(result)
}
Hmac in GO: 6k44vpeNqYSgFkM2sZsJH+Ijg5amftPnqO3v45pMWN0=
What i tried in PHP:
$stringToSign = [
'POST',
'application/json',
'Thu, 17 Jan 2019 02:45:06 GMT',
'/ayden/init',
'qz0HpayQzMDnBfJMfUB5zJGU62nX2Uef66m6YIpDAWA='
];
$string = implode("/n", $stringToSign);
$hmac = base64_encode(hash_hmac('sha256', $string, 'secret', true));
echo $hmac;
Hmac in PHP: 9mt4Ojzr9uVGaB6/jt96bbuEd0gJNh6Cph3q+dY3X38=
I`m a bit lost here, already spent many hours to figure this out, please help.