Pkcs9MessageDigest.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // System.Security.Cryptography.Pkcs.Pkcs9MessageDigest class
  3. //
  4. // Authors:
  5. // Tim Coleman ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) Tim Coleman, 2004
  9. // Copyright (C) 2004-2005 Novell Inc. (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if NET_2_0
  31. using Mono.Security;
  32. namespace System.Security.Cryptography.Pkcs {
  33. public sealed class Pkcs9MessageDigest : Pkcs9AttributeObject {
  34. internal const string oid = "1.2.840.113549.1.9.4";
  35. internal const string friendlyName = "Message Digest";
  36. private byte[] _messageDigest;
  37. private byte[] _encoded;
  38. // constructors
  39. public Pkcs9MessageDigest ()
  40. {
  41. // Pkcs9Attribute remove the "set" accessor on Oid :-(
  42. (this as AsnEncodedData).Oid = new Oid (oid, friendlyName);
  43. _encoded = null;
  44. }
  45. internal Pkcs9MessageDigest (byte[] messageDigest, bool encoded)
  46. {
  47. if (messageDigest == null)
  48. throw new ArgumentNullException ("messageDigest");
  49. if (encoded) {
  50. (this as AsnEncodedData).Oid = new Oid (oid, friendlyName);
  51. RawData = messageDigest;
  52. Decode (messageDigest);
  53. } else {
  54. (this as AsnEncodedData).Oid = new Oid (oid, friendlyName);
  55. _messageDigest = (byte[]) _messageDigest.Clone ();
  56. RawData = Encode ();
  57. }
  58. }
  59. // properties
  60. public byte[] MessageDigest {
  61. get {
  62. if (_encoded != null)
  63. Decode (_encoded);
  64. // FIXME: beta2 returns a reference
  65. return _messageDigest;
  66. }
  67. }
  68. // methods
  69. public override void CopyFrom (AsnEncodedData asnEncodedData)
  70. {
  71. base.CopyFrom (asnEncodedData);
  72. _encoded = asnEncodedData.RawData;
  73. }
  74. // internal stuff
  75. internal void Decode (byte[] attribute)
  76. {
  77. if ((attribute == null) || (attribute [0] != 0x04))
  78. throw new CryptographicException (Locale.GetText ("Expected an OCTETSTRING."));
  79. ASN1 md = new ASN1 (attribute);
  80. _messageDigest = md.Value;
  81. _encoded = null;
  82. }
  83. internal byte[] Encode ()
  84. {
  85. ASN1 md = new ASN1 (0x04, _messageDigest);
  86. return md.GetBytes ();
  87. }
  88. }
  89. }
  90. #endif