Class: Aes256CbcEncryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/aes_256_cbc_encryptor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hex_secret, hex_iv) ⇒ Aes256CbcEncryptor

Returns a new instance of Aes256CbcEncryptor.

Raises:

  • (ArgumentError)


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

#ivObject (readonly)

Returns the value of attribute iv.



11
12
13
# File 'lib/aes_256_cbc_encryptor.rb', line 11

def iv
  @iv
end

#secretObject (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

#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

#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