| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Security.Cryptography; |
| | | 6 | | using System.Text; |
| | | 7 | | using System.Xml; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.IdentityModel |
| | | 10 | | { |
| | | 11 | | // for sequential use by one thread |
| | | 12 | | internal sealed class SignatureResourcePool |
| | | 13 | | { |
| | | 14 | | private const int BufferSize = 64; |
| | | 15 | | private HashStream _hashStream; |
| | | 16 | | private HashAlgorithm _hashAlgorithm; |
| | | 17 | | private XmlDictionaryWriter _utf8Writer; |
| | | 18 | | private byte[] _encodingBuffer; |
| | | 19 | | private char[] _base64Buffer; |
| | | 20 | | |
| | | 21 | | public char[] TakeBase64Buffer() |
| | | 22 | | { |
| | 0 | 23 | | if (_base64Buffer == null) |
| | | 24 | | { |
| | 0 | 25 | | _base64Buffer = new char[BufferSize]; |
| | | 26 | | } |
| | 0 | 27 | | return _base64Buffer; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | public byte[] TakeEncodingBuffer() |
| | | 31 | | { |
| | 0 | 32 | | if (_encodingBuffer == null) |
| | | 33 | | { |
| | 0 | 34 | | _encodingBuffer = new byte[BufferSize]; |
| | | 35 | | } |
| | 0 | 36 | | return _encodingBuffer; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | public HashAlgorithm TakeHashAlgorithm(string algorithm) |
| | | 40 | | { |
| | 23 | 41 | | if (_hashAlgorithm == null) |
| | | 42 | | { |
| | 23 | 43 | | if (string.IsNullOrEmpty(algorithm)) |
| | | 44 | | { |
| | 0 | 45 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(algorithm, SR.Format("EmptyOrNullArgume |
| | | 46 | | } |
| | | 47 | | |
| | 23 | 48 | | _hashAlgorithm = CryptoHelper.CreateHashAlgorithm(algorithm); |
| | | 49 | | } |
| | | 50 | | else |
| | | 51 | | { |
| | 0 | 52 | | _hashAlgorithm.Initialize(); |
| | | 53 | | } |
| | | 54 | | |
| | 23 | 55 | | return _hashAlgorithm; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | public HashStream TakeHashStream(HashAlgorithm hash) |
| | | 59 | | { |
| | 23 | 60 | | if (_hashStream == null) |
| | | 61 | | { |
| | 23 | 62 | | _hashStream = new HashStream(hash); |
| | | 63 | | } |
| | | 64 | | else |
| | | 65 | | { |
| | 0 | 66 | | _hashStream.Reset(hash); |
| | | 67 | | } |
| | 23 | 68 | | return _hashStream; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | public HashStream TakeHashStream(string algorithm) |
| | | 72 | | { |
| | 23 | 73 | | return TakeHashStream(TakeHashAlgorithm(algorithm)); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | public XmlDictionaryWriter TakeUtf8Writer() |
| | | 77 | | { |
| | 0 | 78 | | if (_utf8Writer == null) |
| | | 79 | | { |
| | 0 | 80 | | _utf8Writer = XmlDictionaryWriter.CreateTextWriter(Stream.Null, Encoding.UTF8, false); |
| | | 81 | | } |
| | | 82 | | else |
| | | 83 | | { |
| | 0 | 84 | | ((IXmlTextWriterInitializer)_utf8Writer).SetOutput(Stream.Null, Encoding.UTF8, false); |
| | | 85 | | } |
| | 0 | 86 | | return _utf8Writer; |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | } |