private bool VerifySignature(RSACryptoServiceProvider publicKey) { var signatureValueElement = _xDocument.Root?.XPathSelectElement(SignatureValueExpression, _namespaceManager); if (null == signatureValueElement) { throw new CryptographicException("null == signatureValueElement"); } byte[] signature; try { signature = Convert.FromBase64String(signatureValueElement.Value); } catch (Exception exception) when(exception is FormatException || exception is ArgumentNullException) { throw new CryptographicException("Wrong format of signature value!"); } var signedInfoElement = _xDocument.Root.XPathSelectElement(SignedInfoExpression, _namespaceManager); var xDocument = new XDocument(); xDocument.Add(signedInfoElement); var canonizedSignedInfo = Canonicalizer.Canonize(xDocument); return(publicKey.VerifyData(Encoding.UTF8.GetBytes(canonizedSignedInfo), new SHA256Managed(), signature)); }
private byte[] ComputeDigest() { var xDocument = new XDocument(_xDocument); var signatureElement = xDocument.Root?.XPathSelectElement(SignatureElementExpression, _namespaceManager); if (null == signatureElement) { throw new CryptographicException("null == signatureElement"); } signatureElement.Remove(); var canonizedXml = Canonicalizer.Canonize(xDocument); byte[] hash; using (var sha256 = new SHA256Managed()) { hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(canonizedXml)); } return(hash); }