Module: Hawk::Crypto
Instance Method Summary collapse
- #bewit(options) ⇒ Object
- #hash(options) ⇒ Object
- #mac(options) ⇒ Object
- #normalized_string(options) ⇒ Object
- #openssl_digest(algorithm) ⇒ Object
- #ts_mac(options) ⇒ Object
Instance Method Details
#bewit(options) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/hawk/crypto.rb', line 84 def bewit() [:ts] ||= Time.now.to_i + [:ttl].to_i _mac = mac(.merge(:type => 'bewit')) parts = [] parts << [:credentials][:id] parts << [:ts] parts << _mac parts << [:ext] Base64.urlsafe_encode64(parts.join("\\")).chomp.sub(/=+\Z/, '') end |
#hash(options) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/hawk/crypto.rb', line 25 def hash() parts = [] parts << "hawk.1.payload" parts << [:content_type] parts << [:payload].to_s parts << nil # trailing newline Base64.encode64(OpenSSL::Digest.const_get([:credentials][:algorithm].upcase).digest(parts.join("\n"))).chomp end |
#mac(options) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/hawk/crypto.rb', line 64 def mac() Base64.encode64( OpenSSL::HMAC.digest( openssl_digest([:credentials][:algorithm]).new, [:credentials][:key], normalized_string() ) ).chomp end |
#normalized_string(options) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/hawk/crypto.rb', line 36 def normalized_string() = .dup if ![:hash] && .has_key?(:payload) && ![:payload].nil? [:hash] = hash() end parts = [] parts << "hawk.1.#{[:type] || 'header'}" parts << [:ts] parts << [:nonce] parts << [:method].to_s.upcase parts << [:request_uri] parts << [:host] parts << [:port] parts << [:hash] parts << [:ext] if [:app] parts << [:app] parts << [:dig] end parts << nil # trailing newline parts.join("\n") end |
#openssl_digest(algorithm) ⇒ Object
99 100 101 |
# File 'lib/hawk/crypto.rb', line 99 def openssl_digest(algorithm) OpenSSL::Digest.const_get(algorithm.upcase) end |
#ts_mac(options) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/hawk/crypto.rb', line 74 def ts_mac() Base64.encode64( OpenSSL::HMAC.digest( openssl_digest([:credentials][:algorithm]).new, [:credentials][:key], "hawk.1.ts\n#{[:ts]}\n" ) ).chomp end |