1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/hmac.rb

48 lines
975 B
Ruby
Raw Normal View History

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
@digest = OpenSSL::Digest::Digest.new('sha1')
@signer = lambda do |data|
OpenSSL::HMAC.digest(@digest, @key, data)
end
end
def setup_sha256
begin
@digest = OpenSSL::Digest::Digest.new('sha256')
@signer = lambda do |data|
OpenSSL::HMAC.digest(@digest, @key, data)
end
rescue RuntimeError => error
unless error.message == 'Unsupported digest algorithm (sha256).'
raise error
else
require 'hmac-sha2'
@hmac = ::HMAC::SHA256.new(@key)
@signer = lambda do |data|
@hmac.update(data)
@hmac.digest
end
end
2010-06-16 00:07:15 -04:00
end
2010-06-16 00:04:16 -04:00
end
end
end