require "openssl"
require "base64"
class YHSDWebhook
attr_accessor :token
def initialize( token)
@token = token
end
def verify_webhook( data, hmac_header)
digest = OpenSSL::Digest::Digest .new ( 'sha256' )
calculated_hmac = Base64 .encode64 ( OpenSSL::HMAC .digest ( digest, @token , data) ) .strip
calculated_hmac == hmac_header
end
end
# 数据
data ="{\" created_at\" :\" 2014-08-28T17:28:13.301+08:00\" ,\" domain\" :\" www.example.com\" ,\" enable_email_regist\" :true,\" enable_mobile_regist\" :true,\" enable_username_regist\" :true,\" name\" :\" TEST\" ,\" page_description\" :\" \" ,\" page_title\" :\" \" ,\" updated_at\" :\" 2015-07-27T13:58:14.607+08:00\" ,\" url\" :\" http://w...content-available-to-author-only...e.com\" ,\" webhook_token\" :\" 906155047ff74a14a1ca6b1fa74d3390\" }"
# Webhook密钥
webhook_token = "906155047ff74a14a1ca6b1fa74d3390"
# X_YHSD_HMAC_SHA256 Head
hmac_header = "NS0Wcz2CDgzI4+L9/UYdwaXpPI4As7VD+wKCRgKqNUo="
verify_result = YHSDWebhook.new ( webhook_token) .verify_webhook ( data, hmac_header)
puts "verify_result:" + verify_result.to_s
cmVxdWlyZSAib3BlbnNzbCIKcmVxdWlyZSAiYmFzZTY0IgogCmNsYXNzIFlIU0RXZWJob29rCiAKICBhdHRyX2FjY2Vzc29yIDp0b2tlbgogCiAgZGVmIGluaXRpYWxpemUodG9rZW4pCiAgICBAdG9rZW49IHRva2VuCiAgZW5kCgogIGRlZiB2ZXJpZnlfd2ViaG9vayhkYXRhLCBobWFjX2hlYWRlcikKICAgIGRpZ2VzdCAgPSBPcGVuU1NMOjpEaWdlc3Q6OkRpZ2VzdC5uZXcoJ3NoYTI1NicpCiAgICBjYWxjdWxhdGVkX2htYWMgPSBCYXNlNjQuZW5jb2RlNjQoT3BlblNTTDo6SE1BQy5kaWdlc3QoZGlnZXN0LCBAdG9rZW4sIGRhdGEpKS5zdHJpcAogICAgY2FsY3VsYXRlZF9obWFjID09IGhtYWNfaGVhZGVyCiAg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