**** Merged from MCS ****
[mono-project.git] / mcs / class / Microsoft.Web.Services / Microsoft.Web.Services.Security / Nonce.cs
blobf9fafc21972c92dacbf09ced019168a66dc4d979
1 //
2 // Nonce.cs: Handles WS-Security Nonce
3 //
4 // Author:
5 // Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
10 using System;
11 using System.Security.Cryptography;
12 using System.Xml;
13 using Microsoft.Web.Services;
14 #if !WSE1
15 using Microsoft.Web.Services.Xml;
16 #endif
18 namespace Microsoft.Web.Services.Security {
20 // References:
21 // a. Web Services Security Addendum, Version 1.0, August 18, 2002
22 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnglobspec/html/ws-security.asp
23 // b. Understanding WS-Security
24 // http://msdn.microsoft.com/webservices/building/frameworkandstudio/default.aspx?pull=/library/en-us/dnwssecur/html/understw.asp
26 public class Nonce : IXmlElement {
28 private byte[] nonce;
30 #if WSE1
31 internal Nonce () : this (16) {}
32 internal Nonce (int size)
33 #else
34 //Compilation fix, spoulit please check the authenticity of this.
35 public Nonce () : this (16) {}
36 public Nonce (int size)
37 #endif
39 nonce = new byte [size]; // default is 16 see reference b.
40 if (size > 0) {
41 RandomNumberGenerator rng = RandomNumberGenerator.Create ();
42 rng.GetBytes (nonce);
46 #if WSE1
47 internal Nonce (XmlElement element)
48 #else
49 public Nonce (XmlElement element)
50 #endif
52 LoadXml (element);
55 public string Value {
56 get { return Convert.ToBase64String (nonce); }
59 public byte[] GetValueBytes ()
61 return (byte[]) nonce.Clone ();
64 public XmlElement GetXml (XmlDocument document)
66 if (document == null)
67 throw new ArgumentNullException ("document");
68 // much cleaner than using StringBuilder!
69 XmlElement xel = document.CreateElement (WSSecurity.Prefix, WSSecurity.ElementNames.Nonce, WSSecurity.NamespaceURI);
70 xel.InnerText = Convert.ToBase64String (nonce);
71 return xel;
74 public void LoadXml (XmlElement element)
76 if (element == null)
77 throw new ArgumentNullException ("element");
78 if ((element.LocalName != WSSecurity.ElementNames.Nonce) || (element.NamespaceURI != WSSecurity.NamespaceURI))
79 throw new ArgumentException ("invalid LocalName or NamespaceURI");
81 XmlAttribute xa = element.Attributes [WSSecurity.AttributeNames.EncodingType, WSSecurity.NamespaceURI];
82 if ((xa == null) || (xa.Value == "Base64Binary")) {
83 nonce = Convert.FromBase64String (element.InnerText);
85 else
86 throw new NotSupportedException (xa.Value);