XmlDsigBase64Transform.cs 2.5 KB

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