Class: EasyCipher::Cipher
- Inherits:
-
Object
- Object
- EasyCipher::Cipher
- Defined in:
- lib/easy_cipher/cipher.rb
Overview
The cipher class
Instance Attribute Summary collapse
-
#iv ⇒ Object
readonly
Returns the value of attribute iv.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
-
.new64(key, iv) ⇒ Object
Create a new cipher instance.
Instance Method Summary collapse
-
#decrypt(encrypted_data) ⇒ String
Decrypt the given data.
-
#encrypt(data) ⇒ String
Encrypt the given data.
-
#initialize(key = nil, iv = nil) ⇒ Cipher
constructor
Create a new cipher instance.
-
#iv64 ⇒ Object
Return the iv in base64 (e.g. to store in string fields).
-
#key64 ⇒ Object
Return the key in base64 (e.g. to store in string fields).
Constructor Details
#initialize(key = nil, iv = nil) ⇒ Cipher
Create a new cipher instance.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/easy_cipher/cipher.rb', line 11 def initialize(key = nil, iv = nil) if (key && !iv) || (!key && iv) raise ArgumentError, "Must supply key AND iv, or neither." end @key = key @iv = iv @encryptor = ::OpenSSL::Cipher::AES256.new(:CBC) @encryptor.encrypt if @key @encryptor.key = @key else @key = @encryptor.random_key end if @iv @encryptor.iv = @iv else @iv = @encryptor.random_iv end @decryptor = ::OpenSSL::Cipher::AES256.new(:CBC) @decryptor.decrypt @decryptor.key = @key @decryptor.iv = @iv end |
Instance Attribute Details
#iv ⇒ Object (readonly)
Returns the value of attribute iv.
6 7 8 |
# File 'lib/easy_cipher/cipher.rb', line 6 def iv @iv end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
6 7 8 |
# File 'lib/easy_cipher/cipher.rb', line 6 def key @key end |
Class Method Details
.new64(key, iv) ⇒ Object
Create a new cipher instance.
43 44 45 |
# File 'lib/easy_cipher/cipher.rb', line 43 def self.new64(key, iv) return self.new(Base64.decode64(key), Base64.decode64(iv)) end |
Instance Method Details
#decrypt(encrypted_data) ⇒ String
Decrypt the given data.
68 69 70 |
# File 'lib/easy_cipher/cipher.rb', line 68 def decrypt(encrypted_data) return decrypt_line(Base64.decode64(encrypted_data)) end |
#encrypt(data) ⇒ String
Encrypt the given data.
60 61 62 |
# File 'lib/easy_cipher/cipher.rb', line 60 def encrypt(data) return Base64.encode64(encrypt_line(data, true)) end |
#iv64 ⇒ Object
Return the iv in base64 (e.g. to store in string fields)
53 54 55 |
# File 'lib/easy_cipher/cipher.rb', line 53 def iv64 return Base64.encode64(@iv) end |
#key64 ⇒ Object
Return the key in base64 (e.g. to store in string fields)
48 49 50 |
# File 'lib/easy_cipher/cipher.rb', line 48 def key64 return Base64.encode64(@key) end |