Class: EncryptAttr::Encryptor
- Inherits:
-
Object
- Object
- EncryptAttr::Encryptor
- Defined in:
- lib/encrypt_attr/encryptor.rb
Constant Summary collapse
- CIPHER =
"AES-256-CBC".freeze
Instance Attribute Summary collapse
-
#secret_token ⇒ Object
readonly
Set the encryptor’s secret token.
Class Method Summary collapse
- .decrypt(secret_token, value) ⇒ Object
- .encrypt(secret_token, value) ⇒ Object
- .validate_secret_token(secret_token) ⇒ Object
Instance Method Summary collapse
- #decode(value) ⇒ Object
- #decrypt(value) ⇒ Object
- #encode(value) ⇒ Object
- #encrypt(value) ⇒ Object
-
#initialize(secret_token) ⇒ Encryptor
constructor
A new instance of Encryptor.
Constructor Details
#initialize(secret_token) ⇒ Encryptor
Returns a new instance of Encryptor.
26 27 28 |
# File 'lib/encrypt_attr/encryptor.rb', line 26 def initialize(secret_token) @secret_token = secret_token end |
Instance Attribute Details
#secret_token ⇒ Object (readonly)
Set the encryptor’s secret token.
24 25 26 |
# File 'lib/encrypt_attr/encryptor.rb', line 24 def secret_token @secret_token end |
Class Method Details
.decrypt(secret_token, value) ⇒ Object
19 20 21 |
# File 'lib/encrypt_attr/encryptor.rb', line 19 def self.decrypt(secret_token, value) new(secret_token).decrypt(value) end |
.encrypt(secret_token, value) ⇒ Object
15 16 17 |
# File 'lib/encrypt_attr/encryptor.rb', line 15 def self.encrypt(secret_token, value) new(secret_token).encrypt(value) end |
.validate_secret_token(secret_token) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/encrypt_attr/encryptor.rb', line 5 def self.validate_secret_token(secret_token) return unless secret_token.size < 100 offending_line = caller .reject {|entry| entry.include?(__dir__) || entry.include?("forwardable.rb") } .first[/^(.*?:\d+)/, 1] warn "[encrypt_attribute] secret token must have at least 100 characters (called from #{offending_line})" end |
Instance Method Details
#decode(value) ⇒ Object
65 66 67 |
# File 'lib/encrypt_attr/encryptor.rb', line 65 def decode(value) Base64.decode64(value) end |
#decrypt(value) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/encrypt_attr/encryptor.rb', line 41 def decrypt(value) cipher = OpenSSL::Cipher.new(CIPHER).decrypt key = Digest::SHA256.digest(secret_token) parts = value.split(";") if parts.size == 1 value = decode(value) iv = key[0...cipher.iv_len] else iv = parts.first value = decode(parts.last) end cipher.key = key cipher.iv = iv cipher.update(value) + cipher.final end |
#encode(value) ⇒ Object
61 62 63 |
# File 'lib/encrypt_attr/encryptor.rb', line 61 def encode(value) Base64.encode64(value).chomp end |
#encrypt(value) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/encrypt_attr/encryptor.rb', line 30 def encrypt(value) cipher = OpenSSL::Cipher.new(CIPHER).encrypt key = Digest::SHA256.digest(secret_token) iv = SecureRandom.random_bytes(cipher.iv_len).unpack("H*").first[0...cipher.iv_len] cipher.key = key cipher.iv = iv iv + ";" + encode(cipher.update(value) + cipher.final) end |