Class: ActiveRecordEncryption::Encryptor::Aes256Cbc

Inherits:
Raw
  • Object
show all
Defined in:
lib/active_record_encryption/encryptor/aes_256_cbc.rb

Instance Method Summary collapse

Constructor Details

#initialize(key:, encoding: Encoding::UTF_8) ⇒ Aes256Cbc

Returns a new instance of Aes256Cbc.



8
9
10
11
# File 'lib/active_record_encryption/encryptor/aes_256_cbc.rb', line 8

def initialize(key:, encoding: Encoding::UTF_8)
  @key = key
  @encoding = encoding
end

Instance Method Details

#==(other) ⇒ Object



32
33
34
35
36
# File 'lib/active_record_encryption/encryptor/aes_256_cbc.rb', line 32

def ==(other)
  super &&
    key == other.key &&
    encoding == other.encoding
end

#decrypt(value) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/active_record_encryption/encryptor/aes_256_cbc.rb', line 23

def decrypt(value)
  binary         = Binary.new(value)
  iv             = binary.read(16)
  encrypted_data = binary.read

  decrypted = _decrypt(encrypted_data, key, iv)
  decrypted.force_encoding(encoding)
end

#encrypt(value) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/active_record_encryption/encryptor/aes_256_cbc.rb', line 13

def encrypt(value)
  string = super.dup.force_encoding(encoding)
  encrypted_data, iv = _encrypt(string, key)

  binary = Binary.new
  binary.write(iv) # IV is 16 byte
  binary.write(encrypted_data)
  binary.string
end