我需要使用 ruby 对 xml 进行签名,有人知道任何方法或库吗?
我的 xml 骨架是:
<?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>
我为 DigestValue 尝试了这段代码,并对其进行了测试,并将其与我的 java 示例进行了比较,但 DigestValue 与我的 java 示例的响应不匹配:
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
我的文件 service.xml 包含:
<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>