Class: Sandal::Sig::HS
- Inherits:
-
Object
- Object
- Sandal::Sig::HS
- Defined in:
- lib/sandal/sig/hs.rb
Overview
Base implementation of the HMAC-SHA family of signature algorithms.
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
The JWA name of the algorithm.
Instance Method Summary collapse
-
#initialize(sha_size, key) ⇒ HS
constructor
Creates a new instance; it’s probably easier to use one of the subclass constructors.
-
#sign(payload) ⇒ String
Signs a payload and returns the signature.
-
#valid?(signature, payload) ⇒ Boolean
Validates a payload signature and returns whether the signature matches.
Constructor Details
#initialize(sha_size, key) ⇒ HS
Creates a new instance; it’s probably easier to use one of the subclass constructors.
18 19 20 21 22 |
# File 'lib/sandal/sig/hs.rb', line 18 def initialize(sha_size, key) @name = "HS#{sha_size}" @digest = OpenSSL::Digest.new("sha#{sha_size}") @key = key end |
Instance Attribute Details
#name ⇒ String (readonly)
Returns The JWA name of the algorithm.
11 12 13 |
# File 'lib/sandal/sig/hs.rb', line 11 def name @name end |
Instance Method Details
#sign(payload) ⇒ String
Signs a payload and returns the signature.
28 29 30 |
# File 'lib/sandal/sig/hs.rb', line 28 def sign(payload) OpenSSL::HMAC.digest(@digest, @key, payload) end |
#valid?(signature, payload) ⇒ Boolean
Validates a payload signature and returns whether the signature matches.
37 38 39 |
# File 'lib/sandal/sig/hs.rb', line 37 def valid?(signature, payload) jwt_strings_equal?(sign(payload), signature) end |