RSAPKCS1SignatureFormatter.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //
  2. // RSAPKCS1SignatureFormatter.cs - Handles PKCS#1 v.1.5 signature encryption.
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using System;
  10. namespace System.Security.Cryptography {
  11. public class RSAPKCS1SignatureFormatter : AsymmetricSignatureFormatter {
  12. private RSA rsa;
  13. private HashAlgorithm hash;
  14. public RSAPKCS1SignatureFormatter ()
  15. {
  16. rsa = null;
  17. }
  18. public RSAPKCS1SignatureFormatter (AsymmetricAlgorithm key)
  19. {
  20. SetKey (key);
  21. }
  22. public override byte[] CreateSignature (byte[] rgbHash)
  23. {
  24. if ((rsa == null) || (hash == null))
  25. throw new CryptographicUnexpectedOperationException ();
  26. if (rgbHash == null)
  27. throw new ArgumentNullException ();
  28. string oid = CryptoConfig.MapNameToOID (hash.ToString ());
  29. return PKCS1.Sign_v15 (rsa, oid, rgbHash);
  30. }
  31. public override void SetHashAlgorithm (string strName)
  32. {
  33. hash = HashAlgorithm.Create (strName);
  34. }
  35. public override void SetKey (AsymmetricAlgorithm key)
  36. {
  37. if (key != null) {
  38. if (key is RSA) {
  39. rsa = (RSA)key;
  40. }
  41. else
  42. throw new InvalidCastException ();
  43. }
  44. // here null is accepted!
  45. }
  46. }
  47. }