Sha256: 526bdc50d92b891030b528da967b2887236201760fde9abcdad0752bcaa0f3ea

Contents?: true

Size: 901 Bytes

Versions: 10

Compression:

Stored size: 901 Bytes

Contents

# frozen_string_literal: true

module QuoVadis
  class Crypt

    def self.encrypt(value)
      return nil if value.nil?
      return '' if value == ''

      salt = SecureRandom.hex KEY_LENGTH
      crypt = encryptor key(salt)
      ciphertext = crypt.encrypt_and_sign value
      [salt, ciphertext].join SEPARATOR
    end

    def self.decrypt(value)
      return nil if value.nil?
      return '' if value == ''

      salt, data = value.split SEPARATOR
      crypt = encryptor key(salt)
      crypt.decrypt_and_verify(data)
    end

    private

    KEY_LENGTH = ActiveSupport::MessageEncryptor.key_len
    SEPARATOR = '$$'

    def self.encryptor(key)
      ActiveSupport::MessageEncryptor.new(key)
    end

    def self.key(salt)
      ActiveSupport::KeyGenerator.new(secret).generate_key(salt, KEY_LENGTH)
    end

    def self.secret
      Rails.application.secret_key_base
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
quo_vadis-2.1.6 lib/quo_vadis/crypt.rb
quo_vadis-2.1.5 lib/quo_vadis/crypt.rb
quo_vadis-2.1.4 lib/quo_vadis/crypt.rb
quo_vadis-2.1.3 lib/quo_vadis/crypt.rb
quo_vadis-2.1.2 lib/quo_vadis/crypt.rb
quo_vadis-2.1.1 lib/quo_vadis/crypt.rb
quo_vadis-2.1.0 lib/quo_vadis/crypt.rb
quo_vadis-2.0.2 lib/quo_vadis/crypt.rb
quo_vadis-2.0.1 lib/quo_vadis/crypt.rb
quo_vadis-2.0.0 lib/quo_vadis/crypt.rb