Tôi cần phải ký xml bằng ruby, ai đó biết bất kỳ phương pháp hay lib nào cho điều đó?Làm thế nào để ký xml trong ruby
xml bộ xương của tôi là:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Message>
<MessageId>
<ServiceId>service</ServiceId>
<Version>1.0</Version>
<MsgDesc>Service Description</MsgDesc>
<Code>4</Code>
<FromAddress>from</FromAddress>
<ToAddress>to</ToAddress>
<Date>2012-10-29</Date>
</MessageId>
<MessageBody/>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<Reference URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<DigestValue>??????</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>????????????</SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate>????????</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</message>
Tôi đã thử mã này cho DigestValue và tôi đã thử nghiệm nó, so sánh nó với ví dụ java của tôi, nhưng DigestValue không phù hợp với phản ứng của ví dụ java của tôi:
require 'base64'
require 'openssl'
to_sign_xml = File.read 'service.xml'
digest = OpenSSL::Digest::SHA1.digest(to_sign_xml)
digest = Base64.encode64(digest.to_s).gsub(/\n/, '')
raise digest.inspect
tập tin service.xml My chứa rằng:
<Message>
<MessageId>
<ServiceId>service</ServiceId>
<Version>1.0</Version>
<MsgDesc>Service Description</MsgDesc>
<Code>4</Code>
<FromAddress>from</FromAddress>
<ToAddress>to</ToAddress>
<Date>2012-10-29</Date>
</MessageId>
<MessageBody/>
<Message>
Bạn không nhìn thấy để có vào tài khoản chuẩn hóa, cf. http://www.w3.org/TR/xml-exc-c14n/ trong trường hợp của bạn. – mkl
Tôi xin lỗi trước vì thế giới của nỗi đau bạn sắp nhập =) Xem câu hỏi liên quan này: http://stackoverflow.com/questions/3038757/canonicalizing-xml-in-ruby – maerics