private static XmlElement GenerateContentReference(XmlDocument doc, XmlSignatureAppearance sap, String referenceId) {
IXpathConstructor xpathConstructor = sap.GetXpathConstructor();
XmlElement reference = doc.CreateElement("Reference", SecurityConstants.XMLDSIG_URI);
reference.SetAttribute("URI", "");
if (referenceId != null)
reference.SetAttribute("Id", referenceId);
XmlElement transforms = doc.CreateElement("Transforms", SecurityConstants.XMLDSIG_URI);
XmlElement transform = doc.CreateElement("Transform", SecurityConstants.XMLDSIG_URI);
transform.SetAttribute("Algorithm", SecurityConstants.XMLDSIG_URI_ENVELOPED);
transforms.AppendChild(transform);
byte[] md = null;
if (xpathConstructor != null && xpathConstructor.GetXpathExpression().Length > 0)
{
XmlNodeList nodelist = doc.SelectNodes(xpathConstructor.GetXpathExpression(),
xpathConstructor.GetNamespaceManager());
if (nodelist.Count == 1 && nodelist[0].NodeType == XmlNodeType.Element)
{
XmlElement xpathSelect = (XmlElement) nodelist[0].CloneNode(true);
NormalizeNamespaces(nodelist[0].CreateNavigator(), xpathSelect.CreateNavigator());
XmlDocument digestDoc = new XmlDocument(doc.NameTable);
digestDoc.LoadXml(xpathSelect.OuterXml);
md = CalculateC14nDigest(digestDoc, new SHA1Managed());
transform = doc.CreateElement("Transform", SecurityConstants.XMLDSIG_URI);
transform.SetAttribute("Algorithm", SecurityConstants.XMLDSIG_URI_XPATH_FILTER2);
XmlElement xpath = doc.CreateElement("XPath", SecurityConstants.XMLDSIG_URI);
xpath.SetAttribute("xmlns", SecurityConstants.XMLDSIG_URI_XPATH_FILTER2);
xpath.SetAttribute("Filter", "intersect");
XmlNode xpathNode = doc.CreateTextNode(xpathConstructor.GetXpathExpression());
xpath.AppendChild(xpathNode);
transform.AppendChild(xpath);
transforms.AppendChild(transform);
}
}
if (md == null)
md = CalculateC14nDigest(doc, new SHA1Managed());
reference.AppendChild(transforms);
XmlElement digestMethod = doc.CreateElement("DigestMethod", SecurityConstants.XMLDSIG_URI);
digestMethod.SetAttribute("Algorithm", SecurityConstants.XMLDSIG_URI_SHA1);
reference.AppendChild(digestMethod);
XmlElement digestValue = doc.CreateElement("DigestValue", SecurityConstants.XMLDSIG_URI);
digestValue.AppendChild(doc.CreateTextNode(Convert.ToBase64String(md)));
reference.AppendChild(digestValue);
return reference;
}