Module: EasyType::Encryption

Included in:
CatalogData, NodeGroupData
Defined in:
lib/easy_type/encryption.rb

Overview

Docs

Instance Method Summary collapse

Instance Method Details

#cipherObject



11
12
13
# File 'lib/easy_type/encryption.rb', line 11

def cipher
  @cipher ||= OpenSSL::Cipher.new('AES-128-CBC')
end

#decrypt(value) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/easy_type/encryption.rb', line 35

def decrypt(value)
  # Fetch encryption parts from value
  encrypted_string = value[5..-1]
  crypted_iv, crypted_value = encrypted_string.split('|')
  raise ArgumentError, 'invalid value for decryption' unless crypted_iv && crypted_value
  iv = Base64.decode64(crypted_iv)
  value = Base64.decode64(crypted_value)

  # Decrypt value
  @cipher = nil
  cipher.decrypt
  cipher.key = encryption_key
  cipher.iv = iv
  cipher.update(value) + cipher.final
end

#decrypted_value(value) ⇒ Object



31
32
33
# File 'lib/easy_type/encryption.rb', line 31

def decrypted_value(value)
  encrypted?(value) ? decrypt(value) : value
end

#encrypt(value) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/easy_type/encryption.rb', line 22

def encrypt(value)
  @cipher = nil
  cipher.encrypt
  cipher.key = encryption_key
  iv = cipher.random_iv
  encrypted = cipher.update(value.to_s) << cipher.final
  "{AES}#{Base64.encode64(iv).chop}|#{Base64.encode64(encrypted).chop}"
end

#encrypted?(value) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/easy_type/encryption.rb', line 51

def encrypted?(value)
  #
  # For now we only support one sort of encryption. Might change in the future
  #
  value[0..4] == '{AES}'
end

#encryption_keyObject

For now we use a standard decryption key



18
19
20
# File 'lib/easy_type/encryption.rb', line 18

def encryption_key
  'zEjryBhUkwDnQAKY'
end