Class: Sandal::Enc::Alg::RSA

Inherits:
Object
  • Object
show all
Defined in:
lib/sandal/enc/alg/rsa.rb

Overview

Base class for RSA key encryption algorithm.

Direct Known Subclasses

RSA1_5, RSA_OAEP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, rsa_key, padding) ⇒ RSA

Initialises a new instance.

(private). If the value is a String then it will be passed to the constructor of the RSA class. This must be at least 2048 bits to be compliant with the JWA specification.

Parameters:

  • name (String)

    The JWA name of the algorithm.

  • rsa_key (OpenSSL::PKey::RSA or String)

    The RSA key to use for key encryption (public) or decryption



19
20
21
22
23
# File 'lib/sandal/enc/alg/rsa.rb', line 19

def initialize(name, rsa_key, padding)
  @name = name
  @rsa_key = rsa_key.is_a?(String) ? OpenSSL::PKey::RSA.new(rsa_key) : rsa_key
  @padding = padding
end

Instance Attribute Details

#nameObject (readonly)

The JWA name of the algorithm.



11
12
13
# File 'lib/sandal/enc/alg/rsa.rb', line 11

def name
  @name
end

Instance Method Details

#decrypt_key(encrypted_key) ⇒ String

Decrypts the content key.

Parameters:

  • encrypted_key (String)

    The encrypted content key.

Returns:

  • (String)

    The pre-shared content key.

Raises:



38
39
40
41
42
# File 'lib/sandal/enc/alg/rsa.rb', line 38

def decrypt_key(encrypted_key)
  @rsa_key.private_decrypt(encrypted_key, @padding)
rescue => e
  raise Sandal::InvalidTokenError, "Cannot decrypt content key: #{e.message}"
end

#encrypt_key(key) ⇒ String

Encrypts the content key.

Parameters:

  • key (String)

    The content key.

Returns:

  • (String)

    The encrypted content key.



29
30
31
# File 'lib/sandal/enc/alg/rsa.rb', line 29

def encrypt_key(key)
  @rsa_key.public_encrypt(key, @padding)
end