XmlDsigBase64Transform.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. //
  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. Algorithm = "http://www.w3.org/2000/09/xmldsig#base64";
  20. }
  21. public override Type[] InputTypes {
  22. get {
  23. Type[] input = new Type [3];
  24. input[0] = typeof (System.IO.Stream);
  25. input[1] = typeof (System.Xml.XmlDocument);
  26. input[2] = typeof (System.Xml.XmlNodeList);
  27. return input;
  28. }
  29. }
  30. public override Type[] OutputTypes {
  31. get {
  32. Type[] output = new Type [1];
  33. output[0] = typeof (System.IO.Stream);
  34. return output;
  35. }
  36. }
  37. protected override XmlNodeList GetInnerXml ()
  38. {
  39. return null; // THIS IS DOCUMENTED AS SUCH
  40. }
  41. public override object GetOutput ()
  42. {
  43. return (object) cs;
  44. }
  45. public override object GetOutput (Type type)
  46. {
  47. if (type != typeof (System.IO.Stream))
  48. throw new ArgumentException ("type");
  49. return GetOutput ();
  50. }
  51. public override void LoadInnerXml (XmlNodeList nodeList)
  52. {
  53. // documented as not changing the state of the transform
  54. }
  55. [MonoTODO("There's still one unit test failing")]
  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).ChildNodes;
  64. else if (obj is XmlNodeList)
  65. xnl = (XmlNodeList) obj;
  66. if (xnl != null) {
  67. StringBuilder sb = new StringBuilder ();
  68. foreach (XmlNode xn in xnl) {
  69. if (xn is XmlElement)
  70. sb.Append (xn.InnerText);
  71. }
  72. byte[] data = Encoding.UTF8.GetBytes (sb.ToString ());
  73. stream = new MemoryStream (data);
  74. }
  75. if (stream != null)
  76. cs = new CryptoStream (stream, new FromBase64Transform (), CryptoStreamMode.Read);
  77. // note: there is no default are other types won't throw an exception
  78. }
  79. }
  80. }