Class: Xmlenc::EncryptedData

Inherits:
Object
  • Object
show all
Defined in:
lib/xmlenc/encrypted_data.rb

Constant Summary collapse

ALGORITHMS =
{
    'http://www.w3.org/2001/04/xmlenc#tripledes-cbc' => Algorithms::DES3CBC,
    'http://www.w3.org/2001/04/xmlenc#aes128-cbc'    => Algorithms::AESCBC[128],
    'http://www.w3.org/2001/04/xmlenc#aes256-cbc'    => Algorithms::AESCBC[256]
}
TYPES =
{
    'http://www.w3.org/2001/04/xmlenc#Element' => :element,
    'http://www.w3.org/2001/04/xmlenc#Content' => :content,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ EncryptedData

Returns a new instance of EncryptedData.



16
17
18
# File 'lib/xmlenc/encrypted_data.rb', line 16

def initialize(node)
  @node = node
end

Instance Attribute Details

#nodeObject

Returns the value of attribute node.



14
15
16
# File 'lib/xmlenc/encrypted_data.rb', line 14

def node
  @node
end

Instance Method Details

#cipher_valueObject



28
29
30
# File 'lib/xmlenc/encrypted_data.rb', line 28

def cipher_value
  at_xpath('./xenc:CipherData/xenc:CipherValue').content.gsub(/[\n\s]/, '')
end

#cipher_value=(value) ⇒ Object



32
33
34
# File 'lib/xmlenc/encrypted_data.rb', line 32

def cipher_value=(value)
  at_xpath('./xenc:CipherData/xenc:CipherValue').content = value
end

#decrypt(key) ⇒ Object



36
37
38
39
40
41
# File 'lib/xmlenc/encrypted_data.rb', line 36

def decrypt(key)
  decryptor = algorithm.setup(key)
  decrypted = decryptor.decrypt(Base64.decode64(cipher_value), :node => encryption_method)
  @node.replace(Nokogiri::XML::DocumentFragment.parse(decrypted)) unless @node == document.root
  decrypted
end

#documentObject



20
21
22
# File 'lib/xmlenc/encrypted_data.rb', line 20

def document
  @node.document
end

#encrypt(data) ⇒ Object



43
44
45
46
47
48
# File 'lib/xmlenc/encrypted_data.rb', line 43

def encrypt(data)
  encryptor = algorithm.setup
  encrypted = encryptor.encrypt(data, :node => encryption_method)
  self.cipher_value = Base64.encode64(encrypted)
  encryptor.key
end

#encryption_methodObject



24
25
26
# File 'lib/xmlenc/encrypted_data.rb', line 24

def encryption_method
  at_xpath('./xenc:EncryptionMethod')
end

#typeObject



50
51
52
# File 'lib/xmlenc/encrypted_data.rb', line 50

def type
  TYPES[@node['Type']]
end