Skip to content

Instantly share code, notes, and snippets.

@pobing
Last active July 4, 2018 10:16
Show Gist options
  • Save pobing/11238340 to your computer and use it in GitHub Desktop.
Save pobing/11238340 to your computer and use it in GitHub Desktop.
ruby 使用HMAC算法得到加密字符串
#(1) 把timestamp和token联合;
# (2) 使用HMAC算法得到加密字符串(将appkey作为参数并使用SHA256 哈希方法);
# (3) 比较signature和得到的加密字符串。
timestamp = "1398236282767"
token = "THd78OwTxVGjTgCYSOxr4vkCPj3oIUg1TpZgkYpjerT2zNWdPQ"
signature = "df5fc888e86c8032f0c6b56e91ab91ebdfdae297e7a1dd4983e2e091042c151e"
data = timestamp + token
appkey = 'bl92t6tw-55zn-h14y-uzpd-y52k7jqe5s'
res = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), appkey, data)
return res == signature
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment