Class: Ibrain::Encryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/ibrain/encryptor.rb

Overview

Ibrain::Encryptor is a thin wrapper around ActiveSupport::MessageEncryptor.

Instance Method Summary collapse

Constructor Details

#initialize(key = Ibrain::Config.ibrain_encryptor_key) ⇒ Encryptor

Returns a new instance of Encryptor.

Parameters:

  • key (String) (defaults to: Ibrain::Config.ibrain_encryptor_key)

    the 256 bits signature key



7
8
9
10
11
# File 'lib/ibrain/encryptor.rb', line 7

def initialize(key = Ibrain::Config.ibrain_encryptor_key)
  key = Rails.application.secrets.secret_key_base.byteslice(0..31) if key.blank?

  @crypt = ActiveSupport::MessageEncryptor.new(key)
end

Instance Method Details

#decrypt(encrypted_value) ⇒ String

Decrypt an encrypted value

Parameters:

  • encrypted_value (String)

    the value to decrypt

Returns:

  • (String)

    the decrypted value



23
24
25
# File 'lib/ibrain/encryptor.rb', line 23

def decrypt(encrypted_value)
  @crypt.decrypt_and_verify(encrypted_value)
end

#encrypt(value) ⇒ String

Encrypt a value

Parameters:

  • value (String)

    the value to encrypt

Returns:

  • (String)

    the encrypted value



16
17
18
# File 'lib/ibrain/encryptor.rb', line 16

def encrypt(value)
  @crypt.encrypt_and_sign(value)
end