Class: Sentry::ShaSentry

Inherits:
Object
  • Object
show all
Defined in:
lib/sentry/sha_sentry.rb

Constant Summary collapse

@@salt =
'salt'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute = nil) ⇒ ShaSentry

Initialize the class.

Used by ActiveRecord::Base#generates_crypted to set up as a callback object for a model



14
15
16
# File 'lib/sentry/sha_sentry.rb', line 14

def initialize(attribute = nil)
  @attribute = attribute
end

Instance Attribute Details

#saltObject

Returns the value of attribute salt.



5
6
7
# File 'lib/sentry/sha_sentry.rb', line 5

def salt
  @salt
end

Class Method Details

.encrypt(data) ⇒ Object

Encrypts the data



36
37
38
# File 'lib/sentry/sha_sentry.rb', line 36

def encrypt(data)
  Digest::SHA1.hexdigest(data + @@salt)
end

.saltObject

Gets the class salt value used when encrypting



26
27
28
# File 'lib/sentry/sha_sentry.rb', line 26

def salt
  @@salt
end

.salt=(value) ⇒ Object

Sets the class salt value used when encrypting



31
32
33
# File 'lib/sentry/sha_sentry.rb', line 31

def salt=(value)
  @@salt = value
end

Instance Method Details

#before_validation(model) ⇒ Object

Performs encryption on before_validation Active Record callback



19
20
21
22
# File 'lib/sentry/sha_sentry.rb', line 19

def before_validation(model)
  return unless model.send(@attribute)
  model.send("crypted_#{@attribute}=", encrypt(model.send(@attribute)))
end

#encrypt(data) ⇒ Object

Encrypts data using SHA.



8
9
10
# File 'lib/sentry/sha_sentry.rb', line 8

def encrypt(data)
  self.class.encrypt(data + salt.to_s)
end