Class: ASIR::Coder::Sign

Inherits:
ASIR::Coder show all
Defined in:
lib/asir/coder/sign.rb

Overview

!SLIDE Sign Coder

Sign payload during encode, check signature during decode.

Signature is the digest of secret + payload.

Encode payload as Hash containing the digest function name, signature and payload. Decode and validate Hash containing the digest function name, signature and payload.

Defined Under Namespace

Classes: SignatureError

Instance Attribute Summary collapse

Attributes included from Log

#_logger

Instance Method Summary collapse

Methods inherited from ASIR::Coder

#_subclass_responsibility, #decode, #encode, #prepare

Methods included from Log

#_log, #_log_enabled=, #_log_enabled?, #_log_format, #_log_result, enabled, enabled=, included

Methods included from Initialization

#initialize

Instance Attribute Details

#functionObject

Returns the value of attribute function.



16
17
18
# File 'lib/asir/coder/sign.rb', line 16

def function
  @function
end

#secretObject

Returns the value of attribute secret.



16
17
18
# File 'lib/asir/coder/sign.rb', line 16

def secret
  @secret
end

Instance Method Details

#_decode(obj) ⇒ Object

Raises:



26
27
28
29
30
31
# File 'lib/asir/coder/sign.rb', line 26

def _decode obj
  raise SignatureError, "expected Hash, given #{obj.class}" unless Hash === obj
  payload = obj[:payload]
  raise SignatureError, "signature invalid" unless obj == _encode(payload)
  payload
end

#_encode(obj) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/asir/coder/sign.rb', line 18

def _encode obj
  payload = obj.to_s
  { :function  => function,
    :signature => ::Digest.const_get(function).
                    new.hexdigest(secret + payload),
    :payload   => payload }
end

#initialize_before_optsObject



39
40
41
# File 'lib/asir/coder/sign.rb', line 39

def initialize_before_opts
  @function = :SHA1
end