2004-05-31 Atsushi Enomoto <atsushi@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                 public override void LoadInput (object obj) 
69                 {
70                         XmlNodeList xnl = null;
71                         Stream stream = null;
72
73                         if (obj is Stream) 
74                                 stream = (obj as Stream);
75                         else if (obj is XmlDocument)
76                                 xnl = (obj as XmlDocument).SelectNodes ("//.");
77                         else if (obj is XmlNodeList)
78                                 xnl = (XmlNodeList) obj;
79
80                         if (xnl != null) {
81                                 stream = new MemoryStream ();
82                                 StreamWriter sw = new StreamWriter (stream);
83                                 foreach (XmlNode xn in xnl) {
84                                         switch (xn.NodeType) {
85                                         case XmlNodeType.Attribute:
86                                         case XmlNodeType.Text:
87                                         case XmlNodeType.CDATA:
88                                         case XmlNodeType.SignificantWhitespace:
89                                         case XmlNodeType.Whitespace:
90                                                 sw.Write (xn.Value);
91                                                 break;
92                                         }
93                                 }
94                                 sw.Flush ();
95                                 // ready to be re-used
96                                 stream.Position = 0;
97                         }
98
99                         if (stream != null)
100                                 cs = new CryptoStream (stream, new FromBase64Transform (), CryptoStreamMode.Read);
101                         // note: there is no default are other types won't throw an exception
102                 }
103         }
104 }