Module: Unidom::Common::Concerns::Aes256Cryptor::ClassMethods

Defined in:
app/models/unidom/common/concerns/aes256_cryptor.rb

Instance Method Summary collapse

Instance Method Details

#aes_256_paddingObject



66
67
68
# File 'app/models/unidom/common/concerns/aes256_cryptor.rb', line 66

def aes_256_padding
  respond_to?(:cryption_padding) ? cryption_padding : 9
end

#decrypt(encoded, key: nil) ⇒ Object

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/unidom/common/concerns/aes256_cryptor.rb', line 52

def decrypt(encoded, key: nil)

  raise ArgumentError.new('The encoded argument is required.') if encoded.blank?
  raise ArgumentError.new('The key argument is required.')     if key.blank?

  cipher = OpenSSL::Cipher::AES.new(256, 'CBC')
  cipher.decrypt
  cipher.padding = aes_256_padding
  cipher.key     = key

  cipher.update(encoded)+cipher.final

end

#encrypt(message, key: nil) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/unidom/common/concerns/aes256_cryptor.rb', line 38

def encrypt(message, key: nil)

  raise ArgumentError.new('The message argument is required.') if message.blank?
  raise ArgumentError.new('The key argument is required.')     if key.blank?

  cipher = OpenSSL::Cipher::AES.new(256, 'CBC')
  cipher.encrypt
  cipher.padding = aes_256_padding
  cipher.key     = key

  cipher.update(message)+cipher.final

end

#encryption_algorithmObject



34
35
36
# File 'app/models/unidom/common/concerns/aes256_cryptor.rb', line 34

def encryption_algorithm
  'AES-256-CBC'
end

#hex_decrypt(encoded, key: nil) ⇒ Object



74
75
76
# File 'app/models/unidom/common/concerns/aes256_cryptor.rb', line 74

def hex_decrypt(encoded, key: nil)
  Unidom::Common::Numeration.hex decrypt(Unidom::Common::Numeration.rev_hex(encoded), key: key)
end

#hex_encrypt(message, key: nil) ⇒ Object



70
71
72
# File 'app/models/unidom/common/concerns/aes256_cryptor.rb', line 70

def hex_encrypt(message, key: nil)
  Unidom::Common::Numeration.hex encrypt(message, key: key)
end