Commit 3b61ced
Changed files (3)
lib
xml
kit
crypto
lib/xml/kit/crypto/symmetric_cipher.rb
@@ -13,7 +13,7 @@ module Xml
DEFAULT_ALGORITHM => 'AES-256-CBC',
}.freeze
- attr_reader :key
+ attr_reader :algorithm, :key
def initialize(algorithm, key = nil)
@algorithm = algorithm
@@ -31,33 +31,33 @@ module Xml
end
def decrypt(cipher_text)
- cipher.decrypt
- cipher.key = @key
- cipher.iv = cipher_text[0...cipher.iv_len]
-
- return decrypt_des(cipher, cipher_text) if triple_des?
+ return decrypt_des(cipher_text) if triple_des?
- decrypt_aes(cipher, cipher_text)
+ decrypt_aes(cipher_text)
end
private
- def decrypt_des(cipher, cipher_text)
+ def decrypt_des(cipher_text)
+ cipher.decrypt
+ cipher.key = @key
+ cipher.iv = cipher_text[0...cipher.iv_len]
cipher.update(cipher_text[cipher.iv_len..-1]) << cipher.final
end
- def decrypt_aes(cipher, cipher_text)
- cipher.padding = 0
- result = cipher.update(cipher_text[cipher.iv_len..-1]) << cipher.final
- result[0...-result.last.unpack('c').first]
+ def decrypt_aes(cipher_text)
+ size = ALGORITHMS[algorithm].split('-')[1].to_i
+ aes = Xmlenc::Algorithms::AESCBC.new(size)
+ aes.setup(@key)
+ aes.decrypt(cipher_text)
end
def cipher
- @cipher ||= OpenSSL::Cipher.new(ALGORITHMS[@algorithm])
+ @cipher ||= OpenSSL::Cipher.new(ALGORITHMS[algorithm])
end
def triple_des?
- @algorithm == TRIPLE_DES_ALGORITHM
+ algorithm == TRIPLE_DES_ALGORITHM
end
end
end
lib/xml/kit.rb
@@ -11,6 +11,7 @@ require 'openssl'
require 'pathname'
require 'tilt'
require 'xmldsig'
+require 'xmlenc'
require 'xml/kit/namespaces'
xml-kit.gemspec
@@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'nokogiri', '>= 1.8.5'
spec.add_dependency 'tilt', '>= 1.4.1'
spec.add_dependency 'xmldsig', '~> 0.6'
+ spec.add_dependency 'xmlenc', '~> 0.7'
spec.add_development_dependency 'bundler', '~> 1.16'
spec.add_development_dependency 'bundler-audit', '~> 0.6'
spec.add_development_dependency 'ffaker', '~> 2.7'