Class: Aes256CbcEncryptor
- Inherits:
-
Object
- Object
- Aes256CbcEncryptor
- Defined in:
- lib/aes_256_cbc_encryptor.rb
Instance Attribute Summary collapse
-
#iv ⇒ Object
readonly
Returns the value of attribute iv.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Instance Method Summary collapse
- #cipher(type) ⇒ Object private
- #decrypt(encrypted) ⇒ Object
- #decryption_cipher ⇒ Object private
- #encrypt(payload) ⇒ Object
- #encryption_cipher ⇒ Object private
-
#initialize(hex_secret, hex_iv) ⇒ Aes256CbcEncryptor
constructor
A new instance of Aes256CbcEncryptor.
Constructor Details
#initialize(hex_secret, hex_iv) ⇒ Aes256CbcEncryptor
Returns a new instance of Aes256CbcEncryptor.
4 5 6 7 8 9 |
# File 'lib/aes_256_cbc_encryptor.rb', line 4 def initialize(hex_secret, hex_iv) @secret = [hex_secret].pack('H*') @iv = [hex_iv].pack('H*') raise ArgumentError, 'Secret must be 32 bytes.' unless @secret.length == 32 raise ArgumentError, 'IV must be 16 bytes.' unless @iv.length == 16 end |
Instance Attribute Details
#iv ⇒ Object (readonly)
Returns the value of attribute iv.
11 12 13 |
# File 'lib/aes_256_cbc_encryptor.rb', line 11 def iv @iv end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
11 12 13 |
# File 'lib/aes_256_cbc_encryptor.rb', line 11 def secret @secret end |
Instance Method Details
#cipher(type) ⇒ Object (private)
35 36 37 38 39 40 41 |
# File 'lib/aes_256_cbc_encryptor.rb', line 35 def cipher(type) cipher = OpenSSL::Cipher.new('AES-256-CBC') cipher.send(type) cipher.key = @secret cipher.iv = @iv cipher end |
#decrypt(encrypted) ⇒ Object
19 20 21 22 23 |
# File 'lib/aes_256_cbc_encryptor.rb', line 19 def decrypt(encrypted) cipher = decryption_cipher encrypted_data = Base64.urlsafe_decode64(encrypted) cipher.update(encrypted_data) + cipher.final end |
#decryption_cipher ⇒ Object (private)
31 32 33 |
# File 'lib/aes_256_cbc_encryptor.rb', line 31 def decryption_cipher cipher(:decrypt) end |
#encrypt(payload) ⇒ Object
13 14 15 16 17 |
# File 'lib/aes_256_cbc_encryptor.rb', line 13 def encrypt(payload) cipher = encryption_cipher encrypted = cipher.update(payload) + cipher.final Base64.urlsafe_encode64(encrypted) end |
#encryption_cipher ⇒ Object (private)
27 28 29 |
# File 'lib/aes_256_cbc_encryptor.rb', line 27 def encryption_cipher cipher(:encrypt) end |