Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Security / Cryptography / base64Transforms.cs / 1305376 / base64Transforms.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== //[....] // // // Base64Transform.cs // // This file contains two ICryptoTransforms: ToBase64Transform and FromBase64Transform // they may be attached to a CryptoStream in either read or write mode namespace System.Security.Cryptography { using System; using System.IO; using System.Text; using System.Diagnostics.Contracts; #if !SILVERLIGHT [Serializable] [System.Runtime.InteropServices.ComVisible(true)] #endif // !SILVERLIGHT public enum FromBase64TransformMode { IgnoreWhiteSpaces = 0, DoNotIgnoreWhiteSpaces = 1, } #if !SILVERLIGHT [System.Runtime.InteropServices.ComVisible(true)] public class ToBase64Transform : ICryptoTransform { // converting to Base64 takes 3 bytes input and generates 4 bytes output public int InputBlockSize { get { return(3); } } public int OutputBlockSize { get { return(4); } } public bool CanTransformMultipleBlocks { get { return(false); } } public virtual bool CanReuseTransform { get { return(true); } } public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) { // Do some validation if (inputBuffer == null) throw new ArgumentNullException("inputBuffer"); if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue")); if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); Contract.EndContractBlock(); // for now, only convert 3 bytes to 4 char[] temp = new char[4]; Convert.ToBase64CharArray(inputBuffer, inputOffset, 3, temp, 0); byte[] tempBytes = Encoding.ASCII.GetBytes(temp); if (tempBytes.Length != 4) throw new CryptographicException(Environment.GetResourceString( "Cryptography_SSE_InvalidDataSize" )); Buffer.BlockCopy(tempBytes, 0, outputBuffer, outputOffset, tempBytes.Length); return(tempBytes.Length); } public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) { // Do some validation if (inputBuffer == null) throw new ArgumentNullException("inputBuffer"); if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue")); if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); Contract.EndContractBlock(); // Convert.ToBase64CharArray already does padding, so all we have to check is that // the inputCount wasn't 0 // again, for now only a block at a time if (inputCount == 0) { return(new byte[0]); } else { char[] temp = new char[4]; Convert.ToBase64CharArray(inputBuffer, inputOffset, inputCount, temp, 0); byte[] tempBytes = Encoding.ASCII.GetBytes(temp); return(tempBytes); } } // must implement IDisposable, but in this case there's nothing to do. public void Dispose() { Clear(); } public void Clear() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { } ~ToBase64Transform() { // // A finalizer is not necessary here, however since we shipped a finalizer that called // Dispose(false) in v2.0, we need to keep it around in case any existing code had subclassed // this transform and expects to have a base class finalizer call its dispose method // Dispose(false); } } #endif // !SILVERLIGHT #if !SILVERLIGHT [System.Runtime.InteropServices.ComVisible(true)] #endif // !SILVERLIGHT public class FromBase64Transform : ICryptoTransform { private byte[] _inputBuffer = new byte[4]; private int _inputIndex; private FromBase64TransformMode _whitespaces; // Constructors public FromBase64Transform() : this(FromBase64TransformMode.IgnoreWhiteSpaces) {} public FromBase64Transform(FromBase64TransformMode whitespaces) { _whitespaces = whitespaces; _inputIndex = 0; } // converting from Base64 generates 3 bytes output from each 4 bytes input block public int InputBlockSize { get { return(1); } } public int OutputBlockSize { get { return(3); } } public bool CanTransformMultipleBlocks { get { return(false); } } public virtual bool CanReuseTransform { get { return(true); } } [System.Security.SecuritySafeCritical] // auto-generated public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) { // Do some validation if (inputBuffer == null) throw new ArgumentNullException("inputBuffer"); if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue")); if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); Contract.EndContractBlock(); if (_inputBuffer == null) throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic")); byte[] temp = new byte[inputCount]; char[] tempChar; int effectiveCount; if (_whitespaces == FromBase64TransformMode.IgnoreWhiteSpaces) { temp = DiscardWhiteSpaces(inputBuffer, inputOffset, inputCount); effectiveCount = temp.Length; } else { Buffer.InternalBlockCopy(inputBuffer, inputOffset, temp, 0, inputCount); effectiveCount = inputCount; } if (effectiveCount + _inputIndex < 4) { Buffer.InternalBlockCopy(temp, 0, _inputBuffer, _inputIndex, effectiveCount); _inputIndex += effectiveCount; return 0; } // Get the number of 4 bytes blocks to transform int numBlocks = (effectiveCount + _inputIndex) / 4; byte[] transformBuffer = new byte[_inputIndex + effectiveCount]; Buffer.InternalBlockCopy(_inputBuffer, 0, transformBuffer, 0, _inputIndex); Buffer.InternalBlockCopy(temp, 0, transformBuffer, _inputIndex, effectiveCount); _inputIndex = (effectiveCount + _inputIndex) % 4; Buffer.InternalBlockCopy(temp, effectiveCount - _inputIndex, _inputBuffer, 0, _inputIndex); #if !SILVERLIGHT tempChar = Encoding.ASCII.GetChars(transformBuffer, 0, 4*numBlocks); #else // !SILVERLIGHT tempChar = Encoding.UTF8.GetChars(transformBuffer, 0, 4 * numBlocks); #endif // !SILVERLIGHT byte[] tempBytes = Convert.FromBase64CharArray(tempChar, 0, 4*numBlocks); Buffer.BlockCopy(tempBytes, 0, outputBuffer, outputOffset, tempBytes.Length); return(tempBytes.Length); } [System.Security.SecuritySafeCritical] // auto-generated public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) { // Do some validation if (inputBuffer == null) throw new ArgumentNullException("inputBuffer"); if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue")); if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); Contract.EndContractBlock(); if (_inputBuffer == null) throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic")); byte[] temp = new byte[inputCount]; char[] tempChar; int effectiveCount; if (_whitespaces == FromBase64TransformMode.IgnoreWhiteSpaces) { temp = DiscardWhiteSpaces(inputBuffer, inputOffset, inputCount); effectiveCount = temp.Length; } else { Buffer.InternalBlockCopy(inputBuffer, inputOffset, temp, 0, inputCount); effectiveCount = inputCount; } if (effectiveCount + _inputIndex < 4) { Reset(); return (new byte[0]); } // Get the number of 4 bytes blocks to transform int numBlocks = (effectiveCount + _inputIndex) / 4; byte[] transformBuffer = new byte[_inputIndex + effectiveCount]; Buffer.InternalBlockCopy(_inputBuffer, 0, transformBuffer, 0, _inputIndex); Buffer.InternalBlockCopy(temp, 0, transformBuffer, _inputIndex, effectiveCount); _inputIndex = (effectiveCount + _inputIndex) % 4; Buffer.InternalBlockCopy(temp, effectiveCount - _inputIndex, _inputBuffer, 0, _inputIndex); #if !SILVERLIGHT tempChar = Encoding.ASCII.GetChars(transformBuffer, 0, 4*numBlocks); #else // !SILVERLIGHT tempChar = Encoding.UTF8.GetChars(transformBuffer, 0, 4 * numBlocks); #endif // !SILVERLIGHT byte[] tempBytes = Convert.FromBase64CharArray(tempChar, 0, 4*numBlocks); // reinitialize the transform Reset(); return(tempBytes); } private byte[] DiscardWhiteSpaces(byte[] inputBuffer, int inputOffset, int inputCount) { int i, iCount = 0; for (i=0; i[....] // // // Base64Transform.cs // // This file contains two ICryptoTransforms: ToBase64Transform and FromBase64Transform // they may be attached to a CryptoStream in either read or write mode namespace System.Security.Cryptography { using System; using System.IO; using System.Text; using System.Diagnostics.Contracts; #if !SILVERLIGHT [Serializable] [System.Runtime.InteropServices.ComVisible(true)] #endif // !SILVERLIGHT public enum FromBase64TransformMode { IgnoreWhiteSpaces = 0, DoNotIgnoreWhiteSpaces = 1, } #if !SILVERLIGHT [System.Runtime.InteropServices.ComVisible(true)] public class ToBase64Transform : ICryptoTransform { // converting to Base64 takes 3 bytes input and generates 4 bytes output public int InputBlockSize { get { return(3); } } public int OutputBlockSize { get { return(4); } } public bool CanTransformMultipleBlocks { get { return(false); } } public virtual bool CanReuseTransform { get { return(true); } } public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) { // Do some validation if (inputBuffer == null) throw new ArgumentNullException("inputBuffer"); if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue")); if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); Contract.EndContractBlock(); // for now, only convert 3 bytes to 4 char[] temp = new char[4]; Convert.ToBase64CharArray(inputBuffer, inputOffset, 3, temp, 0); byte[] tempBytes = Encoding.ASCII.GetBytes(temp); if (tempBytes.Length != 4) throw new CryptographicException(Environment.GetResourceString( "Cryptography_SSE_InvalidDataSize" )); Buffer.BlockCopy(tempBytes, 0, outputBuffer, outputOffset, tempBytes.Length); return(tempBytes.Length); } public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) { // Do some validation if (inputBuffer == null) throw new ArgumentNullException("inputBuffer"); if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue")); if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); Contract.EndContractBlock(); // Convert.ToBase64CharArray already does padding, so all we have to check is that // the inputCount wasn't 0 // again, for now only a block at a time if (inputCount == 0) { return(new byte[0]); } else { char[] temp = new char[4]; Convert.ToBase64CharArray(inputBuffer, inputOffset, inputCount, temp, 0); byte[] tempBytes = Encoding.ASCII.GetBytes(temp); return(tempBytes); } } // must implement IDisposable, but in this case there's nothing to do. public void Dispose() { Clear(); } public void Clear() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { } ~ToBase64Transform() { // // A finalizer is not necessary here, however since we shipped a finalizer that called // Dispose(false) in v2.0, we need to keep it around in case any existing code had subclassed // this transform and expects to have a base class finalizer call its dispose method // Dispose(false); } } #endif // !SILVERLIGHT #if !SILVERLIGHT [System.Runtime.InteropServices.ComVisible(true)] #endif // !SILVERLIGHT public class FromBase64Transform : ICryptoTransform { private byte[] _inputBuffer = new byte[4]; private int _inputIndex; private FromBase64TransformMode _whitespaces; // Constructors public FromBase64Transform() : this(FromBase64TransformMode.IgnoreWhiteSpaces) {} public FromBase64Transform(FromBase64TransformMode whitespaces) { _whitespaces = whitespaces; _inputIndex = 0; } // converting from Base64 generates 3 bytes output from each 4 bytes input block public int InputBlockSize { get { return(1); } } public int OutputBlockSize { get { return(3); } } public bool CanTransformMultipleBlocks { get { return(false); } } public virtual bool CanReuseTransform { get { return(true); } } [System.Security.SecuritySafeCritical] // auto-generated public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) { // Do some validation if (inputBuffer == null) throw new ArgumentNullException("inputBuffer"); if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue")); if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); Contract.EndContractBlock(); if (_inputBuffer == null) throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic")); byte[] temp = new byte[inputCount]; char[] tempChar; int effectiveCount; if (_whitespaces == FromBase64TransformMode.IgnoreWhiteSpaces) { temp = DiscardWhiteSpaces(inputBuffer, inputOffset, inputCount); effectiveCount = temp.Length; } else { Buffer.InternalBlockCopy(inputBuffer, inputOffset, temp, 0, inputCount); effectiveCount = inputCount; } if (effectiveCount + _inputIndex < 4) { Buffer.InternalBlockCopy(temp, 0, _inputBuffer, _inputIndex, effectiveCount); _inputIndex += effectiveCount; return 0; } // Get the number of 4 bytes blocks to transform int numBlocks = (effectiveCount + _inputIndex) / 4; byte[] transformBuffer = new byte[_inputIndex + effectiveCount]; Buffer.InternalBlockCopy(_inputBuffer, 0, transformBuffer, 0, _inputIndex); Buffer.InternalBlockCopy(temp, 0, transformBuffer, _inputIndex, effectiveCount); _inputIndex = (effectiveCount + _inputIndex) % 4; Buffer.InternalBlockCopy(temp, effectiveCount - _inputIndex, _inputBuffer, 0, _inputIndex); #if !SILVERLIGHT tempChar = Encoding.ASCII.GetChars(transformBuffer, 0, 4*numBlocks); #else // !SILVERLIGHT tempChar = Encoding.UTF8.GetChars(transformBuffer, 0, 4 * numBlocks); #endif // !SILVERLIGHT byte[] tempBytes = Convert.FromBase64CharArray(tempChar, 0, 4*numBlocks); Buffer.BlockCopy(tempBytes, 0, outputBuffer, outputOffset, tempBytes.Length); return(tempBytes.Length); } [System.Security.SecuritySafeCritical] // auto-generated public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) { // Do some validation if (inputBuffer == null) throw new ArgumentNullException("inputBuffer"); if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue")); if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); Contract.EndContractBlock(); if (_inputBuffer == null) throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic")); byte[] temp = new byte[inputCount]; char[] tempChar; int effectiveCount; if (_whitespaces == FromBase64TransformMode.IgnoreWhiteSpaces) { temp = DiscardWhiteSpaces(inputBuffer, inputOffset, inputCount); effectiveCount = temp.Length; } else { Buffer.InternalBlockCopy(inputBuffer, inputOffset, temp, 0, inputCount); effectiveCount = inputCount; } if (effectiveCount + _inputIndex < 4) { Reset(); return (new byte[0]); } // Get the number of 4 bytes blocks to transform int numBlocks = (effectiveCount + _inputIndex) / 4; byte[] transformBuffer = new byte[_inputIndex + effectiveCount]; Buffer.InternalBlockCopy(_inputBuffer, 0, transformBuffer, 0, _inputIndex); Buffer.InternalBlockCopy(temp, 0, transformBuffer, _inputIndex, effectiveCount); _inputIndex = (effectiveCount + _inputIndex) % 4; Buffer.InternalBlockCopy(temp, effectiveCount - _inputIndex, _inputBuffer, 0, _inputIndex); #if !SILVERLIGHT tempChar = Encoding.ASCII.GetChars(transformBuffer, 0, 4*numBlocks); #else // !SILVERLIGHT tempChar = Encoding.UTF8.GetChars(transformBuffer, 0, 4 * numBlocks); #endif // !SILVERLIGHT byte[] tempBytes = Convert.FromBase64CharArray(tempChar, 0, 4*numBlocks); // reinitialize the transform Reset(); return(tempBytes); } private byte[] DiscardWhiteSpaces(byte[] inputBuffer, int inputOffset, int inputCount) { int i, iCount = 0; for (i=0; i
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- HideDisabledControlAdapter.cs
- BrowserCapabilitiesCompiler.cs
- GenericAuthenticationEventArgs.cs
- TemplateField.cs
- QueryableDataSourceView.cs
- TypeInfo.cs
- XsltConvert.cs
- CodeParameterDeclarationExpression.cs
- ServiceChannel.cs
- TimeSpanSecondsConverter.cs
- AutomationInteropProvider.cs
- DefaultSection.cs
- SelectionRange.cs
- DrawingContextWalker.cs
- GreenMethods.cs
- WebPartDescription.cs
- Soap12ServerProtocol.cs
- AuthorizationRule.cs
- TextPointerBase.cs
- HtmlElementErrorEventArgs.cs
- MergablePropertyAttribute.cs
- CategoryNameCollection.cs
- LiteralControl.cs
- CollectionExtensions.cs
- CachedFontFace.cs
- DateTimeStorage.cs
- ThrowOnMultipleAssignment.cs
- FlowDocumentPage.cs
- IPAddressCollection.cs
- ComboBox.cs
- UrlMappingsModule.cs
- RectAnimationUsingKeyFrames.cs
- DataGridComboBoxColumn.cs
- X509SecurityTokenParameters.cs
- CharacterString.cs
- DecoderReplacementFallback.cs
- CTreeGenerator.cs
- SafeNativeMethods.cs
- ErrorFormatter.cs
- RelationalExpressions.cs
- InternalCache.cs
- FileSecurity.cs
- SamlAttribute.cs
- DesignerTextViewAdapter.cs
- TextBlock.cs
- GridViewColumnHeader.cs
- InstanceKeyCollisionException.cs
- ControlBuilderAttribute.cs
- WebHttpEndpoint.cs
- WebPartCollection.cs
- TimeZone.cs
- PointF.cs
- SqlCacheDependencyDatabaseCollection.cs
- EndOfStreamException.cs
- ReturnEventArgs.cs
- RSAOAEPKeyExchangeDeformatter.cs
- ChtmlPhoneCallAdapter.cs
- XmlProcessingInstruction.cs
- EntityDesignPluralizationHandler.cs
- AsyncResult.cs
- UnmanagedMemoryStream.cs
- ControlCollection.cs
- SkipQueryOptionExpression.cs
- ToolboxItemLoader.cs
- Enumerable.cs
- DefaultPrintController.cs
- ParameterModifier.cs
- SoapFormatter.cs
- TakeQueryOptionExpression.cs
- GotoExpression.cs
- CodeCatchClauseCollection.cs
- x509store.cs
- RelationHandler.cs
- WindowsGraphics.cs
- TextViewSelectionProcessor.cs
- RequestTimeoutManager.cs
- ZoneIdentityPermission.cs
- CompressStream.cs
- Peer.cs
- XmlSchemaValidator.cs
- ComplusTypeValidator.cs
- Axis.cs
- DependencyPropertyChangedEventArgs.cs
- AttributeQuery.cs
- FontFamily.cs
- PathGeometry.cs
- URI.cs
- GrammarBuilderBase.cs
- GeometryCollection.cs
- DataGridViewCellMouseEventArgs.cs
- OLEDB_Enum.cs
- TagNameToTypeMapper.cs
- Inflater.cs
- BindingList.cs
- CodeGroup.cs
- XmlSerializerNamespaces.cs
- ConfigurationStrings.cs
- EditorZoneAutoFormat.cs
- SctClaimDictionary.cs
- MonthCalendar.cs