fork(2) download
  1. require "openssl"
  2. require "base64"
  3.  
  4. class YHSDWebhook
  5.  
  6. attr_accessor :token
  7.  
  8. def initialize(token)
  9. @token= token
  10. end
  11.  
  12. def verify_webhook(data, hmac_header)
  13. digest = OpenSSL::Digest::Digest.new('sha256')
  14. calculated_hmac = Base64.encode64(OpenSSL::HMAC.digest(digest, @token, data)).strip
  15. calculated_hmac == hmac_header
  16. end
  17.  
  18. end
  19.  
  20. # 数据
  21. 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\"}"
  22.  
  23. # Webhook密钥
  24. webhook_token = "906155047ff74a14a1ca6b1fa74d3390"
  25.  
  26. # X_YHSD_HMAC_SHA256 Head
  27. hmac_header = "NS0Wcz2CDgzI4+L9/UYdwaXpPI4As7VD+wKCRgKqNUo="
  28.  
  29. verify_result = YHSDWebhook.new(webhook_token).verify_webhook(data, hmac_header)
  30.  
  31. puts "verify_result:" + verify_result.to_s
Success #stdin #stdout #stderr 0.07s 13384KB
stdin
Standard input is empty
stdout
verify_result:true
stderr
Digest::Digest is deprecated; use Digest