public void SignatureProviders_SignAndVerify() { // asymmetric try { Random r = new Random(); AsymmetricSignatureProvider provider = new AsymmetricSignatureProvider(KeyingMaterial.DefaultAsymmetricKey_2048, SecurityAlgorithms.RsaSha256Signature); byte[] bytesin = new byte[1024]; r.NextBytes(bytesin); byte[] signature = provider.Sign(bytesin); } catch (Exception ex) { Assert.AreEqual(ex.GetType(), typeof(InvalidOperationException)); } // asymmetric try { Random r = new Random(); AsymmetricSignatureProvider provider = new AsymmetricSignatureProvider(KeyingMaterial.DefaultAsymmetricKey_2048, SecurityAlgorithms.RsaSha256Signature, true); byte[] bytesin = new byte[1024]; r.NextBytes(bytesin); byte[] signature = provider.Sign(bytesin); Assert.IsTrue(provider.Verify(bytesin, signature)); } catch (Exception) { Assert.Fail("Should have thrown, it is possible that crypto config mapped this."); } // unknown algorithm try { Random r = new Random(); AsymmetricSignatureProvider provider = new AsymmetricSignatureProvider(KeyingMaterial.DefaultAsymmetricKey_2048, "SecurityAlgorithms.RsaSha256Signature"); Assert.Fail(string.Format("Should have thrown, it is possible that crypto config mapped this.")); } catch (Exception ex) { Assert.IsFalse(ex.GetType() != typeof(InvalidOperationException), "ex.GetType() != typeof( InvalidOperationException )"); } // symmetric try { Random r = new Random(); SymmetricSignatureProvider provider = new SymmetricSignatureProvider(KeyingMaterial.DefaultSymmetricSecurityKey_256, SecurityAlgorithms.HmacSha256Signature); byte[] bytesin = new byte[1024]; r.NextBytes(bytesin); byte[] signature = provider.Sign(bytesin); Assert.IsTrue(provider.Verify(bytesin, signature), string.Format("Signature did not verify")); } catch (Exception ex) { Assert.Fail(string.Format("Unexpected exception received: '{0}'", ex)); } // symmetric different byte[] sizes try { SymmetricSignatureProvider provider = new SymmetricSignatureProvider(KeyingMaterial.DefaultSymmetricSecurityKey_256, SecurityAlgorithms.HmacSha256Signature); byte[] bytesin = new byte[1024]; byte[] signature = new byte[1024]; Assert.IsFalse(provider.Verify(bytesin, signature), string.Format("Signature did not verify")); } catch (Exception ex) { Assert.Fail(string.Format("Unexpected exception received: '{0}'", ex)); } // unknown algorithm try { Random r = new Random(); SymmetricSignatureProvider provider = new SymmetricSignatureProvider(KeyingMaterial.DefaultSymmetricSecurityKey_256, "SecurityAlgorithms.HmacSha256Signature"); Assert.Fail(string.Format("Should have thrown, it is possible that crypto config mapped this.")); } catch (Exception ex) { Assert.IsFalse(ex.GetType() != typeof(InvalidOperationException), "ex.GetType() != typeof( InvalidOperationException )"); } }
public void AsymmetricSignatureProvider_Dispose() { AsymmetricSignatureProvider provider = new AsymmetricSignatureProvider(KeyingMaterial.DefaultAsymmetricKey_Public_2048, SecurityAlgorithms.RsaSha256Signature); provider.Dispose(); ExpectedException expectedException = ExpectedException.ObjectDisposedException; try { provider.Sign(new byte[256]); } catch (Exception ex) { expectedException.ProcessException(ex); } try { provider.Verify(new byte[256], new byte[256]); } catch (Exception ex) { expectedException.ProcessException(ex); } try { provider.Dispose(); } catch (Exception ex) { Assert.Fail(string.Format("AsymmetricSignatureProvider.Dispose called twice, caught exception: '{0}'", ex)); } }