Method XMLSignatureUtil.sign(Document doc, Node nodeToBeSigned, keyPair, digestMethod, signatureMethod, referenceURI) sometimes doesn't work correctly.