Sha256: b4cb037b087825c6bbb28d13491dc2d081cc9b7c3ffdb138316e35f33c42d33e

Contents?: true

Size: 900 Bytes

Versions: 5

Compression:

Stored size: 900 Bytes

Contents

module NoPassword
  # Handle encrypting and decrypting secrets.
  class Encryptor
    KEY_LENGTH = ActiveSupport::MessageEncryptor.key_len

    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

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

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
nopassword-0.1.5 lib/nopassword/encryptor.rb
nopassword-0.1.4 lib/nopassword/encryptor.rb
nopassword-0.1.3 lib/nopassword/encryptor.rb
nopassword-0.1.2 lib/nopassword/encryptor.rb
nopassword-0.1.0 lib/nopassword/encryptor.rb