1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4 
5 namespace System.IdentityModel
6 {
7     using System.IO;
8     using System.Security.Cryptography;
9     using System.IdentityModel.Tokens;
10     using System.Xml;
11     using System.Text;
12 
13     // for sequential use by one thread
14     sealed class SignatureResourcePool
15     {
16         const int BufferSize = 64;
17         CanonicalizationDriver canonicalizationDriver;
18         HashStream hashStream;
19         HashAlgorithm hashAlgorithm;
20 #if NO
21         XmlC14NWriter integratedWriter;
22 #endif
23         XmlDictionaryWriter utf8Writer;
24         byte[] encodingBuffer;
25         char[] base64Buffer;
26 
TakeBase64Buffer()27         public char[] TakeBase64Buffer()
28         {
29             if (this.base64Buffer == null)
30             {
31                 this.base64Buffer = new char[BufferSize];
32             }
33             return this.base64Buffer;
34         }
35 
TakeCanonicalizationDriver()36         public CanonicalizationDriver TakeCanonicalizationDriver()
37         {
38             if (this.canonicalizationDriver == null)
39             {
40                 this.canonicalizationDriver = new CanonicalizationDriver();
41             }
42             else
43             {
44                 this.canonicalizationDriver.Reset();
45             }
46             return this.canonicalizationDriver;
47         }
48 
TakeEncodingBuffer()49         public byte[] TakeEncodingBuffer()
50         {
51             if (this.encodingBuffer == null)
52             {
53                 this.encodingBuffer = new byte[BufferSize];
54             }
55             return this.encodingBuffer;
56         }
57 
TakeHashAlgorithm(string algorithm)58         public HashAlgorithm TakeHashAlgorithm(string algorithm)
59         {
60             if ( this.hashAlgorithm == null )
61             {
62                 if ( string.IsNullOrEmpty( algorithm ) )
63                 {
64                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( algorithm, SR.GetString( SR.EmptyOrNullArgumentString, "algorithm" ) );
65                 }
66 
67                 this.hashAlgorithm = CryptoHelper.CreateHashAlgorithm( algorithm );
68             }
69             else
70             {
71                 this.hashAlgorithm.Initialize();
72             }
73 
74             return this.hashAlgorithm;
75         }
76 
TakeHashStream(HashAlgorithm hash)77         public HashStream TakeHashStream(HashAlgorithm hash)
78         {
79             if (this.hashStream == null)
80             {
81                 this.hashStream = new HashStream(hash);
82             }
83             else
84             {
85                 this.hashStream.Reset(hash);
86             }
87             return this.hashStream;
88         }
89 
TakeHashStream(string algorithm)90         public HashStream TakeHashStream(string algorithm)
91         {
92             return TakeHashStream(TakeHashAlgorithm(algorithm));
93         }
94 #if NO
TakeIntegratedWriter(Stream stream)95         public XmlC14NWriter TakeIntegratedWriter(Stream stream)
96         {
97             return TakeIntegratedWriter(stream, false, null);
98         }
99 
TakeIntegratedWriter(Stream stream, bool includeComments, string[] inclusivePrefixes)100         public XmlC14NWriter TakeIntegratedWriter(Stream stream, bool includeComments, string[] inclusivePrefixes)
101         {
102             if (this.integratedWriter == null)
103             {
104                 this.integratedWriter = new XmlC14NWriter(stream, includeComments, inclusivePrefixes);
105             }
106             else
107             {
108                 this.integratedWriter.SetOutput(stream, includeComments, inclusivePrefixes);
109             }
110             return this.integratedWriter;
111         }
112 #endif
113 
TakeUtf8Writer()114         public XmlDictionaryWriter TakeUtf8Writer()
115         {
116             if (this.utf8Writer == null)
117             {
118                 this.utf8Writer = XmlDictionaryWriter.CreateTextWriter(Stream.Null, Encoding.UTF8, false);
119             }
120             else
121             {
122                 ((IXmlTextWriterInitializer) this.utf8Writer).SetOutput(Stream.Null, Encoding.UTF8, false);
123             }
124             return this.utf8Writer;
125         }
126     }
127 }
128