2010-06-16 00:04:16 -04:00
|
|
|
module Fog
|
|
|
|
class HMAC
|
|
|
|
|
|
|
|
def initialize(type, key)
|
2010-06-16 00:07:15 -04:00
|
|
|
@key = key
|
|
|
|
case type
|
2010-06-16 00:04:16 -04:00
|
|
|
when 'sha1'
|
2010-06-16 00:07:15 -04:00
|
|
|
setup_sha1
|
2010-06-16 00:04:16 -04:00
|
|
|
when 'sha256'
|
2010-06-16 00:07:15 -04:00
|
|
|
setup_sha256
|
2010-06-16 00:04:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sign(data)
|
2010-06-16 00:07:15 -04:00
|
|
|
@signer.call(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def setup_sha1
|
2013-12-09 11:12:54 -05:00
|
|
|
@digest = OpenSSL::Digest.new('sha1')
|
2010-06-16 00:07:15 -04:00
|
|
|
@signer = lambda do |data|
|
|
|
|
OpenSSL::HMAC.digest(@digest, @key, data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_sha256
|
2013-12-26 21:44:37 -05:00
|
|
|
@digest = OpenSSL::Digest.new('sha256')
|
|
|
|
@signer = lambda do |data|
|
|
|
|
OpenSSL::HMAC.digest(@digest, @key, data)
|
2010-06-16 00:07:15 -04:00
|
|
|
end
|
2010-06-16 00:04:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-12-09 11:12:54 -05:00
|
|
|
end
|