Class: SimpleScripting::Configuration::Value

Inherits:
String
  • Object
show all
Defined in:
lib/simple_scripting/configuration/value.rb

Overview

The purpose of encryption in this library is just to avoid displaying passwords in plaintext; it’s not considered safe against attacks.

Constant Summary collapse

ENCRYPTION_CIPHER =
'des3'

Instance Method Summary collapse

Constructor Details

#initialize(string, encryption_key = nil) ⇒ Value

Returns a new instance of Value.



15
16
17
18
19
20
21
# File 'lib/simple_scripting/configuration/value.rb', line 15

def initialize(string, encryption_key = nil)
  super(string)

  if encryption_key
    @encryption_key = encryption_key + '*' * (24 - encryption_key.bytesize)
  end
end

Instance Method Details

#decryptedObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/simple_scripting/configuration/value.rb', line 31

def decrypted
  raise "Encryption key not provided!" if @encryption_key.nil?

  ciphertext = Base64.decode64(self)

  cipher = OpenSSL::Cipher::Cipher.new(ENCRYPTION_CIPHER)
  cipher.decrypt

  cipher.key = @encryption_key

  cipher.iv = ciphertext[0...cipher.iv_len]
  plaintext = cipher.update(ciphertext[cipher.iv_len..-1]) + cipher.final

  plaintext
end

#encryptedObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/simple_scripting/configuration/value.rb', line 47

def encrypted
  cipher = OpenSSL::Cipher::Cipher.new(ENCRYPTION_CIPHER)
  cipher.encrypt

  iv = cipher.random_iv

  cipher.key = @encryption_key
  cipher.iv  = iv

  ciphertext = iv + cipher.update(self) + cipher.final

  Base64.encode64(ciphertext).rstrip
end

#full_pathObject



23
24
25
# File 'lib/simple_scripting/configuration/value.rb', line 23

def full_path
  start_with?('/') ? self : File.expand_path(self, '~')
end

#full_pathsObject



27
28
29
# File 'lib/simple_scripting/configuration/value.rb', line 27

def full_paths
  split(':').map { |value| self.class.new(value).full_path }
end