DSASignatureFormatter.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //
  2. // System.Security.Cryptography DSASignatureFormatter.cs
  3. //
  4. // Authors:
  5. // Thomas Neidhart ([email protected])
  6. // Sebastien Pouliot ([email protected])
  7. //
  8. // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  9. //
  10. using System;
  11. namespace System.Security.Cryptography {
  12. /// <summary>
  13. /// DSA Signature Formatter
  14. /// </summary>
  15. public class DSASignatureFormatter : AsymmetricSignatureFormatter {
  16. private DSA dsa;
  17. public DSASignatureFormatter () {}
  18. public DSASignatureFormatter (AsymmetricAlgorithm key)
  19. {
  20. SetKey (key);
  21. }
  22. public override byte[] CreateSignature (byte[] rgbHash)
  23. {
  24. if (dsa == null)
  25. throw new CryptographicUnexpectedOperationException ("missing key");
  26. return dsa.CreateSignature (rgbHash);
  27. }
  28. public override void SetHashAlgorithm (string strName)
  29. {
  30. if (strName == null)
  31. throw new ArgumentNullException ("strName");
  32. try {
  33. // just to test, we don't need the object
  34. SHA1 hash = SHA1.Create (strName);
  35. }
  36. catch {
  37. throw new CryptographicUnexpectedOperationException ("DSA requires SHA1");
  38. }
  39. }
  40. public override void SetKey (AsymmetricAlgorithm key)
  41. {
  42. if (key != null) {
  43. // this will throw a InvalidCastException if this isn't
  44. // a DSA keypair
  45. dsa = (DSA) key;
  46. }
  47. // here null is accepted!
  48. }
  49. } // DSASignatureFormatter
  50. } // System.Security.Cryptography