Class: Auxilium::EasyCrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/auxilium/easy_crypt.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_base, digest: OpenSSL::Digest::SHA1) ⇒ EasyCrypt

When no digest is given, the default Rails digest is used. Digest can be something like OpenSSL::Digest::SHA1 or OpenSSL::Digest::SHA256



5
6
7
8
# File 'lib/auxilium/easy_crypt.rb', line 5

def initialize(key_base, digest: OpenSSL::Digest::SHA1)
  @key_base = key_base
  @digest = digest
end

Class Method Details

.dump(text) ⇒ Object



57
58
59
# File 'lib/auxilium/easy_crypt.rb', line 57

def dump(text)
  new(Rails.application.credentials.secret_key_base).encrypt(text)
end

.load(text) ⇒ Object



61
62
63
# File 'lib/auxilium/easy_crypt.rb', line 61

def load(text)
  new(Rails.application.credentials.secret_key_base).decrypt(text)
end

Instance Method Details

#decrypt(data) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/auxilium/easy_crypt.rb', line 22

def decrypt(data)
  return data unless crypted?(data)

  salt, data = data.gsub(/^EasyCrypt@/, '').split("$$", 2)

  crypt(salt).decrypt_and_verify(data)
end

#encrypt(text) ⇒ Object

Raises:

  • (StandardError)


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/auxilium/easy_crypt.rb', line 10

def encrypt(text)
  text = text.to_s unless text.is_a?(String)

  # Return the crypted text is it was already crypted by this routine
  return text if crypted?(text)
  return text if text.blank?

  data = encrypted(text)
  raise StandardError, 'WTF' unless decrypt(data)
  data
end