XmlDsigBase64Transform.cs 2.4 KB

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