-1

I am working on ESign process for XML The signing steps includes,

  1. Hash the new invoice body using SHA-256
  2. Encode the hashed invoice using base64 Further is to Generate Digital Signature.
  • Does this answer your question? [Getting a File's Checksum in Java](https://stackoverflow.com/questions/304268/getting-a-files-checksum-in-java) – Dave Jarvis Dec 23 '22 at 21:24

1 Answers1

1

This may help you

public String getXMLInvHash(String xmlDocument) {
        String xmlHasHString = "";
        try {
            Transformer transformer = this.getTransformer();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            StreamResult xmlOutput = new StreamResult(byteArrayOutputStream);
            transformer.transform(new StreamSource(new StringReader(xmlDocument)), xmlOutput);
            String canString = canonicalizeXml(byteArrayOutputStream.toByteArray());
            byte[] shaByte = hashStringToBytes(canString);
            xmlHasHString = Base64.getEncoder().encodeToString(shaByte);
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        return xmlHasHString;
    }

    private String canonicalizeXml(final byte[] xmlDocument) {
        String canonicString = "";
        try {
            Init.init();
            Canonicalizer canon = Canonicalizer.getInstance("http://www.w3.org/2006/12/xml-c14n11");
            // System.out.println("Can : " + new String(canon.canonicalize(xmlDocument)));
            canonicString = new String(canon.canonicalize(xmlDocument));
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (InvalidCanonicalizerException e) {
            e.printStackTrace();
        } catch (CanonicalizationException e) {
            e.printStackTrace();
        }
        return canonicString;
    }

    private Transformer getTransformer() {
        TransformerFactory transformerFactory = null;
        Transformer transformer = null;
        try {
            transformerFactory = TransformerFactory.newInstance();
            transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty("encoding", "UTF-8");
            transformer.setOutputProperty("indent", "no");
            transformer.setOutputProperty("omit-xml-declaration", "yes");

        } catch (TransformerException e) {
            e.printStackTrace();
        }
        return transformer;
    }

    private byte[] hashStringToBytes(String input) {
        MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        final byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8));
        return hash;
    }
Jegadeep V
  • 38
  • 5