DsaDigitalSignatureTest.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Text;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Renci.SshNet.Abstractions;
  4. using Renci.SshNet.Security;
  5. using Renci.SshNet.Security.Cryptography;
  6. using Renci.SshNet.Tests.Common;
  7. namespace Renci.SshNet.Tests.Classes.Security.Cryptography
  8. {
  9. [TestClass]
  10. public class DsaDigitalSignatureTest : TestBase
  11. {
  12. [TestMethod]
  13. public void Verify()
  14. {
  15. byte[] data = Encoding.UTF8.GetBytes("Hello, World!");
  16. DsaKey dsaKey = GetDsaKey("Key.DSA.txt");
  17. Assert.AreEqual(1024, dsaKey.P.BitLength);
  18. Assert.AreEqual(160, dsaKey.Q.BitLength);
  19. var digitalSignature = new DsaDigitalSignature(dsaKey);
  20. byte[] signedBytes = digitalSignature.Sign(data);
  21. // We can't compare signatures for value equality because they have a source of randomness
  22. Assert.AreEqual(40, signedBytes.Length);
  23. Assert.IsTrue(digitalSignature.Verify(data, signedBytes));
  24. byte[] signatureToVerify = new byte[]
  25. {
  26. // Generated with a previous DsaDigitalSignature implementation in order to confirm consistent
  27. // behaviour. We can't seem to validate against openssl because openssl outputs a DER signature,
  28. // where as we want IEEE P1363 (fixed size) format.
  29. 0x07, 0x4c, 0x5e, 0x15, 0x53, 0x36, 0x21, 0xbe, 0x5a, 0x82, 0x35, 0xd5, 0xb6, 0xe6, 0x7d, 0x2f,
  30. 0x01, 0x2a, 0x78, 0x9b, 0x16, 0x4a, 0xe5, 0x8d, 0x85, 0xa6, 0x34, 0x56, 0x9d, 0x38, 0xd6, 0x1a,
  31. 0xa4, 0xa1, 0x5b, 0x98, 0x7d, 0xd5, 0x35, 0x40
  32. };
  33. Assert.IsTrue(digitalSignature.Verify(data, signatureToVerify));
  34. Assert.IsFalse(digitalSignature.Verify(data, CryptoAbstraction.GenerateRandom(40)));
  35. }
  36. private static DsaKey GetDsaKey(string fileName, string passPhrase = null)
  37. {
  38. using (var stream = GetData(fileName))
  39. {
  40. return (DsaKey)new PrivateKeyFile(stream, passPhrase).Key;
  41. }
  42. }
  43. }
  44. }