Module: Raptcha::Encryptor
Instance Method Summary collapse
- #alg ⇒ Object
- #cycle(plaintext, options = {}) ⇒ Object
- #decrypt(ciphertext, options = {}) ⇒ Object
- #default_key ⇒ Object
- #encrypt(plaintext, options = {}) ⇒ Object
- #key(*key) ⇒ Object
- #key=(key) ⇒ Object
- #salt ⇒ Object
- #salt=(salt) ⇒ Object
Instance Method Details
#alg ⇒ Object
190 191 192 |
# File 'lib/raptcha.rb', line 190 def alg @alg ||= 'AES-256-CBC' end |
#cycle(plaintext, options = {}) ⇒ Object
172 173 174 |
# File 'lib/raptcha.rb', line 172 def cycle(plaintext, = {}) decrypt(encrypt(plaintext, ), ) end |
#decrypt(ciphertext, options = {}) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/raptcha.rb', line 160 def decrypt(ciphertext, = {}) ciphertext = Encoder.decode(ciphertext.to_s) key = [:key] || ['key'] || Encryptor.key alg = [:alg] || ['alg'] || Encryptor.alg salt = [:salt] || ['salt'] || Encryptor.salt dec = OpenSSL::Cipher::Cipher.new(alg) dec.decrypt dec.pkcs5_keyivgen(key, salt) plaintext = dec.update(ciphertext) plaintext << dec.final end |
#default_key ⇒ Object
182 183 184 |
# File 'lib/raptcha.rb', line 182 def default_key Rails.application.config.secret_token end |
#encrypt(plaintext, options = {}) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/raptcha.rb', line 147 def encrypt(plaintext, = {}) plaintext = plaintext.to_s key = [:key] || ['key'] || Encryptor.key alg = [:alg] || ['alg'] || Encryptor.alg salt = [:salt] || ['salt'] || Encryptor.salt enc = OpenSSL::Cipher::Cipher.new(alg) enc.encrypt enc.pkcs5_keyivgen(key, salt) ciphertext = enc.update(plaintext) ciphertext << enc.final Encoder.encode(ciphertext) end |
#key(*key) ⇒ Object
176 177 178 179 180 |
# File 'lib/raptcha.rb', line 176 def key(*key) self.key = key.first.to_s unless key.empty? self.key = default_key unless defined?(@key) @key end |
#key=(key) ⇒ Object
186 187 188 |
# File 'lib/raptcha.rb', line 186 def key=(key) @key = key.to_s[0, 56] end |
#salt ⇒ Object
194 195 196 |
# File 'lib/raptcha.rb', line 194 def salt @salt ||= nil end |
#salt=(salt) ⇒ Object
198 199 200 |
# File 'lib/raptcha.rb', line 198 def salt=(salt) @salt = salt end |