XmlDsigBase64Transform.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. [MonoTODO("There's still one unit test failing")]
  57. public override void LoadInput (object obj)
  58. {
  59. XmlNodeList xnl = null;
  60. Stream stream = null;
  61. if (obj is Stream)
  62. stream = (obj as Stream);
  63. else if (obj is XmlDocument)
  64. xnl = (obj as XmlDocument).ChildNodes;
  65. else if (obj is XmlNodeList)
  66. xnl = (XmlNodeList) obj;
  67. if (xnl != null) {
  68. stream = new MemoryStream ();
  69. StreamWriter sw = new StreamWriter (stream);
  70. foreach (XmlNode xn in xnl) {
  71. if (xn is XmlElement)
  72. sw.Write (xn.InnerText);
  73. }
  74. sw.Flush ();
  75. // ready to be re-used
  76. stream.Position = 0;
  77. }
  78. if (stream != null)
  79. cs = new CryptoStream (stream, new FromBase64Transform (), CryptoStreamMode.Read);
  80. // note: there is no default are other types won't throw an exception
  81. }
  82. }
  83. }