Class: NoPassword::Encryptor

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

Overview

Handle encrypting and decrypting secrets.

Constant Summary collapse

KEY_LENGTH =
ActiveSupport::MessageEncryptor.key_len

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret_key:, salt: self.class.generate_salt, key_length: KEY_LENGTH) ⇒ Encryptor

Returns a new instance of Encryptor.



6
7
8
9
10
11
12
13
# File 'lib/nopassword/encryptor.rb', line 6

def initialize(secret_key:, salt: self.class.generate_salt, key_length: KEY_LENGTH)
  raise "salt can't be nil" if salt.nil?
  raise "secret_key can't be nil" if secret_key.nil?

  # binding.pry if secret_key.nil?
  key = ActiveSupport::KeyGenerator.new(secret_key).generate_key(salt, key_length)
  @crypt = ActiveSupport::MessageEncryptor.new(key)
end

Class Method Details

.generate_salt(key_length: KEY_LENGTH) ⇒ Object



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

def self.generate_salt(key_length: KEY_LENGTH)
  SecureRandom.urlsafe_base64 key_length
end

Instance Method Details

#decrypt_and_verify(encrypted_data, *args, **kwargs) ⇒ Object



19
20
21
# File 'lib/nopassword/encryptor.rb', line 19

def decrypt_and_verify(encrypted_data, *args, **kwargs)
  @crypt.decrypt_and_verify(encrypted_data, *args, **kwargs)
end

#encrypt_and_sign(decrypted_data, *args, **kwargs) ⇒ Object



15
16
17
# File 'lib/nopassword/encryptor.rb', line 15

def encrypt_and_sign(decrypted_data, *args, **kwargs)
  @crypt.encrypt_and_sign(decrypted_data, *args, **kwargs)
end