public static void Test(VipNetCrytoProvider provider) { using (StreamReader file = File.OpenText("original-signedInfo.txt")) { //"boivBh1ioIPFGrKQ7LhhVw2pGfar/+T9Cqgz6JZ35IA=" //"Pbp8cgljASFaqSsAHwA2eMC8GGvE9hjS5giHHYS8ZXQ=" //"FD289YMUI30ICSHDA0boH479x0G+ZjYnBx748mk6uT8=" //"fTBlROVsvTt/fAoyBLWVuaMYCIX/z4Eu+IVwQrNTb3k=" //"W4hHfJjhWWvFRLxQVMu16sltkr/wLN36ulR+ySbYyCE=" //"FD289YMUI30ICSHDA0boH479x0G+ZjYnBx748mk6uT8=" 6SEf0xFQFeilZRxXLCyflb3V9oTbyU7lsB/yLhPE2c25ZBOd7vEXNhY/wQnzt2+gNZRBkW26sUtbAWi59m1h/w== var hash = provider.HashData(file.ReadToEnd()); Signature signature = provider.SigningHash(hash); bool result = provider.VerifySignature(signature); Console.WriteLine(signature.Base64); } X509Certificate cert = new X509Certificate(); using (StreamReader file = File.OpenText("CorrectCertificate.txt")) { var data = file.ReadToEnd().Replace("\n", ""); var certBytes = Convert.FromBase64String(data); cert.Import(certBytes); var publicKey = cert.GetPublicKey(); } }
public override byte[] GetDigestedOutput(HashAlgorithm hash) { var output = new StreamReader((MemoryStream)base.GetOutput()).ReadToEnd(); var gostHash = new VipNetCrytoProvider().HashData(output); //return base.GetDigestedOutput(hash); return(gostHash.Bytes); }
public static bool Verify(string xml, VipNetCrytoProvider provider) { XmlDocument doc = XDocument.Parse(xml).GetXmlDocument(); var body = Canonicalizer.GetObject(doc, "#body"); var bodyHash = provider.HashData(body); XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable); nsManager.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#"); nsManager.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/"); nsManager.AddNamespace("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); nsManager.AddNamespace("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); var digestValue = (doc.SelectSingleNode("//soapenv:Envelope/soapenv:Header/wsse:Security/ds:Signature/ds:SignedInfo/ds:Reference/ds:DigestValue", nsManager) as XmlElement).InnerText; if (bodyHash.Base64 != digestValue) { return(false); } var signedInfo = Canonicalizer.GetObject(doc, "#SignedInfo"); //using (var file = File.CreateText("original-signedInfo" + ".txt")) //{ // file.WriteLine(signedInfo); //} using (var file = File.OpenText("original-signedInfo" + ".txt")) { var signedInfoWithoutDs = file.ReadToEnd(); Signature sig = provider.SigningHash(provider.HashData(signedInfoWithoutDs)); } var signedInfoHash = provider.HashData(signedInfo); var signature = provider.SigningHash(signedInfoHash); var signatureValue = (doc.SelectSingleNode("//soapenv:Envelope/soapenv:Header/wsse:Security/ds:Signature/ds:SignatureValue", nsManager) as XmlElement).InnerText; var certificateBase64 = (doc.SelectSingleNode("//soapenv:Envelope/soapenv:Header/wsse:Security/wsse:BinarySecurityToken", nsManager) as XmlElement).InnerText; X509Certificate certificate = new X509Certificate(Convert.FromBase64String(certificateBase64)); //var signature = new Signature {SignedHash = signedInfoHash, Bytes = Convert.FromBase64String(signatureValue)}; signature.Bytes = Convert.FromBase64String(signatureValue); return(provider.VerifySignature(signature)); }
public void Test(SignedXmlDocument doc) { Console.WriteLine("================== Certificate Test =================="); var certVipNet = doc.CertificateValue.Value; var result = doc.CertificateValue.Value == Convert.ToBase64String(Certificate.GetRawCertData()); Console.WriteLine("Test done: {0}", result); Console.WriteLine("======================================================"); Console.WriteLine("================ Canonicalization Test ================"); var doc2 = doc.Document.GetXmlDocument(); XmlDsigExcC14NTransform t = new XmlDsigExcC14NTransform(); var bodyNode = doc.Body.GetXmlNode(); t.LoadInput(bodyNode); Stream canonicalXmlStream = (MemoryStream)t.GetOutput(typeof(Stream)); var canonicalXml = new StreamReader(canonicalXmlStream).ReadToEnd(); var t2 = new XmlDsigC14NTransform(); t2.LoadInput(doc.Body.GetXmlNode()); Stream canonicalXmlStream2 = (MemoryStream)t2.GetOutput(typeof(Stream)); var canonicalXml2 = new StreamReader(canonicalXmlStream2).ReadToEnd(); Console.WriteLine(canonicalXml == canonicalXml2); Console.WriteLine(canonicalXml); Console.WriteLine("======================================================"); var signedXml = new SignedXml(doc2); var reference = new Reference("#body"); reference.AddTransform(new XmlDsigExcC14NTransform()); signedXml.AddReference(reference); Hash hash = new VipNetCrytoProvider().HashData(canonicalXml); Console.WriteLine(hash.Base64); Console.WriteLine(hash.Hex); Console.WriteLine("CorrectHashExample: {0}", CorrectHashExample); Console.WriteLine("CorrectHashExampleHex: {0}", Convert.FromBase64String(CorrectHashExample).ByteArrayToString()); }
public void HashTest() { string message = "This is message, length=32 bytes"; Console.WriteLine("====================== Hash Test ======================"); var crypto = new VipNetCrytoProvider(); var hash1 = crypto.HashData(message); crypto = new VipNetCrytoProvider(); var hash2 = crypto.HashData(message); if (hash1.Hex != hash2.Hex) { throw new Exception("Wrong hash value"); } Console.WriteLine(hash1.Hex); Console.WriteLine("======================================================"); }
public override object GetOutput() { var result = new StreamReader((MemoryStream)base.GetOutput()).ReadToEnd(); Console.WriteLine(result); var sha1 = SHA1.Create(); var resultBytes = Encoding.UTF8.GetBytes(result); Console.WriteLine(Convert.ToBase64String(sha1.ComputeHash(Encoding.UTF8.GetBytes(result)))); Console.WriteLine(Convert.ToBase64String(sha1.ComputeHash((Stream)base.GetOutput()))); var hash = new VipNetCrytoProvider().HashData(result); Console.WriteLine(hash.Base64); Console.WriteLine(hash.Hex); //var input = File.OpenWrite("C:\\Users\\Yura\\Downloads\\rhash-1.2.10-win32\\input.txt"); //input.Write(resultBytes,0, resultBytes.Length); //input.Close(); return(base.GetOutput()); }
public void Run() { var testDoc = new SignedXmlDocument(DocWithoutSignature); testDoc = new SignedXmlDocument(testDoc.ToString()); var testDoc2 = new SignedXmlDocument(testDoc.ToString()); var testDoc3 = new SignedXmlDocument(testDoc2.ToString()); var testDoc4 = new SignedXmlDocument(testDoc3.ToString()); var testDoc5 = new SignedXmlDocument(testDoc4.ToString()); var provider = new VipNetCrytoProvider(); TestSignature.Test(provider); //return; //TestSignedXmlPrefix(); using (var output = File.CreateText("output.txt")) { provider.SignDocument(testDoc); Console.WriteLine(testDoc.Document); output.WriteLine(testDoc.ToString()); } var verify = SignatureVerifier.Verify(testDoc.ToString(), provider); var testCertificate = new TestCertificate(); testCertificate.Test(testDoc); var doc = new XmlDocument(); doc.LoadXml(DocWithoutSignature); new SignedXmlDoc(DocWithoutSignature); }