Class: SimpleEncryptor
- Inherits:
-
Object
- Object
- SimpleEncryptor
- Defined in:
- lib/simple_encryptor.rb
Overview
Simple class to crypt / decrypt strings (url-safe version)
Constant Summary collapse
- VERSION =
Current lib version
'0.0.3'
- ENCRYPTION_TYPE =
Encryption type to use (set as a constant since changing it without changing code is unlikely)
'aes-256-cbc'
Class Method Summary collapse
-
.decrypt(encrypted_secret, key) ⇒ String
Static method to decrypt a message.
-
.encrypt(secret, key, _ = "\0") ⇒ String
Static method to encrypt a message.
Class Method Details
.decrypt(encrypted_secret, key) ⇒ String
Static method to decrypt a message
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/simple_encryptor.rb', line 51 def self.decrypt(encrypted_secret, key) encrypted_secret = Base64.urlsafe_decode64(encrypted_secret) cipher = OpenSSL::Cipher.new(ENCRYPTION_TYPE).decrypt cipher.key = key cipher.iv = encrypted_secret.slice!(0, cipher.iv_len) # initialisation vector cipher.update(encrypted_secret) + cipher.final end |
.encrypt(secret, key, _ = "\0") ⇒ String
Static method to encrypt a message
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/simple_encryptor.rb', line 33 def self.encrypt(secret, key, _ = "\0") cipher = OpenSSL::Cipher.new(ENCRYPTION_TYPE).encrypt cipher.key = key iv = cipher.random_iv # initialisation vector encrypted = cipher.update(secret) + cipher.final encrypted = iv + encrypted Base64.urlsafe_encode64(encrypted, padding: false) end |