Class: Gloo::Objs::Cipher

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/security/cipher.rb

Constant Summary collapse

KEYWORD =
'cipher'.freeze
KEYWORD_SHORT =
'crypt'.freeze
CIPHER_TYPE =
'AES-256-CBC'.freeze
KEY =
'key'.freeze
INIT_VECTOR =
'init_vector'.freeze
DATA =
'data'.freeze

Constants inherited from Core::Baseo

Core::Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary

Attributes inherited from Core::Obj

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Obj

#add_child, can_create?, #can_receive_message?, #child_count, #child_index, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, #find_child_resolve_alias, #find_child_value, help, inherited, #initialize, #is_alias?, #is_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_unload, #multiline_value?, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #set_value, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Obj

Class Method Details

.decrypt(data, key, iv) ⇒ Object

Decrypt the data using the key and initialization vector. Returns the decrypted data.



181
182
183
184
185
186
187
188
189
# File 'lib/gloo/objs/security/cipher.rb', line 181

def self.decrypt( data, key, iv )
  cipher = OpenSSL::Cipher.new( CIPHER_TYPE )
  data = Base64.decode64( data )
  cipher.decrypt
  cipher.key = Base64.decode64( key )
  cipher.iv = Base64.decode64( iv ) unless iv.blank?

  return cipher.update( data ) + cipher.final
end

.encrypt(data, key, iv) ⇒ Object

Encrypt the data using the key and initialization vector. Returns the encrypted data (base64 encoded).



167
168
169
170
171
172
173
174
175
# File 'lib/gloo/objs/security/cipher.rb', line 167

def self.encrypt( data, key, iv )
  cipher = OpenSSL::Cipher.new( CIPHER_TYPE )
  cipher.encrypt
  cipher.key = Base64.decode64( key )
  cipher.iv = Base64.decode64( iv ) unless iv.blank?

  encrypted_msg = cipher.update( data ) + cipher.final
  return Base64.encode64( encrypted_msg )
end

.messagesObject

Get a list of message names that this object receives.



126
127
128
# File 'lib/gloo/objs/security/cipher.rb', line 126

def self.messages
  return super + %w[generate_keys encrypt decrypt]
end

.short_typenameObject

The short name of the object type.



31
32
33
# File 'lib/gloo/objs/security/cipher.rb', line 31

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



24
25
26
# File 'lib/gloo/objs/security/cipher.rb', line 24

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:



103
104
105
# File 'lib/gloo/objs/security/cipher.rb', line 103

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



112
113
114
115
116
117
# File 'lib/gloo/objs/security/cipher.rb', line 112

def add_default_children
  fac = @engine.factory
  fac.create_string KEY, '', self
  fac.create_string INIT_VECTOR, '', self
  fac.create_string DATA, '', self
end

#dataObject

Get the data value of the object. This might be encrypted or decrypted based on what action was last taken.



58
59
60
61
# File 'lib/gloo/objs/security/cipher.rb', line 58

def data
  o = find_child DATA
  return o&.value
end

#init_vectorObject

Get the Initialization Vector. Returns nil if there is none.



48
49
50
51
# File 'lib/gloo/objs/security/cipher.rb', line 48

def init_vector
  o = find_child INIT_VECTOR
  return o&.value
end

#keyObject

Get the Cipher Key. Returns nil if there is none.



39
40
41
42
# File 'lib/gloo/objs/security/cipher.rb', line 39

def key
  o = find_child KEY
  return o&.value
end

#msg_decryptObject

Decrypt the encrypted child object.



148
149
150
# File 'lib/gloo/objs/security/cipher.rb', line 148

def msg_decrypt
  update_data Cipher.decrypt( data, key, init_vector )
end

#msg_encryptObject

Encrypt the decrypted child object.



155
156
157
# File 'lib/gloo/objs/security/cipher.rb', line 155

def msg_encrypt
  update_data Cipher.encrypt( data, key, init_vector )
end

#msg_generate_keysObject

Generate random Key and Initialization Vector.



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/gloo/objs/security/cipher.rb', line 133

def msg_generate_keys
  cipher = OpenSSL::Cipher.new( CIPHER_TYPE )

  key = cipher.random_key
  key = Base64.encode64 key
  update_key key

  iv = update_init_vector cipher.random_iv
  iv = Base64.encode64 iv
  update_init_vector iv
end

#update_data(new_val) ⇒ Object

Update the data value of the object.



86
87
88
89
90
91
# File 'lib/gloo/objs/security/cipher.rb', line 86

def update_data( new_val )
  o = find_child DATA
  return unless o

  o.set_value new_val
end

#update_init_vector(new_val) ⇒ Object

Update the initialization vector value.



76
77
78
79
80
81
# File 'lib/gloo/objs/security/cipher.rb', line 76

def update_init_vector( new_val )
  o = find_child INIT_VECTOR
  return unless o

  o.set_value new_val
end

#update_key(new_val) ⇒ Object

Update the key value.



66
67
68
69
70
71
# File 'lib/gloo/objs/security/cipher.rb', line 66

def update_key( new_val )
  o = find_child KEY
  return unless o

  o.set_value new_val
end