5

署名が MyXML タグの末尾ではなくタグ内に入るように、Apache santuario でドキュメントに署名するにはどうすればよいですか?

<MyXML>
    <SignaturePlace></SignaturePlace>
    <DataToSign>BlaBlaBla</DataToSign>
</MyXML>

標準の JSE dsig ライブラリ内には javax.xml.crypto.dsig.dom.DOMSignContext クラスがあり、コンストラクターは 2 つのパラメーター (RSA 秘密鍵と結果の XMLSignature の親要素の場所) を取ります。Apache santuario の実装の中に似たようなものはありますか?

4

1 に答える 1

1

はい、Apache Santuario でこれを行うことができます。

上記の XML の例を使用してこれを行うためのコード例を次に示します。

// Assume "document" is the Document you want to sign, and that you have already have the cert and the key

// Construct the signature and add the necessary transforms, etc.
XMLSignature signature = new XMLSignature(document, null, XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1);
final Transforms transforms = new Transforms(document);
transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
transforms.addTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS);
signature.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);

// Now insert the signature as the last child of the outermost node
document.getDocumentElement().appendChild(signature.getElement());

// Finally, actually sign the document.
signature.addKeyInfo(x509Certificate);
signature.addKeyInfo(x509Certificate.getPublicKey());
signature.sign(privateKey);

署名を最も外側のノードの最後の子にしたかったので、このケースは簡単です。3 番目の子ノードの前に署名を挿入する場合は、最初に署名を挿入するノードを指す Node を取得してから、「insertBefore()」メソッドを使用します。

final Node thirdChildNode = document.getFirstChild().getNextSibling().getNextSibling();
document.getDocumentElement().insertBefore(signature.getElement(), thirdChildNode);
于 2014-09-15T20:03:00.380 に答える