Last active
February 16, 2022 14:44
-
-
Save Phathdt/b1e460fa29a7c949c10d1548106168b8 to your computer and use it in GitHub Desktop.
hmac sha256
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/sha256" | |
"encoding/hex" | |
"fmt" | |
"strings" | |
) | |
func main() { | |
hash := sign("randomstring", "/path/abccodeabc") | |
fmt.Println(hash) | |
} | |
func sign(secret string, paramsStr string) string { | |
fmt.Println(paramsStr) | |
h := sha256.New() | |
h.Write([]byte(paramsStr)) | |
sha := hex.EncodeToString(h.Sum([]byte(secret))) | |
return strings.ToUpper(sha) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hmac | |
import hashlib | |
def sign(secret, parameters_str): | |
print(parameters_str) | |
h = hmac.new(secret.encode(encoding="utf-8"), parameters_str.encode(encoding="utf-8"), digestmod=hashlib.sha256) | |
return h.hexdigest().upper() | |
a = sign("randomstring", "/path/abccodeabc") | |
print(a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Utils.HmacSigning do | |
require Logger | |
def generate_sign(secret_key, hash) do | |
:crypto.mac(:hmac, :sha256, secret_key, hash) | |
|> IO.inspect(label: "abc.ex:11") | |
|> Base.encode16() | |
end | |
end | |
Utils.HmacSigning.generate_sign("randomstring", "/path/abccodeabc") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment