Class: SimpleAES
- Inherits:
-
Object
- Object
- SimpleAES
- Defined in:
- lib/simple_aes.rb
Instance Attribute Summary collapse
-
#cipher ⇒ Object
readonly
Returns the value of attribute cipher.
-
#iv ⇒ Object
readonly
Returns the value of attribute iv.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
- #decrypt(encrypted_data) ⇒ Object
- #encrypt(data) ⇒ Object
-
#initialize(args = {}) ⇒ SimpleAES
constructor
A new instance of SimpleAES.
Constructor Details
#initialize(args = {}) ⇒ SimpleAES
Returns a new instance of SimpleAES.
6 7 8 9 10 |
# File 'lib/simple_aes.rb', line 6 def initialize(args={}) @key = args.fetch(:key) @iv = args.fetch(:iv) @cipher = args[:cypher] || 'AES-128-CBC' end |
Instance Attribute Details
#cipher ⇒ Object (readonly)
Returns the value of attribute cipher.
4 5 6 |
# File 'lib/simple_aes.rb', line 4 def cipher @cipher end |
#iv ⇒ Object (readonly)
Returns the value of attribute iv.
4 5 6 |
# File 'lib/simple_aes.rb', line 4 def iv @iv end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
4 5 6 |
# File 'lib/simple_aes.rb', line 4 def key @key end |
Instance Method Details
#decrypt(encrypted_data) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/simple_aes.rb', line 12 def decrypt(encrypted_data) encrypted_data = Array(encrypted_data).pack('H*') aes = OpenSSL::Cipher::Cipher.new(cipher) aes.decrypt aes.key = key aes.iv = iv aes.update(encrypted_data) + aes.final end |
#encrypt(data) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/simple_aes.rb', line 21 def encrypt(data) aes = OpenSSL::Cipher::Cipher.new(cipher) aes.encrypt aes.key = key aes.iv = iv (aes.update(data) + aes.final).unpack('H*').first end |