Class: Xml::Kit::Crypto::SymmetricCipher
- Inherits:
-
Object
- Object
- Xml::Kit::Crypto::SymmetricCipher
- Defined in:
- lib/xml/kit/crypto/symmetric_cipher.rb
Constant Summary collapse
- DEFAULT_ALGORITHM =
"#{::Xml::Kit::Namespaces::XMLENC}aes256-cbc".freeze
- ALGORITHMS =
{ "#{::Xml::Kit::Namespaces::XMLENC}tripledes-cbc" => 'DES-EDE3-CBC', "#{::Xml::Kit::Namespaces::XMLENC}aes128-cbc" => 'AES-128-CBC', "#{::Xml::Kit::Namespaces::XMLENC}aes192-cbc" => 'AES-192-CBC', "#{::Xml::Kit::Namespaces::XMLENC}aes256-cbc" => 'AES-256-CBC', }.freeze
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
Instance Method Summary collapse
- #decrypt(cipher_text) ⇒ Object
- #encrypt(plain_text) ⇒ Object
-
#initialize(algorithm, key = nil) ⇒ SymmetricCipher
constructor
A new instance of SymmetricCipher.
Constructor Details
#initialize(algorithm, key = nil) ⇒ SymmetricCipher
Returns a new instance of SymmetricCipher.
17 18 19 20 |
# File 'lib/xml/kit/crypto/symmetric_cipher.rb', line 17 def initialize(algorithm, key = nil) @algorithm = algorithm @key = key || cipher.random_key end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
15 16 17 |
# File 'lib/xml/kit/crypto/symmetric_cipher.rb', line 15 def key @key end |
Class Method Details
.matches?(algorithm) ⇒ Boolean
22 23 24 |
# File 'lib/xml/kit/crypto/symmetric_cipher.rb', line 22 def self.matches?(algorithm) ALGORITHMS[algorithm] end |
Instance Method Details
#decrypt(cipher_text) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/xml/kit/crypto/symmetric_cipher.rb', line 32 def decrypt(cipher_text) cipher.decrypt iv = cipher_text[0..cipher.iv_len - 1] data = cipher_text[cipher.iv_len..-1] # cipher.padding = 0 cipher.key = @key cipher.iv = iv cipher.update(data) + cipher.final end |
#encrypt(plain_text) ⇒ Object
26 27 28 29 30 |
# File 'lib/xml/kit/crypto/symmetric_cipher.rb', line 26 def encrypt(plain_text) cipher.encrypt cipher.key = @key cipher.random_iv + cipher.update(plain_text) + cipher.final end |