Class: Nobi::Signer

Inherits:
Object
  • Object
show all
Defined in:
lib/nobi.rb

Direct Known Subclasses

TimestampSigner

Instance Method Summary collapse

Constructor Details

#initialize(secret, salt: 'nobi.Signer', sep: '.', digest_method: 'sha1') ⇒ Signer

Returns a new instance of Signer.



69
70
71
72
73
74
75
76
77
78
# File 'lib/nobi.rb', line 69

def initialize(secret,
  salt: 'nobi.Signer',
  sep: '.',
  digest_method: 'sha1')

  @secret = secret
  @salt = salt
  @sep = sep
  @algorithm = HMACAlgorithm.new(digest_method)
end

Instance Method Details

#derive_keyObject



98
99
100
# File 'lib/nobi.rb', line 98

def derive_key
  @algorithm.signature(@secret, @salt)
end

#sign(value) ⇒ Object



80
81
82
# File 'lib/nobi.rb', line 80

def sign(value)
  '%s%s%s' % [value, @sep, signature(value)]
end

#signature(value) ⇒ Object



102
103
104
105
106
107
# File 'lib/nobi.rb', line 102

def signature(value)
  key = derive_key
  sig = @algorithm.signature(key, value)

  Utils.base64_encode(sig)
end

#unsign(value) ⇒ Object

Raises:



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/nobi.rb', line 84

def unsign(value)
  if not value.include?(@sep)
    raise BadSignature, 'No "%s" found in value' % @sep
  end

  value, sig = Utils.rsplit(value, @sep)

  if Utils.constant_time_compare(sig, signature(value))
    return value
  end

  raise BadSignature, 'Signature "%s" does not match' % sig
end