public void Verify_When_v1_Algorithm_User_Should_Return_True(string url, string method, string body, int futureSeconds) { var uri = new Uri(url); var unsignedRequest = SignatureRequest.CreateSignRequest(uri, time, appId, secret, method, body, algorithm: "IO2-HMAC-SHA256"); var signedUrl = underTest.Sign(unsignedRequest); var signedRequest = SignatureRequest.CreateVerificationRequest(signedUrl, time.AddSeconds(futureSeconds), secret, method, 60, body); underTest.Verify(signedRequest).Should().BeTrue(); }
private void SignatureProvider_DisposeVariation(string testCase, SignatureProvider provider, ExpectedException expectedException) { try { if (testCase.StartsWith("Sign")) { provider.Sign(new byte[256]); } else if (testCase.StartsWith("Verify")) { provider.Verify(new byte[256], new byte[256]); } else if (testCase.StartsWith("Dispose")) { provider.Dispose(); } else { Assert.True(false, "Test case does not match any scenario"); } expectedException.ProcessNoException(); } catch (Exception ex) { expectedException.ProcessException(ex); } }
public ValueResult <string> Sign(string data, string key) { try { return(new ValueResult <string>(SignatureProvider.Sign(data, key), true)); } catch (Exception ex) { Logger.Error(ex, $"Can't sign data: {ex.Message}"); return(new ValueResult <string>(null, false).Error($"Can't sign data: {ex.Message}", ex)); } }
private void SignatureProvider_SignVariation(SignatureProvider provider, byte[] bytes, byte[] signature, ExpectedException expectedException) { try { provider.Sign(bytes); expectedException.ProcessNoException(); } catch (Exception ex) { expectedException.ProcessException(ex); } }
/// <summary> /// Flushes all data currently buffered for sending. /// </summary> public override void Flush() => stsm.RunSafe(() => { var buf = sendBuffer.GetAsByteArray(); if (buf.Length == 0) { return; } sendBuffer.Clear(); Log.RecordEvent(this, $"Sending data: {buf.Select(x => x.ToString()).Aggregate((x, y) => x + "," + y)}", LogEntrySeverity.Info); using (var s = new MemoryStream()) using (var w = new BinaryWriter(s)) { w.Write(buf.Length); w.Write(buf); var sig = sp.Sign(buf); w.Write(sig.Length); w.Write(sig); _send(s.ToArray()); } });
private void Provider_Sign_Verify_ParameterChecking(string testcase, SignatureProvider provider, byte[] bytes, byte[] signature, ExpectedException exceptionExpected = null) { Console.WriteLine(string.Format("Testcase: '{0}'", testcase)); try { if (testcase.StartsWith("Sign")) { provider.Sign(bytes); } else { provider.Verify(bytes, signature); } Assert.IsFalse(exceptionExpected != null && exceptionExpected.Thrown != null, string.Format("Expected exception: '{0}'", exceptionExpected.Thrown)); } catch (Exception ex) { ExpectedException.ProcessException(exceptionExpected, ex); } }
public void BallotPrompt(ElectionType electionType) { if (electionType == ElectionType.FirstPastThePost) { var i = 1; Console.WriteLine("Welcome to the election! This is a 'First Past The Post' election model.\nThese are the following candidates to select from:\n" + string.Join("\n", this.candidateArr.Select(x => $"{i++}: {x}")) ); int counter = 1; int choice = 0; while (counter < 4) { Console.WriteLine("Please select your choice of candidate e.g. 1."); choice = Convert.ToInt32(Console.ReadLine()); if (choice < 1 || choice > candidateArr.Length) { Console.WriteLine("Please enter a valid choice."); counter += 1; } else { Console.WriteLine("Thank you for voting!"); break; } } if (counter == 4) { Console.WriteLine("You've exhausted your tries. Bye Bye"); return; } FirstPastThePostVote irVote = new FirstPastThePostVote(this.candidateArr[choice - 1]); var jsonVote = this.voteSerializer.Serialize(irVote); var signature = SignatureProvider.Sign(this.password, this.keyPair, jsonVote.GetBytes()); Vote vote = new Vote(this.keyPair.PublicKey.GetBase64String(), jsonVote, signature.GetBase64String()); this.voteMemoryPool.AddVote(vote); } else if (electionType == ElectionType.InstantRunoff) { var i = 1; Console.WriteLine("Welcome to the election! This is a 'Instant Runoff' election model.\nThese are the following candidates to select from:\n" + string.Join("\n", this.candidateArr.Select(x => $"{i++}: {x}")) ); // We need to consider when a candidate is not wishing NONE int counter = 1; var prefs = ""; List <string> tokens = null; char[] charSeparators = new char[] { ',' }; while (counter < 4) { Console.WriteLine("Please type a list in order of candidate preference e.g. 2,1,3."); prefs = Console.ReadLine(); tokens = prefs.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries).ToList(); if (tokens.Count > this.candidateArr.Length) { Console.WriteLine("Please enter valid choices in the correct format."); counter += 1; } List <string> rankedOrderedCandidates = new List <string>(); for (int j = 0; j < tokens.Count; j++) { rankedOrderedCandidates.Add(this.candidateArr[Convert.ToInt32(tokens[j]) - 1]); } InstantRunoffVote iroVote = new InstantRunoffVote(rankedOrderedCandidates); // make the IR vote var jsonVote = this.voteSerializer.Serialize(iroVote); var signature = SignatureProvider.Sign(this.password, this.keyPair, jsonVote.GetBytes()); Vote vote = new Vote(this.keyPair.PublicKey.GetBase64String(), jsonVote, signature.GetBase64String()); this.voteMemoryPool.AddVote(vote); return; } Console.WriteLine("You've exhausted your tries. Bye Bye"); return; } }
void ballotPrompt(ElectionType electionType) { if (electionType == ElectionType.FirstPastThePost) { var i = 1; Console.WriteLine("Welcome to the election! These are the following candidates to select from:\n" + string.Join("\n", this.candidateArr.Select(x => $"{i++}: {x}")) ); int counter = 1; int choice = 0; while (counter < 4) { Console.WriteLine("Please select your choice of candidate e.g. 1."); choice = Convert.ToInt32(Console.ReadLine()); if (choice < 1 || choice > candidateArr.Length) { Console.WriteLine("Please enter a valid choice."); counter += 1; } else { break; } } if (counter == 4) { Console.WriteLine("You've exhausted your tries. Bye Bye"); return; } FirstPastThePostVote irVote = new FirstPastThePostVote(this.candidateArr[choice - 1]); var jsonVote = this.voteSerializer.Serialize(irVote); var signature = SignatureProvider.Sign(this.password, this.keyPair, jsonVote.GetBytes()); Vote vote = new Vote(this.keyPair.PublicKey.GetBase64String(), jsonVote, signature.GetBase64String()); } else if (electionType == ElectionType.InstantRunoff) { var i = 1; Console.WriteLine("Welcome to the election! These are the following candidates to select from:\n" + string.Join("\n", this.candidateArr.Select(x => $"{i++}: {x}")) ); // We need to consider when a candidate is not wishing NONE int counter = 1; var prefs = ""; char[] charSeparators = new char[] { ',' }; while (counter < 4) { Console.WriteLine("Please type a list in order of candidate preference e.g. 2, 1, 3."); prefs = Console.ReadLine(); string[] tokens = prefs.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries); if () //TODO validation check of voter input check { Console.WriteLine("Please enter a valid choice."); counter += 1; } else { break; } } if (counter == 4) { Console.WriteLine("You've exhausted your tries. Bye Bye"); return; } InstantRunoffVote iroVote = new InstantRunoffVote(); // make the IR vote var jsonVote = this.voteSerializer.Serialize(iroVote); var signature = SignatureProvider.Sign(this.password, this.keyPair, jsonVote.GetBytes()); Vote vote = new Vote(this.keyPair.PublicKey.GetBase64String(), jsonVote, signature.GetBase64String()); } }
public void RsaCngAdapterTest(RsaCngAdapterTheoryData theoryData) { var context = TestUtilities.WriteHeader($"{this}.RsaCngAdapterTest", theoryData); var cert = KeyingMaterial.CertSelfSigned2048_SHA256; var rsaCapiPrivateKey = new RsaSecurityKey(cert.PrivateKey as RSA); var rsaCapiPublicKey = new RsaSecurityKey(cert.PublicKey.Key as RSA); var clearBytes = Encoding.UTF8.GetBytes("blue star"); byte[] capiSignatureBytes = null; AsymmetricSignatureProvider providerCapiPrivate = null; AsymmetricSignatureProvider providerCapiPublic = null; // create CAPI providers try { providerCapiPrivate = new AsymmetricSignatureProvider(rsaCapiPrivateKey, theoryData.Algorithm, true); capiSignatureBytes = providerCapiPrivate.Sign(clearBytes); providerCapiPublic = new AsymmetricSignatureProvider(rsaCapiPublicKey, theoryData.Algorithm, false); if (!providerCapiPublic.Verify(clearBytes, capiSignatureBytes)) { context.AddDiff("providerCapiPublic.Verify(clearBytes, capiSignatureBytes)"); } } catch (Exception ex) { theoryData.ExpectedException.ProcessException(ex, context); } var rsaCngPrivateKey = new RsaSecurityKey(cert.GetRSAPrivateKey()); var rsaCngPublicKey = new RsaSecurityKey(cert.GetRSAPublicKey()); byte[] cngSignatureBytes = null; byte[] cngSignatureBytesByFactory = null; // create private signing AsymmetricSignatureProvider providerCngPrivate = null; try { providerCngPrivate = new AsymmetricSignatureProvider(rsaCngPrivateKey, theoryData.Algorithm, true); } catch (Exception ex) { theoryData.ExpectedException.ProcessException(ex, context); } // create private signing with CryptoProviderFactory.Default SignatureProvider providerCngPrivateByFactory = null; try { providerCngPrivateByFactory = CryptoProviderFactory.Default.CreateForSigning(rsaCngPrivateKey, theoryData.Algorithm); } catch (Exception ex) { theoryData.ExpectedException.ProcessException(ex, context); } // create public verifying AsymmetricSignatureProvider providerCngPublic = null; try { providerCngPublic = new AsymmetricSignatureProvider(rsaCngPublicKey, theoryData.Algorithm, false); } catch (Exception ex) { theoryData.ExpectedException.ProcessException(ex, context); } // create public verifying with CryptoProviderFactory.Default SignatureProvider providerCngPublicByFactory = null; try { providerCngPublicByFactory = CryptoProviderFactory.Default.CreateForVerifying(rsaCngPublicKey, theoryData.Algorithm); } catch (Exception ex) { theoryData.ExpectedException.ProcessException(ex, context); } try { cngSignatureBytes = providerCngPrivate.Sign(clearBytes); } catch (Exception ex) { theoryData.ExpectedException.ProcessException(ex, context); } // cngByFactory Sign try { cngSignatureBytesByFactory = providerCngPrivateByFactory.Sign(clearBytes); } catch (Exception ex) { theoryData.ExpectedException.ProcessException(ex, context); } // CngPublic -> CngPrivate validates try { var cngVerify = providerCngPublic.Verify(clearBytes, cngSignatureBytes); if (!cngVerify) { context.AddDiff($"cngVerify = providerCngPublic.Verify(clearBytes, cngSignatureBytes) == false."); } } catch (Exception ex) { theoryData.ExpectedException.ProcessException(ex, context); } // CngPublicByFactory -> CngPrivate validates try { var cngVerify = providerCngPublicByFactory.Verify(clearBytes, cngSignatureBytes); if (!cngVerify) { context.AddDiff($"cngVerify = providerCngPublicByFactory.Verify(clearBytes, cngSignatureBytes) == false."); } } catch (Exception ex) { theoryData.ExpectedException.ProcessException(ex, context); } // CngPublic -> CAPI validates try { var cngVerify = providerCngPublic.Verify(clearBytes, capiSignatureBytes); if (!cngVerify) { context.AddDiff($"cngVerify = providerCngPublic.Verify(clearBytes, capiSignatureBytes) == false."); } } catch (Exception ex) { theoryData.ExpectedException.ProcessException(ex, context); } // CngPublicByFactory -> CAPI validates try { var verify = providerCngPublicByFactory.Verify(clearBytes, capiSignatureBytes); if (!verify) { context.AddDiff($"verify = providerCngPublicByFactory.Verify(clearBytes, capiSignatureBytes) == false."); } } catch (Exception ex) { theoryData.ExpectedException.ProcessException(ex, context); } // CAPIPublic -> Cng validates try { var verify = providerCapiPublic.Verify(clearBytes, cngSignatureBytes); if (!verify) { context.AddDiff($"verify = providerCapiPublic.Verify(clearBytes, cngSignatureBytes) == false."); } } catch (Exception ex) { theoryData.ExpectedException.ProcessException(ex, context); } // CAPIPublic -> CngByFactory validates try { var verify = providerCapiPublic.Verify(clearBytes, cngSignatureBytesByFactory); if (!verify) { context.AddDiff($"verify = providerCapiPublic.Verify(clearBytes, cngSignatureBytesByFactory) == false."); } } catch (Exception ex) { theoryData.ExpectedException.ProcessException(ex, context); } TestUtilities.AssertFailIfErrors(context); }