Class: Sandal::Sig::RS

Inherits:
Object
  • Object
show all
Defined in:
lib/sandal/sig/rs.rb

Overview

Base implementation of the RSA-SHA family of signature algorithms.

Direct Known Subclasses

RS256, RS384, RS512

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sha_size, key) ⇒ RS

Creates a new instance; it’s probably easier to use one of the subclass constructors.

Parameters:

  • sha_size (Integer)

    The size of the SHA algorithm.

  • key (OpenSSL::PKey::RSA)

    The key to use for signing (private) or validation (public). This must be at least 2048 bits to be compliant with the JWA specification.



17
18
19
20
21
# File 'lib/sandal/sig/rs.rb', line 17

def initialize(sha_size, key)
  @name = "RS#{sha_size}"
  @digest = OpenSSL::Digest.new("sha#{sha_size}")
  @key = key
end

Instance Attribute Details

#nameString (readonly)

Returns The JWA name of the algorithm.

Returns:

  • (String)

    The JWA name of the algorithm.



10
11
12
# File 'lib/sandal/sig/rs.rb', line 10

def name
  @name
end

Instance Method Details

#sign(payload) ⇒ String

Signs a payload and returns the signature.

Parameters:

  • payload (String)

    The payload of the token to sign.

Returns:

  • (String)

    The signature.



27
28
29
# File 'lib/sandal/sig/rs.rb', line 27

def sign(payload)
  @key.sign(@digest, payload)
end

#valid?(signature, payload) ⇒ Boolean

Validates a payload signature and returns whether the signature matches.

Parameters:

  • signature (String)

    The signature to verify.

  • payload (String)

    The payload of the token.

Returns:

  • (Boolean)

    true if the signature is correct; otherwise false.



36
37
38
# File 'lib/sandal/sig/rs.rb', line 36

def valid?(signature, payload)
  @key.verify(@digest, signature, payload)
end