Class: Gloo::Objs::Cipher

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/str_utils/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_container?, #is_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_responds_to?, #msg_unload, #multiline_value?, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #set_value, #sql_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.



178
179
180
181
182
183
184
185
186
# File 'lib/gloo/objs/str_utils/cipher.rb', line 178

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).



164
165
166
167
168
169
170
171
172
# File 'lib/gloo/objs/str_utils/cipher.rb', line 164

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.



123
124
125
# File 'lib/gloo/objs/str_utils/cipher.rb', line 123

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/str_utils/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/str_utils/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:



100
101
102
# File 'lib/gloo/objs/str_utils/cipher.rb', line 100

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.



109
110
111
112
113
114
# File 'lib/gloo/objs/str_utils/cipher.rb', line 109

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.



56
57
58
# File 'lib/gloo/objs/str_utils/cipher.rb', line 56

def data
  return find_child_value DATA
end

#init_vectorObject

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



47
48
49
# File 'lib/gloo/objs/str_utils/cipher.rb', line 47

def init_vector
  return find_child_value INIT_VECTOR
end

#keyObject

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



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

def key
  return find_child_value KEY
end

#msg_decryptObject

Decrypt the encrypted child object.



145
146
147
# File 'lib/gloo/objs/str_utils/cipher.rb', line 145

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

#msg_encryptObject

Encrypt the decrypted child object.



152
153
154
# File 'lib/gloo/objs/str_utils/cipher.rb', line 152

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

#msg_generate_keysObject

Generate random Key and Initialization Vector.



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/gloo/objs/str_utils/cipher.rb', line 130

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

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

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

#update_data(new_val) ⇒ Object

Update the data value of the object.



83
84
85
86
87
88
# File 'lib/gloo/objs/str_utils/cipher.rb', line 83

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

  o.set_value new_val
end

#update_init_vector(new_val) ⇒ Object

Update the initialization vector value.



73
74
75
76
77
78
# File 'lib/gloo/objs/str_utils/cipher.rb', line 73

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

  o.set_value new_val
end

#update_key(new_val) ⇒ Object

Update the key value.



63
64
65
66
67
68
# File 'lib/gloo/objs/str_utils/cipher.rb', line 63

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

  o.set_value new_val
end