Sha256: 78a5e0cb8c2f5f0eaad58fd550fca7c59c77571fb86173d40e07448e48fc1aec

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

require 'openssl'
require 'base64'

module SimpleScripting

  module Configuration

    # The purpose of encryption in this library is just to avoid displaying passwords in
    # plaintext; it's not considered safe against attacks.
    #
    class Value < String

      ENCRYPTION_CIPHER = 'des3'

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

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

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

      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

      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

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_scripting-0.9.0 lib/simple_scripting/configuration/value.rb