private Document signElement(Document doc,String id,X509Certificate cert,PrivateKey key,String signAlgorithm,String digestAlgorithm) throws MarshalException, XMLSignatureException {
  try {
    XMLSignatureFactory xmlSignatureFactory=XMLSignatureFactory.getInstance("DOM",new org.jcp.xml.dsig.internal.dom.XMLDSigRI());
    List<Transform> transforms=new ArrayList<Transform>();
    Transform enveloped=xmlSignatureFactory.newTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE,(XMLStructure)null);
    transforms.add(enveloped);
    Transform c14n=xmlSignatureFactory.newTransform(Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS,(XMLStructure)null);
    transforms.add(c14n);
    Reference ref;
    try {
      ref=xmlSignatureFactory.newReference("#" + id,xmlSignatureFactory.newDigestMethod(digestAlgorithm,null),transforms,null,null);
    }
 catch (    NoSuchAlgorithmException e) {
      ref=xmlSignatureFactory.newReference("#" + id,xmlSignatureFactory.newDigestMethod(DigestMethod.SHA256,null),transforms,null,null);
    }
    SignedInfo signedInfo;
    try {
      signedInfo=xmlSignatureFactory.newSignedInfo(xmlSignatureFactory.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE,(C14NMethodParameterSpec)null),xmlSignatureFactory.newSignatureMethod(signAlgorithm,null),Collections.singletonList(ref));
    }
 catch (    NoSuchAlgorithmException e) {
      signedInfo=xmlSignatureFactory.newSignedInfo(xmlSignatureFactory.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE,(C14NMethodParameterSpec)null),xmlSignatureFactory.newSignatureMethod(XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256,null),Collections.singletonList(ref));
    }
    KeyInfoFactory keyInfoFactory=xmlSignatureFactory.getKeyInfoFactory();
    List<X509Certificate> x509Content=new ArrayList<>();
    x509Content.add(cert);
    X509Data x509Data=keyInfoFactory.newX509Data(x509Content);
    KeyInfo keyInfo=keyInfoFactory.newKeyInfo(Collections.singletonList(x509Data));
    Element elementToSign=doc.getElementById(id);
    NodeList issuerList=elementToSign.getElementsByTagNameNS("*","Issuer");
    Element elementBeforeSignature;
    if (issuerList.getLength() > 0) {
      elementBeforeSignature=(Element)issuerList.item(0);
    }
 else {
      elementBeforeSignature=elementToSign;
    }
    Element nextElementAfterIssuer=(Element)elementBeforeSignature.getNextSibling();
    DOMSignContext domSignContext=new DOMSignContext(key,elementToSign);
    domSignContext.setDefaultNamespacePrefix("ds");
    domSignContext.setNextSibling(nextElementAfterIssuer);
    javax.xml.crypto.dsig.XMLSignature signature=xmlSignatureFactory.newXMLSignature(signedInfo,keyInfo);
    signature.sign(domSignContext);
    return doc;
  }
 catch (  InvalidAlgorithmParameterException e) {
    e.printStackTrace();
  }
catch (  NoSuchAlgorithmException e1) {
    e1.printStackTrace();
  }
  return null;
}

private Document signElement ( Document doc , String id , X509Certificate cert , PrivateKey key , String signAlgorithm , String digestAlgorithm ) throws MarshalException , XMLSignatureException { try { XMLSignatureFactory xmlSignatureFactory = XMLSignatureFactory . getInstance ( "DOM" , new org . jcp . xml . dsig . internal . dom . XMLDSigRI ( ) ) ; List < Transform > transforms = new ArrayList < Transform > ( ) ; Transform enveloped = xmlSignatureFactory . newTransform ( Transforms . TRANSFORM_ENVELOPED_SIGNATURE , ( XMLStructure ) null ) ; transforms . add ( enveloped ) ; Transform c14n = xmlSignatureFactory . newTransform ( Transforms . TRANSFORM_C14N_EXCL_OMIT_COMMENTS , ( XMLStructure ) null ) ; transforms . add ( c14n ) ; Reference ref ; try { ref = xmlSignatureFactory . newReference ( "#" + id , xmlSignatureFactory . newDigestMethod ( digestAlgorithm , null ) , transforms , null , null ) ; } catch ( NoSuchAlgorithmException e ) { ref = xmlSignatureFactory . newReference ( "#" + id , xmlSignatureFactory . newDigestMethod ( DigestMethod . SHA256 , null ) , transforms , null , null ) ; } SignedInfo signedInfo ; try { signedInfo = xmlSignatureFactory . newSignedInfo ( xmlSignatureFactory . newCanonicalizationMethod ( CanonicalizationMethod . EXCLUSIVE , ( C14NMethodParameterSpec ) null ) , xmlSignatureFactory . newSignatureMethod ( signAlgorithm , null ) , Collections . singletonList ( ref ) ) ; } catch ( NoSuchAlgorithmException e ) { signedInfo = xmlSignatureFactory . newSignedInfo ( xmlSignatureFactory . newCanonicalizationMethod ( CanonicalizationMethod . EXCLUSIVE , ( C14NMethodParameterSpec ) null ) , xmlSignatureFactory . newSignatureMethod ( XMLSignature . ALGO_ID_SIGNATURE_RSA_SHA256 , null ) , Collections . singletonList ( ref ) ) ; } KeyInfoFactory keyInfoFactory = xmlSignatureFactory . getKeyInfoFactory ( ) ; List < X509Certificate > x509Content = new ArrayList < > ( ) ; x509Content . add ( cert ) ; X509Data x509Data = keyInfoFactory . newX509Data ( x509Content ) ; KeyInfo keyInfo = keyInfoFactory . newKeyInfo ( Collections . singletonList ( x509Data ) ) ; Element elementToSign = doc . getElementById ( id ) ; NodeList issuerList = elementToSign . getElementsByTagNameNS ( "*" , "Issuer" ) ; Element elementBeforeSignature ; if ( issuerList . getLength ( ) > 0 ) { elementBeforeSignature = ( Element ) issuerList . item ( 0 ) ; } else { elementBeforeSignature = elementToSign ; } Element nextElementAfterIssuer = ( Element ) elementBeforeSignature . getNextSibling ( ) ; DOMSignContext domSignContext = new DOMSignContext ( key , elementToSign ) ; domSignContext . setDefaultNamespacePrefix ( "ds" ) ; domSignContext . setNextSibling ( nextElementAfterIssuer ) ; javax . xml . crypto . dsig . XMLSignature signature = xmlSignatureFactory . newXMLSignature ( signedInfo , keyInfo ) ; signature . sign ( domSignContext ) ; return doc ; } catch ( InvalidAlgorithmParameterException e ) { e . printStackTrace ( ) ; } catch ( NoSuchAlgorithmException e1 ) { e1 . printStackTrace ( ) ; } return null ; }
sign the assertion with the given id
