Class: Sandal::Enc::Alg::Direct

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

Overview

The direct (“dir”) key encryption algorithm, which uses a pre-shared symmetric key.

Constant Summary collapse

NAME =

The JWA name of the algorithm.

"dir"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(preshared_key) ⇒ Direct

Initialises a new instance.

Parameters:

  • preshared_key (String)

    The pre-shared symmetric key.



19
20
21
# File 'lib/sandal/enc/alg/direct.rb', line 19

def initialize(preshared_key)
  @preshared_key = preshared_key
end

Instance Attribute Details

#preshared_keyString (readonly)

Returns The pre-shared symmetric key.

Returns:

  • (String)

    The pre-shared symmetric key.



14
15
16
# File 'lib/sandal/enc/alg/direct.rb', line 14

def preshared_key
  @preshared_key
end

Instance Method Details

#decrypt_key(encrypted_key) ⇒ String

Returns the pre-shared content key.

Parameters:

  • encrypted_key (String)

    The encrypted key.

Returns:

  • (String)

    The pre-shared symmetric key.

Raises:



41
42
43
44
45
46
# File 'lib/sandal/enc/alg/direct.rb', line 41

def decrypt_key(encrypted_key)
  unless encrypted_key.nil? || encrypted_key.empty?
    raise Sandal::InvalidTokenError, "Tokens using direct key exchange must not include a content key."
  end
  @preshared_key
end

#encrypt_key(key) ⇒ String

Returns an empty string as the key is not included in JWE tokens using direct key exchange.

Parameters:

  • key (String)

    This parameter is ignored.

Returns:

  • (String)

    An empty string.



32
33
34
# File 'lib/sandal/enc/alg/direct.rb', line 32

def encrypt_key(key)
  ""
end

#nameObject

The JWA name of the algorithm.



24
25
26
# File 'lib/sandal/enc/alg/direct.rb', line 24

def name
  NAME
end