Class: SignedJson::Signer
- Inherits:
-
Object
- Object
- SignedJson::Signer
- Defined in:
- lib/signed_json.rb
Instance Method Summary collapse
- #decode(input) ⇒ Object
-
#digest_for(input) ⇒ Object
Generates an HMAC digest for the JSON representation of the given input.
- #encode(input) ⇒ Object
-
#initialize(secret, digest = 'SHA1') ⇒ Signer
constructor
A new instance of Signer.
Constructor Details
#initialize(secret, digest = 'SHA1') ⇒ Signer
Returns a new instance of Signer.
7 8 9 10 |
# File 'lib/signed_json.rb', line 7 def initialize(secret, digest = 'SHA1') @secret = secret @digest = digest end |
Instance Method Details
#decode(input) ⇒ Object
17 18 19 20 21 |
# File 'lib/signed_json.rb', line 17 def decode(input) digest, data = decode_digest_and_data(input) raise SignatureError unless digest === digest_for(data) data end |
#digest_for(input) ⇒ Object
Generates an HMAC digest for the JSON representation of the given input. JSON generation must be consistent across platforms. e.g. in Python, specify separators=(‘,’,‘:’) to eliminate whitespace.
26 27 28 29 30 |
# File 'lib/signed_json.rb', line 26 def digest_for(input) require 'openssl' unless defined?(OpenSSL) # from ActiveSupport::MessageVerifier digest = OpenSSL::Digest.const_get(@digest).new OpenSSL::HMAC.hexdigest(digest, @secret, signature_input(input)) end |
#encode(input) ⇒ Object
12 13 14 15 |
# File 'lib/signed_json.rb', line 12 def encode(input) data_to_encode = [digest_for(input), input] JSON.generate(data_to_encode) end |