Class: JSON::JWE

Inherits:
JWT
  • Object
show all
Defined in:
lib/json/jwe.rb

Defined Under Namespace

Classes: DecryptionFailed, InvalidFormat, UnexpectedAlgorithm

Instance Attribute Summary collapse

Attributes inherited from JWT

#header, #signature

Instance Method Summary collapse

Methods inherited from JWT

decode, #encrypt, register_header_keys, #sign, #verify

Constructor Details

#initialize(input) ⇒ JWE

Returns a new instance of JWE.



19
20
21
# File 'lib/json/jwe.rb', line 19

def initialize(input)
  self.input = input.to_s
end

Instance Attribute Details

#cipher_textObject

Returns the value of attribute cipher_text.



10
11
12
# File 'lib/json/jwe.rb', line 10

def cipher_text
  @cipher_text
end

#encrypted_master_key=(value) ⇒ Object

Sets the attribute encrypted_master_key

Parameters:

  • value

    the value to set the attribute encrypted_master_key to.



10
11
12
# File 'lib/json/jwe.rb', line 10

def encrypted_master_key=(value)
  @encrypted_master_key = value
end

#encryption_keyObject

Returns the value of attribute encryption_key.



10
11
12
# File 'lib/json/jwe.rb', line 10

def encryption_key
  @encryption_key
end

#inputObject

Returns the value of attribute input.



10
11
12
# File 'lib/json/jwe.rb', line 10

def input
  @input
end

#integrity_keyObject

Returns the value of attribute integrity_key.



10
11
12
# File 'lib/json/jwe.rb', line 10

def integrity_key
  @integrity_key
end

#integrity_value=(value) ⇒ Object

Sets the attribute integrity_value

Parameters:

  • value

    the value to set the attribute integrity_value to.



10
11
12
# File 'lib/json/jwe.rb', line 10

def integrity_value=(value)
  @integrity_value = value
end

#ivObject

Returns the value of attribute iv.



10
11
12
# File 'lib/json/jwe.rb', line 10

def iv
  @iv
end

#master_keyObject

Returns the value of attribute master_key.



10
11
12
# File 'lib/json/jwe.rb', line 10

def master_key
  @master_key
end

#modeObject

Returns the value of attribute mode.



10
11
12
# File 'lib/json/jwe.rb', line 10

def mode
  @mode
end

#plain_textObject

Returns the value of attribute plain_text.



10
11
12
# File 'lib/json/jwe.rb', line 10

def plain_text
  @plain_text
end

#private_key_or_secretObject

Returns the value of attribute private_key_or_secret.



10
11
12
# File 'lib/json/jwe.rb', line 10

def private_key_or_secret
  @private_key_or_secret
end

#public_key_or_secretObject

Returns the value of attribute public_key_or_secret.



10
11
12
# File 'lib/json/jwe.rb', line 10

def public_key_or_secret
  @public_key_or_secret
end

Instance Method Details

#decrypt!(private_key_or_secret) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/json/jwe.rb', line 33

def decrypt!(private_key_or_secret)
  self.mode = :decryption
  self.private_key_or_secret = private_key_or_secret
  decode_segments!
  cipher.decrypt
  restore_cipher_keys!
  self.plain_text = cipher.update(cipher_text) + cipher.final
  verify_cbc_integirity_value! if cbc?
  self
end

#encrypt!(public_key_or_secret) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/json/jwe.rb', line 23

def encrypt!(public_key_or_secret)
  self.mode = :encyption
  self.plain_text = input
  self.public_key_or_secret = public_key_or_secret
  cipher.encrypt
  generate_cipher_keys!
  self.cipher_text = cipher.update(plain_text) + cipher.final
  self
end

#to_sObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/json/jwe.rb', line 44

def to_s
  if mode == :encyption
    [
      header.to_json,
      encrypted_master_key,
      iv,
      cipher_text,
      integrity_value
    ].collect do |segment|
      UrlSafeBase64.encode64 segment.to_s
    end.join('.')
  else
    plain_text
  end
end