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
-
#algorithm ⇒ Object
readonly
Returns the value of attribute algorithm.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#padding ⇒ Object
readonly
Returns the value of attribute padding.
Class Method Summary collapse
Instance Method Summary collapse
- #decrypt(cipher_text) ⇒ Object
- #encrypt(plain_text) ⇒ Object
-
#initialize(algorithm, key = nil, padding = nil) ⇒ SymmetricCipher
constructor
A new instance of SymmetricCipher.
Constructor Details
#initialize(algorithm, key = nil, padding = nil) ⇒ SymmetricCipher
Returns a new instance of SymmetricCipher.
17 18 19 20 21 |
# File 'lib/xml/kit/crypto/symmetric_cipher.rb', line 17 def initialize(algorithm, key = nil, padding = nil) @algorithm = algorithm @key = key || cipher.random_key @padding = padding end |
Instance Attribute Details
#algorithm ⇒ Object (readonly)
Returns the value of attribute algorithm.
15 16 17 |
# File 'lib/xml/kit/crypto/symmetric_cipher.rb', line 15 def algorithm @algorithm end |
#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 |
#padding ⇒ Object (readonly)
Returns the value of attribute padding.
15 16 17 |
# File 'lib/xml/kit/crypto/symmetric_cipher.rb', line 15 def padding @padding end |
Class Method Details
.matches?(algorithm) ⇒ Boolean
23 24 25 |
# File 'lib/xml/kit/crypto/symmetric_cipher.rb', line 23 def self.matches?(algorithm) ALGORITHMS[algorithm] end |
Instance Method Details
#decrypt(cipher_text) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/xml/kit/crypto/symmetric_cipher.rb', line 33 def decrypt(cipher_text) result = default_decrypt( cipher_text[0...cipher.iv_len], cipher_text[cipher.iv_len..-1] ) return result if padding.nil? padding_size = result.bytes.last result[0...-padding_size] end |
#encrypt(plain_text) ⇒ Object
27 28 29 30 31 |
# File 'lib/xml/kit/crypto/symmetric_cipher.rb', line 27 def encrypt(plain_text) cipher.encrypt cipher.key = @key cipher.random_iv + cipher.update(plain_text) + cipher.final end |