Class: EncryptedAttributes::ShaCipher

Inherits:
EncryptedStrings::ShaCipher
  • Object
show all
Defined in:
lib/encrypted_attributes/sha_cipher.rb

Overview

Adds support for embedding salts in the encrypted value

Instance Method Summary collapse

Constructor Details

#initialize(value, options = {}) ⇒ ShaCipher

Encrypts a string using a Secure Hash Algorithm (SHA), specifically SHA-1.

Configuration options:

  • :salt - Random bytes used as one of the inputs for generating the encrypted string

  • :embed_salt - Whether to embed the salt directly within the encrypted value. Default is false. This is useful for storing both the salt and the encrypted value in the same attribute.



12
13
14
15
16
17
18
19
20
# File 'lib/encrypted_attributes/sha_cipher.rb', line 12

def initialize(value, options = {}) #:nodoc:
  if @embed_salt = options.delete(:embed_salt)
    # The salt is at the end of the value
    salt = value[40..-1]
    options[:salt] = salt unless salt.blank?
  end
  
  super(options)
end

Instance Method Details

#encrypt(data) ⇒ Object

Encrypts the data, embedding the salt at the end of the string if configured to do so



24
25
26
27
28
# File 'lib/encrypted_attributes/sha_cipher.rb', line 24

def encrypt(data)
  encrypted_data = super
  encrypted_data << salt if @embed_salt
  encrypted_data
end