Module: Macaroons::Utils
- Defined in:
- lib/macaroons/utils.rb
Class Method Summary collapse
- .convert_to_bytes(string) ⇒ Object
- .generate_derived_key(key) ⇒ Object
- .hexlify(value) ⇒ Object
- .hmac(key, data, digest = nil) ⇒ Object
- .sign_first_party_caveat(signature, predicate) ⇒ Object
- .sign_third_party_caveat(signature, verification_id, caveat_id) ⇒ Object
- .truncate_or_pad(string, size = nil) ⇒ Object
- .unhexlify(value) ⇒ Object
Class Method Details
.convert_to_bytes(string) ⇒ Object
6 7 8 |
# File 'lib/macaroons/utils.rb', line 6 def self.convert_to_bytes(string) string.encode('us-ascii') unless string.nil? end |
.generate_derived_key(key) ⇒ Object
45 46 47 |
# File 'lib/macaroons/utils.rb', line 45 def self.generate_derived_key(key) Utils.hmac('macaroons-key-generator', key) end |
.hexlify(value) ⇒ Object
10 11 12 |
# File 'lib/macaroons/utils.rb', line 10 def self.hexlify(value) value.unpack('C*').map { |byte| '%02X' % byte }.join('') end |
.hmac(key, data, digest = nil) ⇒ Object
29 30 31 32 |
# File 'lib/macaroons/utils.rb', line 29 def self.hmac(key, data, digest=nil) digest = OpenSSL::Digest.new('sha256') if digest.nil? OpenSSL::HMAC.digest(digest, key, data) end |
.sign_first_party_caveat(signature, predicate) ⇒ Object
34 35 36 |
# File 'lib/macaroons/utils.rb', line 34 def self.sign_first_party_caveat(signature, predicate) Utils.hmac(signature, predicate) end |
.sign_third_party_caveat(signature, verification_id, caveat_id) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/macaroons/utils.rb', line 38 def self.sign_third_party_caveat(signature, verification_id, caveat_id) verification_id_hash = Utils.hmac(signature, verification_id) caveat_id_hash = Utils.hmac(signature, caveat_id) combined = verification_id_hash + caveat_id_hash Utils.hmac(signature, combined) end |
.truncate_or_pad(string, size = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/macaroons/utils.rb', line 18 def self.truncate_or_pad(string, size=nil) size = size.nil? ? 32 : size if string.length > size string[0, size] elsif string.length < size string + "\0"*(size-string.length) else string end end |
.unhexlify(value) ⇒ Object
14 15 16 |
# File 'lib/macaroons/utils.rb', line 14 def self.unhexlify(value) [value].pack('H*') end |