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