XmlDsigBase64Transform.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // XmlDsigBase64Transform.cs - Base64 Transform implementation for XML Signature
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using System.IO;
  10. using System.Security.Cryptography;
  11. using System.Text;
  12. using System.Xml;
  13. namespace System.Security.Cryptography.Xml {
  14. // http://www.w3.org/2000/09/xmldsig#base64
  15. public class XmlDsigBase64Transform : Transform {
  16. private CryptoStream cs;
  17. public XmlDsigBase64Transform ()
  18. {
  19. algo = "http://www.w3.org/2000/09/xmldsig#base64";
  20. }
  21. public override Type[] InputTypes {
  22. get {
  23. if (input == null) {
  24. lock (this) {
  25. // this way the result is cached if called multiple time
  26. input = new Type [3];
  27. input[0] = typeof (System.IO.Stream);
  28. input[1] = typeof (System.Xml.XmlDocument);
  29. input[2] = typeof (System.Xml.XmlNodeList);
  30. }
  31. }
  32. return input;
  33. }
  34. }
  35. public override Type[] OutputTypes {
  36. get {
  37. if (output == null) {
  38. lock (this) {
  39. // this way the result is cached if called multiple time
  40. output = new Type [1];
  41. output[0] = typeof (System.IO.Stream);
  42. }
  43. }
  44. return output;
  45. }
  46. }
  47. protected override XmlNodeList GetInnerXml ()
  48. {
  49. return null; // THIS IS DOCUMENTED AS SUCH
  50. }
  51. public override object GetOutput ()
  52. {
  53. return (object) cs;
  54. }
  55. public override object GetOutput (Type type)
  56. {
  57. if (type != Type.GetType ("System.IO.Stream"))
  58. throw new ArgumentException ("type");
  59. return GetOutput ();
  60. }
  61. public override void LoadInnerXml (XmlNodeList nodeList)
  62. {
  63. // documented as not changing the state of the transform
  64. }
  65. public override void LoadInput (object obj)
  66. {
  67. XmlNodeList xnl = null;
  68. Stream stream = null;
  69. if (obj is Stream)
  70. stream = (obj as Stream);
  71. else if (obj is XmlDocument)
  72. xnl = (obj as XmlDocument).ChildNodes;
  73. else if (obj is XmlNodeList)
  74. xnl = (XmlNodeList) obj;
  75. if (xnl != null) {
  76. StringBuilder sb = new StringBuilder ();
  77. foreach (XmlNode xn in xnl)
  78. sb.Append (xn.InnerText);
  79. UTF8Encoding utf8 = new UTF8Encoding ();
  80. byte[] data = utf8.GetBytes (sb.ToString ());
  81. stream = new MemoryStream (data);
  82. }
  83. if (stream != null)
  84. cs = new CryptoStream (stream, new FromBase64Transform (), CryptoStreamMode.Read);
  85. // note: there is no default are other types won't throw an exception
  86. }
  87. }
  88. }