Module: EncryptedAttributes::InstanceMethods

Defined in:
lib/encryptedattributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cipher_ivObject

reader for @cipher_iv that returns a default value if none exists



45
46
47
# File 'lib/encryptedattributes.rb', line 45

def cipher_iv
  @cipher_iv ||= "1234567812345678"  # 16 bytes (128 bits) of data
end

#cipher_keyObject

Returns the value of attribute cipher_key.



42
43
44
# File 'lib/encryptedattributes.rb', line 42

def cipher_key
  @cipher_key
end

#encryptedObject

Returns the value of attribute encrypted.



42
43
44
# File 'lib/encryptedattributes.rb', line 42

def encrypted
  @encrypted
end

Instance Method Details

#credentialsObject

Marshals the @cipher_key and @cipher_iv and returns it in base64



71
72
73
# File 'lib/encryptedattributes.rb', line 71

def credentials
  Base64.encode64(Marshal.dump([self.cipher_key, self.cipher_iv]))
end

#credentials=(credentials) ⇒ Object

Unmarshals supplied base64 data into @cipher_key and @cipher_iv



76
77
78
# File 'lib/encryptedattributes.rb', line 76

def credentials=(credentials)
  (@cipher_key, @cipher_iv) = Marshal.load(Base64.decode64(credentials))
end

#crypto_hash(data) ⇒ Object

Generates a 256-bit hash from the supplied data



81
82
83
# File 'lib/encryptedattributes.rb', line 81

def crypto_hash(data)
  Digest::SHA256.digest(data)
end

#crypto_hash64(data) ⇒ Object

Returns a base64 encoded version of crypto_hash



86
87
88
# File 'lib/encryptedattributes.rb', line 86

def crypto_hash64(data)
  Base64.encode64(crypto_hash(data))
end

#decrypt_allObject

Decrypts all encrypted attributes with the crypt_all private method



53
# File 'lib/encryptedattributes.rb', line 53

def decrypt_all; crypt_all(:decrypt); end

#encrypt_allObject

Encrypts all encrypted attributes with the crypt_all private method



50
# File 'lib/encryptedattributes.rb', line 50

def encrypt_all; crypt_all(:encrypt); end

#key_from_password(password, salt = '') ⇒ Object

Takes the supplied password and optional salt and sets the @cipher_key



56
57
58
# File 'lib/encryptedattributes.rb', line 56

def key_from_password(password, salt='')
  @cipher_key = crypto_hash(salt+password)
end

#random_ivObject

Generates a random IV



66
67
68
# File 'lib/encryptedattributes.rb', line 66

def random_iv
  @cipher_iv = self.class.random_iv
end

#random_keyObject

Generates a random 256-bit hash



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

def random_key
  @cipher_key = self.class.random_key
end