Last active
October 14, 2023 00:53
Revisions
-
viktorbenei revised this gist
Oct 5, 2018 . No changes.There are no files selected for viewing
-
viktorbenei revised this gist
Oct 5, 2018 . 2 changed files with 18 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package main import ( "crypto/hmac" "crypto/sha1" "crypto/subtle" "encoding/hex" "fmt" "os" @@ -15,7 +16,14 @@ func generateSignature(secretToken, payloadBody string) string { return "sha1=" + hex.EncodeToString(expectedMAC) } func verifySignature(secretToken, payloadBody string, signatureToCompareWith string) bool { signature := generateSignature(secretToken, payloadBody) return subtle.ConstantTimeCompare([]byte(signature), []byte(signatureToCompareWith)) == 1 } func main() { testPayloadBody := `{"message":"test content"}` testSignatureToCompareWith := `sha1=33a08e9b5e9c8d5e944d9288e9b499abb298344d` fmt.Println("signature match? :", verifySignature(os.Getenv("SECRET_TOKEN"), testPayloadBody, testSignatureToCompareWith)) } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,16 @@ require 'openssl' require 'rack' # gem install rack test_payload_body='{"message":"test content"}' test_signature_to_compare_with='sha1=33a08e9b5e9c8d5e944d9288e9b499abb298344d' def generate_signature(secret_token, payload_body) return 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), secret_token, payload_body) end def verify_signature(secret_token, payload_body, signature_to_compare_with) signature = generate_signature(secret_token, payload_body) return Rack::Utils.secure_compare(signature, signature_to_compare_with) end puts "signature match? : #{verify_signature(ENV['SECRET_TOKEN'], test_payload_body, test_signature_to_compare_with)}" -
viktorbenei created this gist
Oct 5, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ package main import ( "crypto/hmac" "crypto/sha1" "encoding/hex" "fmt" "os" ) func generateSignature(secretToken, payloadBody string) string { mac := hmac.New(sha1.New, []byte(secretToken)) mac.Write([]byte(payloadBody)) expectedMAC := mac.Sum(nil) return "sha1=" + hex.EncodeToString(expectedMAC) } func main() { var testPayloadBody = `{"message":"test content"}` fmt.Println("signature:", generateSignature(os.Getenv("SECRET_TOKEN"), testPayloadBody)) } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ require 'openssl' test_payload_body='{"message":"test content"}' def generate_signature(secret_token, payload_body) return 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), secret_token, payload_body) end puts "signature: #{generate_signature(ENV['SECRET_TOKEN'], test_payload_body)}"