Class: App::Encrypter

Inherits:
Object
  • Object
show all
Defined in:
lib/core/encrypter.rb

Instance Method Summary collapse

Constructor Details

#initializeEncrypter

Returns a new instance of Encrypter.



11
12
13
14
15
16
17
18
19
20
# File 'lib/core/encrypter.rb', line 11

def initialize

    @key = App::Config::param(App::Config::CRYPT_KEY)
    @hex = App::Config::param(App::Config::CRYPT_HEX)

    if @key.nil? || @hex.nil?
        App::Terminal::error('Encryption keys not found', ["The command you're trying to run probably requires access to an EC2 database (or some other advanced feature).", "In order for this to work you will need valid #{App::Terminal::format_highlight('encryption keys')}.", "Please speak to #{App::Terminal::format_highlight('Albert')} (or team Raptor) for more info."], true)
    end

end

Instance Method Details

#decrypt(string) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/core/encrypter.rb', line 37

def decrypt(string)

    string_decoded = Base64.decode64(string)
    c = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
    c.decrypt
    c.key = @key = Digest::SHA1.hexdigest(@hex)
    begin
        d = c.update(string_decoded)
        d << c.final
        return d
    rescue
        App::Terminal::error('Decryption failed', "Could not decrypt string #{string == '' ? '[blank]' : App::Terminal::format_highlight(string)} using crypt_key #{App::Terminal::format_highlight(App::Config::param(App::Config::CRYPT_KEY) == '' ? '[blank]' : App::Config::param(App::Config::CRYPT_KEY))} and crypt_hex #{App::Terminal::format_highlight(App::Config::param(App::Config::CRYPT_HEX) == '' ? '[blank]' : App::Config::param(App::Config::CRYPT_HEX))}")
    end

end

#encrypt(string) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/core/encrypter.rb', line 22

def encrypt(string)

    c = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
    c.encrypt
    c.key = @key = Digest::SHA1.hexdigest(@hex)
    begin
        e = c.update(string)
        e << c.final
        return Base64.encode64(e)
    rescue
        App::Terminal::error('Encryption failed', "Could not encrypt string #{string == '' ? '[blank]' : App::Terminal::format_highlight(string)} using crypt_key #{App::Terminal::format_highlight(App::Config::param(App::Config::CRYPT_KEY) == '' ? '[blank]' : App::Config::param(App::Config::CRYPT_KEY))} and crypt_hex #{App::Terminal::format_highlight(App::Config::param(App::Config::CRYPT_HEX) == '' ? '[blank]' : App::Config::param(App::Config::CRYPT_HEX))}")
    end

end