肥皂标准化转换

时间:2016-02-24 15:58:00

标签: java xml node.js web-services soap

我正在尝试使用Datapower设备上托管的soap服务。到目前为止,发送肥皂服务和接收者以不同方式计算以下xml的摘要。

用于创建摘要值的转换

<ds:Reference URI="#TS-f3c103e9-1897-43d8-8cf6-274bdb647678">
    <ds:Transforms>
       <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
          <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="wsse soap"/>
       </ds:Transform>
   </ds:Transforms>
   <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
   <ds:DigestValue>1rjXQQWVMM5KBWY8uswUynk6PCk=</ds:DigestValue>
</ds:Reference>

引用的元素。

<wsu:Timestamp wsu:Id="TS-f3c103e9-1897-43d8-8cf6-274bdb647678">
  <wsu:Created>2016-02-24T15:32:12.693Z</wsu:Created>
  <wsu:Expires>2016-02-24T15:37:12.693Z</wsu:Expires>
</wsu:Timestamp>

上述元素的命名空间应用于根节点

  • 的xmlns:WSU = “http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd”
  • 的xmlns:DS = “http://www.w3.org/2000/09/xmldsig#”

发送上述XML可正常工作(从Java soap客户端库创建)。但是,当我在节点中使用sha1消化相同的元素时,我得到一个不同的摘要值。

我试图在计算摘要值之前发现java在转换中的作用。如果我能弄清楚它实际发送到sha1方法的内容,我可以更正节点中的代码来做同样的事情。

给定上面的引用元素,在使用sha1进行散列之前的转换之后输出xml是什么?

1 个答案:

答案 0 :(得分:2)

您需要弄清楚java库或API如何创建它发送给SHA1方法的规范化XML。

使用Apache Commons Codec(https://commons.apache.org/proper/commons-codec/)并猜测来自Timestamp元素的C14N&XML和Transform元素中包含的命名空间,我能够得到相同的摘要值:1rjXQQWVMM5KBWY8uswUynk6PCk =

这是我使用的代码:

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

public class Sha1Test {

    public static void main(String args[]) throws Exception {
        String data = "<wsu:Timestamp xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" wsu:Id=\"TS-f3c103e9-1897-43d8-8cf6-274bdb647678\"><wsu:Created>2016-02-24T15:32:12.693Z</wsu:Created><wsu:Expires>2016-02-24T15:37:12.693Z</wsu:Expires></wsu:Timestamp>";
        printShaDigest(data);
    }

    static void printShaDigest(String data) {
        System.out.println("data = " + data);
        System.out.println("sha1 = " + new String(Base64.encodeBase64(DigestUtils.sha1(data.getBytes()))));
    }

}
相关问题