Last active
May 11, 2018 14:46
-
-
Save beepony/0f8bf3d65df3200ca809b329fa99345f to your computer and use it in GitHub Desktop.
ruby HMAC SHA1 for UPYUN API
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 characters
#!/usr/bin/ruby -w | |
# hexdigest 16 进制,digest 二进制 | |
# HMAC SHA1 加密方法 | |
require 'base64' | |
require 'digest' | |
require 'openssl' | |
def hamc_sha_for_upyun(key, data) | |
digest = OpenSSL::Digest.new('sha1') | |
hmac = OpenSSL::HMAC.digest(digest, key, data) # hamc sha1 二进制加密结果 | |
puts Base64.strict_encode64 hmac | |
end | |
key = 'ab296a01090ca2eab5fe5b246999da54' | |
data = 'POST&/pretreatment/&Wed, 9 Nov 2016 14:26:58 GMT&346867d17ec42a2e5d1593a420e85323' | |
hamc_sha_for_upyun(key,data) | |
=begin | |
另外一种写法 | |
require 'base64' | |
require 'cgi' | |
require 'openssl' | |
base = 'POST&https%3A%2F%2Fapi.twitter.com%2F1%2Fstatuses%2Fupdate.json&include_entities%3Dtrue%26oauth_consumer_key%3Dxvz1evFS4wEEPTGEFPHBog%26oauth_nonce%3DkYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1318622958%26oauth_token%3D370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb%26oauth_version%3D1.0%26status%3DHello%2520Ladies%2520%252B%2520Gentlemen%252C%2520a%2520signed%2520OAuth%2520request%2521' | |
key = 'kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw&LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE' | |
puts CGI.escape(Base64.encode64("#{OpenSSL::HMAC.digest('sha1', key, base)}\n")) | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment