|
| 1 | +package org.nodejs.xmlcrypto; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.List; |
| 7 | +import javax.crypto.KeyGenerator; |
| 8 | +import javax.crypto.SecretKey; |
| 9 | +import javax.xml.crypto.dsig.CanonicalizationMethod; |
| 10 | +import javax.xml.crypto.dsig.DigestMethod; |
| 11 | +import javax.xml.crypto.dsig.Reference; |
| 12 | +import javax.xml.crypto.dsig.SignatureMethod; |
| 13 | +import javax.xml.crypto.dsig.SignedInfo; |
| 14 | +import javax.xml.crypto.dsig.Transform; |
| 15 | +import javax.xml.crypto.dsig.XMLSignature; |
| 16 | +import javax.xml.crypto.dsig.XMLSignatureFactory; |
| 17 | +import javax.xml.crypto.dsig.dom.DOMSignContext; |
| 18 | +import javax.xml.crypto.dsig.keyinfo.KeyInfo; |
| 19 | +import javax.xml.crypto.dsig.keyinfo.KeyInfoFactory; |
| 20 | +import javax.xml.crypto.dsig.spec.C14NMethodParameterSpec; |
| 21 | +import javax.xml.parsers.DocumentBuilder; |
| 22 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 23 | +import javax.xml.transform.Transformer; |
| 24 | +import javax.xml.transform.TransformerFactory; |
| 25 | +import javax.xml.transform.dom.DOMSource; |
| 26 | +import javax.xml.transform.stream.StreamResult; |
| 27 | +import org.apache.commons.io.FileUtils; |
| 28 | +import org.junit.Test; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | +import org.w3c.dom.Document; |
| 32 | +import org.w3c.dom.Element; |
| 33 | +import org.w3c.dom.Node; |
| 34 | + |
| 35 | +public class HMACTest { |
| 36 | + |
| 37 | + public static final String NamespaceSpecNS = "http://www.w3.org/2000/xmlns/"; |
| 38 | + |
| 39 | + private static final Logger LOGGER = LoggerFactory |
| 40 | + .getLogger(HMACTest.class); |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testCreateHMACSignature() throws Exception { |
| 44 | + // generate key |
| 45 | + KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA1"); |
| 46 | + SecretKey secretKey = keyGenerator.generateKey(); |
| 47 | + |
| 48 | + // generate DOM document |
| 49 | + DocumentBuilderFactory documentBuilderFactory |
| 50 | + = DocumentBuilderFactory.newInstance(); |
| 51 | + documentBuilderFactory.setNamespaceAware(true); |
| 52 | + DocumentBuilder documentBuilder = documentBuilderFactory |
| 53 | + .newDocumentBuilder(); |
| 54 | + Document document = documentBuilder.newDocument(); |
| 55 | + Element rootElement = document.createElementNS("urn:test", |
| 56 | + "test:Root"); |
| 57 | + rootElement.setAttributeNS(NamespaceSpecNS, |
| 58 | + "xmlns:test", "urn:test"); |
| 59 | + document.appendChild(rootElement); |
| 60 | + |
| 61 | + XMLSignatureFactory xmlSignatureFactory = XMLSignatureFactory |
| 62 | + .getInstance("DOM"); |
| 63 | + |
| 64 | + // XML Signature references |
| 65 | + List<Reference> references = new LinkedList<>(); |
| 66 | + List<Transform> transforms = new LinkedList<>(); |
| 67 | + Transform envTransform = xmlSignatureFactory.newTransform( |
| 68 | + CanonicalizationMethod.ENVELOPED, |
| 69 | + (C14NMethodParameterSpec) null); |
| 70 | + transforms.add(envTransform); |
| 71 | + Transform exclTransform = xmlSignatureFactory.newTransform( |
| 72 | + CanonicalizationMethod.EXCLUSIVE, |
| 73 | + (C14NMethodParameterSpec) null); |
| 74 | + transforms.add(exclTransform); |
| 75 | + Reference reference = xmlSignatureFactory.newReference("", |
| 76 | + xmlSignatureFactory.newDigestMethod(DigestMethod.SHA256, |
| 77 | + null), transforms, null, null); |
| 78 | + references.add(reference); |
| 79 | + |
| 80 | + // XML Signature SignedInfo |
| 81 | + SignedInfo signedInfo = xmlSignatureFactory.newSignedInfo( |
| 82 | + xmlSignatureFactory.newCanonicalizationMethod( |
| 83 | + CanonicalizationMethod.EXCLUSIVE, |
| 84 | + (C14NMethodParameterSpec) null), |
| 85 | + xmlSignatureFactory.newSignatureMethod( |
| 86 | + SignatureMethod.HMAC_SHA1, |
| 87 | + null), references); |
| 88 | + |
| 89 | + // XML Signature KeyInfo |
| 90 | + KeyInfoFactory keyInfoFactory |
| 91 | + = xmlSignatureFactory.getKeyInfoFactory(); |
| 92 | + KeyInfo keyInfo = keyInfoFactory.newKeyInfo(Collections |
| 93 | + .singletonList(keyInfoFactory.newKeyName("some-key-name"))); |
| 94 | + |
| 95 | + Element parentElement = document.getDocumentElement(); |
| 96 | + DOMSignContext domSignContext = new DOMSignContext( |
| 97 | + secretKey, parentElement); |
| 98 | + domSignContext.setDefaultNamespacePrefix("ds"); |
| 99 | + XMLSignature xmlSignature = xmlSignatureFactory.newXMLSignature( |
| 100 | + signedInfo, keyInfo); |
| 101 | + xmlSignature.sign(domSignContext); |
| 102 | + |
| 103 | + File tmpFile = File.createTempFile("xml-signature-hmac-", ".xml"); |
| 104 | + LOGGER.debug("XML signature file: {}", tmpFile.getAbsolutePath()); |
| 105 | + toFile(document, tmpFile); |
| 106 | + |
| 107 | + File tmpKeyFile = File.createTempFile("hmac-", ".key"); |
| 108 | + FileUtils.writeByteArrayToFile(tmpKeyFile, secretKey.getEncoded()); |
| 109 | + LOGGER.debug("key file: {}", tmpKeyFile.getAbsolutePath()); |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + private void toFile(Node node, File file) throws Exception { |
| 114 | + TransformerFactory transformerFactory = TransformerFactory |
| 115 | + .newInstance(); |
| 116 | + Transformer transformer = transformerFactory.newTransformer(); |
| 117 | + transformer.transform(new DOMSource(node), new StreamResult(file)); |
| 118 | + } |
| 119 | +} |
0 commit comments