2004-02-07 Tim Coleman <tim@timcoleman.com>
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / EncryptionMethod.cs
1 //
2 // EncryptionMethod.cs - EncryptionMethod implementation for XML Encryption
3 // http://www.w3.org/2001/04/xmlenc#sec-EncryptionMethod
4 //
5 // Author:
6 //      Tim Coleman (tim@timcoleman.com)
7 //
8 // Copyright (C) Tim Coleman, 2004
9
10 #if NET_1_2
11
12 using System.Xml;
13
14 namespace System.Security.Cryptography.Xml {
15         public class EncryptionMethod {
16
17                 #region Fields
18
19                 string algorithm;
20                 int keySize;
21
22                 #endregion // Fields
23         
24                 #region Constructors
25
26                 public EncryptionMethod ()
27                 {
28                         KeyAlgorithm = null;
29                 }
30
31                 public EncryptionMethod (string strAlgorithm)
32                 {
33                         KeyAlgorithm = strAlgorithm;
34                 }
35
36                 #endregion // Constructors
37
38                 #region Properties
39
40                 public string KeyAlgorithm {
41                         get { return algorithm; }
42                         set { algorithm = value; }
43                 }
44
45                 public int KeySize {
46                         get { return keySize; }
47                         set {
48                                 if (value <= 0)
49                                         throw new ArgumentOutOfRangeException ("The key size should be a non negative integer.");
50                                 keySize = value; 
51                         }
52                 }
53
54                 #endregion // Properties
55
56                 #region Methods
57
58                 public XmlElement GetXml ()
59                 {
60                         return GetXml (new XmlDocument ());
61                 }
62
63                 internal XmlElement GetXml (XmlDocument document)
64                 {
65                         XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptionMethod, XmlEncryption.NamespaceURI);
66
67                         if (KeySize != 0) {
68                                 XmlElement xks = document.CreateElement (XmlEncryption.ElementNames.KeySize, XmlEncryption.NamespaceURI);
69                                 xks.InnerText = String.Format ("{0}", keySize);
70                                 xel.AppendChild (xks);
71                         }
72
73                         if (KeyAlgorithm != null)
74                                 xel.SetAttribute (XmlEncryption.AttributeNames.Algorithm, KeyAlgorithm);
75                         return xel;
76                 }
77
78                 public void LoadXml (XmlElement value)
79                 {
80                         if (value == null)
81                                 throw new ArgumentNullException ("value");
82                         if ((value.LocalName != XmlEncryption.ElementNames.EncryptionMethod) || (value.NamespaceURI != XmlEncryption.NamespaceURI))
83                                 throw new CryptographicException ("Malformed EncryptionMethod element.");
84                         else {
85                                 KeyAlgorithm = null;
86                                 foreach (XmlNode n in value.ChildNodes) {
87                                         if (n is XmlWhitespace)
88                                                 continue;
89                                         switch (n.LocalName) {
90                                         case XmlEncryption.ElementNames.KeySize:
91                                                 KeySize = Int32.Parse (n.InnerText);
92                                                 break;
93                                         }
94                                 }
95                                 if (value.HasAttribute (XmlEncryption.AttributeNames.Algorithm))
96                                         KeyAlgorithm = value.Attributes [XmlEncryption.AttributeNames.Algorithm].Value;
97                         }
98                 }
99
100                 #endregion // Methods
101         }
102 }
103
104 #endif