Class: Veil::Cipher::V2
- Inherits:
-
Object
- Object
- Veil::Cipher::V2
- Defined in:
- lib/veil/cipher/v2.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(ciphertext) ⇒ Object
- #encrypt(plaintext) ⇒ Object
-
#initialize(opts = {}) ⇒ V2
constructor
A new instance of V2.
- #to_hash ⇒ Object (also: #to_h)
Constructor Details
#initialize(opts = {}) ⇒ V2
Returns a new instance of V2.
9 10 11 12 13 |
# File 'lib/veil/cipher/v2.rb', line 9 def initialize(opts = {}) @cipher = OpenSSL::Cipher.new("aes-256-cbc") @key = opts[:key] ? Base64.strict_decode64(opts[:key]) : cipher.random_key @iv = opts[:iv] ? Base64.strict_decode64(opts[:iv]) : cipher.random_iv end |
Instance Attribute Details
#cipher ⇒ Object (readonly)
Returns the value of attribute cipher.
7 8 9 |
# File 'lib/veil/cipher/v2.rb', line 7 def cipher @cipher end |
#iv ⇒ Object (readonly)
Returns the value of attribute iv.
7 8 9 |
# File 'lib/veil/cipher/v2.rb', line 7 def iv @iv end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/veil/cipher/v2.rb', line 7 def key @key end |
Instance Method Details
#decrypt(ciphertext) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/veil/cipher/v2.rb', line 23 def decrypt(ciphertext) c = cipher c.decrypt c.key = key c.iv = iv JSON.parse(c.update(Base64.strict_decode64(ciphertext)) + c.final, symbolize_names: true) end |
#encrypt(plaintext) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/veil/cipher/v2.rb', line 15 def encrypt(plaintext) c = cipher c.encrypt c.key = key c.iv = iv Base64.strict_encode64(c.update(plaintext) + c.final) end |
#to_hash ⇒ Object Also known as: to_h
31 32 33 34 35 36 37 |
# File 'lib/veil/cipher/v2.rb', line 31 def to_hash { type: self.class.name, key: Base64.strict_encode64(key), iv: Base64.strict_encode64(iv) } end |