Class: Sentry::ShaSentry
- Inherits:
-
Object
- Object
- Sentry::ShaSentry
- Defined in:
- lib/sentry/sha_sentry.rb
Constant Summary collapse
- @@salt =
'salt'
Instance Attribute Summary collapse
-
#salt ⇒ Object
Returns the value of attribute salt.
Class Method Summary collapse
-
.encrypt(data) ⇒ Object
Encrypts the data.
-
.salt ⇒ Object
Gets the class salt value used when encrypting.
-
.salt=(value) ⇒ Object
Sets the class salt value used when encrypting.
Instance Method Summary collapse
-
#before_validation(model) ⇒ Object
Performs encryption on before_validation Active Record callback.
-
#encrypt(data) ⇒ Object
Encrypts data using SHA.
-
#initialize(attribute = nil) ⇒ ShaSentry
constructor
Initialize the class.
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
#salt ⇒ Object
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 |
.salt ⇒ Object
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 |