Class: QuoVadis::Crypt

Inherits:
Object
  • Object
show all
Defined in:
lib/quo_vadis/crypt.rb

Class Method Summary collapse

Class Method Details

.decrypt(value) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/quo_vadis/crypt.rb', line 16

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

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

.encrypt(value) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/quo_vadis/crypt.rb', line 6

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

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

.encryptor(salt) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/quo_vadis/crypt.rb', line 30

def self.encryptor(salt)
  key_sha256 = key salt, OpenSSL::Digest::SHA256
  key_sha1   = key salt, OpenSSL::Digest::SHA1
  ActiveSupport::MessageEncryptor.new(key_sha256).tap { |crypt|
    crypt.rotate key_sha1
  }
end

.key(salt, hash_digest_class) ⇒ Object



38
39
40
41
42
# File 'lib/quo_vadis/crypt.rb', line 38

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

.secretObject



44
45
46
# File 'lib/quo_vadis/crypt.rb', line 44

def self.secret
  Rails.application.secret_key_base
end