Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / infocard / Client / System / IdentityModel / Selectors / InfoCardKeyedHashAlgorithm.cs / 1305376 / InfoCardKeyedHashAlgorithm.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- // // Presharp uses the c# pragma mechanism to supress its warnings. // These are not recognised by the base compiler so we need to explictly // disable the following warnings. See http://winweb/cse/Tools/PREsharp/userguide/default.asp // for details. // #pragma warning disable 1634, 1691 // unknown message, unknown pragma namespace System.IdentityModel.Selectors { using System; using System.ComponentModel; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Runtime.ConstrainedExecution; using System.Runtime.CompilerServices; using IDT=Microsoft.InfoCards.Diagnostics.InfoCardTrace; using DiagnosticUtility=Microsoft.InfoCards.Diagnostics.DiagnosticUtility; // // For common & resources // using Microsoft.InfoCards; // // Summary: // Remotes a KeyedHashAlgorithm from the InfoCard service. // internal class InfoCardKeyedHashAlgorithm : KeyedHashAlgorithm { HashCryptoHandle m_cryptoHandle; RpcHashCryptoParameters m_param; byte[] m_cachedBlock; // // Summary: // Creates a new InfoCardKeyedHashAlgorithm based on a SymmetricCryptoHandle. // // Parameters: // cryptoHandle - The handle to the symmetric key on which to base the keyed hash. // public InfoCardKeyedHashAlgorithm( SymmetricCryptoHandle cryptoHandle ) { InternalRefCountedHandle nativeHandle = null; try { // // Call native api to get a hashCryptoHandle. // int status = CardSpaceSelector.GetShim().m_csShimGetKeyedHash( cryptoHandle.InternalHandle, out nativeHandle ); if ( 0 != status ) { IDT.CloseInvalidOutSafeHandle(nativeHandle); ExceptionHelper.ThrowIfCardSpaceException( status ); throw IDT.ThrowHelperError( new Win32Exception( status ) ); } m_cryptoHandle = (HashCryptoHandle) CryptoHandle.Create( nativeHandle ); m_param = (RpcHashCryptoParameters) m_cryptoHandle.Parameters; } catch { if ( null != m_cryptoHandle ) { m_cryptoHandle.Dispose(); } throw; } } #pragma warning disable 56503 // property gets should not throw. public override byte[] Key { get { throw IDT.ThrowHelperError( new NotImplementedException() ); } } public override int HashSize { get { return m_param.hashSize;} } public override int InputBlockSize { get { return m_param.transform.inputBlockSize;} } public override int OutputBlockSize { get { return m_param.transform.outputBlockSize;} } public override bool CanTransformMultipleBlocks { get { return m_param.transform.canTransformMultipleBlocks;} } public override bool CanReuseTransform { get { return m_param.transform.canReuseTransform;} } #pragma warning restore 56503 public override void Initialize() { } // // Summary: // Implements the HashCore method of the KeyedHashAlgorithm class by calling the InfoCard native client. // // Parameters: // array - the bytes to hash. // ibStart - the index in the array from which to begin hashing. // cbSize - the number of bytes after the starting index to hash. // protected override void HashCore(byte[] array, int ibStart, int cbSize) { // // will cache one block and call TransformBlock on the previous block. // if ( null != m_cachedBlock ) { HGlobalSafeHandle pInData = null; try { if ( 0 != m_cachedBlock.Length ) { pInData = HGlobalSafeHandle.Construct( m_cachedBlock.Length ); Marshal.Copy( m_cachedBlock, 0, pInData.DangerousGetHandle(), m_cachedBlock.Length ); } int status = CardSpaceSelector.GetShim().m_csShimHashCore( m_cryptoHandle.InternalHandle, m_cachedBlock.Length, null != pInData ? pInData: HGlobalSafeHandle.Construct() ); if ( 0 != status ) { ExceptionHelper.ThrowIfCardSpaceException( status ); throw IDT.ThrowHelperError( new Win32Exception( status ) ); } } finally { if( null != pInData ) { pInData.Dispose(); } } } // // Cache the current block. // if ( null != m_cachedBlock ) { Array.Clear( m_cachedBlock, 0, m_cachedBlock.Length ); } m_cachedBlock = DiagnosticUtility.Utility.AllocateByteArray(cbSize); Array.Copy(array, ibStart, m_cachedBlock, 0, cbSize); return; } // // Summary: // Implements the HashFinal method of the KeyedHashAlgorithm class by calling the InfoCard native client. // protected override byte[] HashFinal() { byte[] outData = null; int cbOutData = 0; IDT.DebugAssert( null != m_cachedBlock, "null cached block" ); HGlobalSafeHandle pInData = null; GlobalAllocSafeHandle pOutData = null; try { if( null != m_cachedBlock ) { if( 0 != m_cachedBlock.Length ) { pInData = HGlobalSafeHandle.Construct( m_cachedBlock.Length ); Marshal.Copy( m_cachedBlock, 0, pInData.DangerousGetHandle(), m_cachedBlock.Length ); } int status = CardSpaceSelector.GetShim().m_csShimHashFinal( m_cryptoHandle.InternalHandle, m_cachedBlock.Length, null != pInData ? pInData : HGlobalSafeHandle.Construct(), out cbOutData, out pOutData ); if( 0 != status ) { ExceptionHelper.ThrowIfCardSpaceException( status ); throw IDT.ThrowHelperError( new Win32Exception( status ) ); } pOutData.Length = cbOutData; outData = DiagnosticUtility.Utility.AllocateByteArray( pOutData.Length ); using( pOutData ) { Marshal.Copy( pOutData.DangerousGetHandle(), outData, 0, pOutData.Length ); } } } finally { if ( null != pInData ) { pInData.Dispose(); } Array.Clear( m_cachedBlock, 0, m_cachedBlock.Length ); m_cachedBlock = null; } return outData; } protected override void Dispose( bool disposing ) { try { if ( null != m_cachedBlock ) { Array.Clear( m_cachedBlock, 0, m_cachedBlock.Length ); } m_cryptoHandle.Dispose(); } finally { base.Dispose( disposing ); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- // // Presharp uses the c# pragma mechanism to supress its warnings. // These are not recognised by the base compiler so we need to explictly // disable the following warnings. See http://winweb/cse/Tools/PREsharp/userguide/default.asp // for details. // #pragma warning disable 1634, 1691 // unknown message, unknown pragma namespace System.IdentityModel.Selectors { using System; using System.ComponentModel; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Runtime.ConstrainedExecution; using System.Runtime.CompilerServices; using IDT=Microsoft.InfoCards.Diagnostics.InfoCardTrace; using DiagnosticUtility=Microsoft.InfoCards.Diagnostics.DiagnosticUtility; // // For common & resources // using Microsoft.InfoCards; // // Summary: // Remotes a KeyedHashAlgorithm from the InfoCard service. // internal class InfoCardKeyedHashAlgorithm : KeyedHashAlgorithm { HashCryptoHandle m_cryptoHandle; RpcHashCryptoParameters m_param; byte[] m_cachedBlock; // // Summary: // Creates a new InfoCardKeyedHashAlgorithm based on a SymmetricCryptoHandle. // // Parameters: // cryptoHandle - The handle to the symmetric key on which to base the keyed hash. // public InfoCardKeyedHashAlgorithm( SymmetricCryptoHandle cryptoHandle ) { InternalRefCountedHandle nativeHandle = null; try { // // Call native api to get a hashCryptoHandle. // int status = CardSpaceSelector.GetShim().m_csShimGetKeyedHash( cryptoHandle.InternalHandle, out nativeHandle ); if ( 0 != status ) { IDT.CloseInvalidOutSafeHandle(nativeHandle); ExceptionHelper.ThrowIfCardSpaceException( status ); throw IDT.ThrowHelperError( new Win32Exception( status ) ); } m_cryptoHandle = (HashCryptoHandle) CryptoHandle.Create( nativeHandle ); m_param = (RpcHashCryptoParameters) m_cryptoHandle.Parameters; } catch { if ( null != m_cryptoHandle ) { m_cryptoHandle.Dispose(); } throw; } } #pragma warning disable 56503 // property gets should not throw. public override byte[] Key { get { throw IDT.ThrowHelperError( new NotImplementedException() ); } } public override int HashSize { get { return m_param.hashSize;} } public override int InputBlockSize { get { return m_param.transform.inputBlockSize;} } public override int OutputBlockSize { get { return m_param.transform.outputBlockSize;} } public override bool CanTransformMultipleBlocks { get { return m_param.transform.canTransformMultipleBlocks;} } public override bool CanReuseTransform { get { return m_param.transform.canReuseTransform;} } #pragma warning restore 56503 public override void Initialize() { } // // Summary: // Implements the HashCore method of the KeyedHashAlgorithm class by calling the InfoCard native client. // // Parameters: // array - the bytes to hash. // ibStart - the index in the array from which to begin hashing. // cbSize - the number of bytes after the starting index to hash. // protected override void HashCore(byte[] array, int ibStart, int cbSize) { // // will cache one block and call TransformBlock on the previous block. // if ( null != m_cachedBlock ) { HGlobalSafeHandle pInData = null; try { if ( 0 != m_cachedBlock.Length ) { pInData = HGlobalSafeHandle.Construct( m_cachedBlock.Length ); Marshal.Copy( m_cachedBlock, 0, pInData.DangerousGetHandle(), m_cachedBlock.Length ); } int status = CardSpaceSelector.GetShim().m_csShimHashCore( m_cryptoHandle.InternalHandle, m_cachedBlock.Length, null != pInData ? pInData: HGlobalSafeHandle.Construct() ); if ( 0 != status ) { ExceptionHelper.ThrowIfCardSpaceException( status ); throw IDT.ThrowHelperError( new Win32Exception( status ) ); } } finally { if( null != pInData ) { pInData.Dispose(); } } } // // Cache the current block. // if ( null != m_cachedBlock ) { Array.Clear( m_cachedBlock, 0, m_cachedBlock.Length ); } m_cachedBlock = DiagnosticUtility.Utility.AllocateByteArray(cbSize); Array.Copy(array, ibStart, m_cachedBlock, 0, cbSize); return; } // // Summary: // Implements the HashFinal method of the KeyedHashAlgorithm class by calling the InfoCard native client. // protected override byte[] HashFinal() { byte[] outData = null; int cbOutData = 0; IDT.DebugAssert( null != m_cachedBlock, "null cached block" ); HGlobalSafeHandle pInData = null; GlobalAllocSafeHandle pOutData = null; try { if( null != m_cachedBlock ) { if( 0 != m_cachedBlock.Length ) { pInData = HGlobalSafeHandle.Construct( m_cachedBlock.Length ); Marshal.Copy( m_cachedBlock, 0, pInData.DangerousGetHandle(), m_cachedBlock.Length ); } int status = CardSpaceSelector.GetShim().m_csShimHashFinal( m_cryptoHandle.InternalHandle, m_cachedBlock.Length, null != pInData ? pInData : HGlobalSafeHandle.Construct(), out cbOutData, out pOutData ); if( 0 != status ) { ExceptionHelper.ThrowIfCardSpaceException( status ); throw IDT.ThrowHelperError( new Win32Exception( status ) ); } pOutData.Length = cbOutData; outData = DiagnosticUtility.Utility.AllocateByteArray( pOutData.Length ); using( pOutData ) { Marshal.Copy( pOutData.DangerousGetHandle(), outData, 0, pOutData.Length ); } } } finally { if ( null != pInData ) { pInData.Dispose(); } Array.Clear( m_cachedBlock, 0, m_cachedBlock.Length ); m_cachedBlock = null; } return outData; } protected override void Dispose( bool disposing ) { try { if ( null != m_cachedBlock ) { Array.Clear( m_cachedBlock, 0, m_cachedBlock.Length ); } m_cryptoHandle.Dispose(); } finally { base.Dispose( disposing ); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- AnonymousIdentificationSection.cs
- QueryContinueDragEvent.cs
- __TransparentProxy.cs
- XmlBuffer.cs
- PageAsyncTaskManager.cs
- HtmlButton.cs
- XXXInfos.cs
- DataGridViewCellParsingEventArgs.cs
- GridViewCancelEditEventArgs.cs
- TextOutput.cs
- PresentationAppDomainManager.cs
- MailBnfHelper.cs
- XamlClipboardData.cs
- TreeBuilderXamlTranslator.cs
- X509IssuerSerialKeyIdentifierClause.cs
- HTMLTagNameToTypeMapper.cs
- ParseChildrenAsPropertiesAttribute.cs
- XpsImageSerializationService.cs
- ProviderUtil.cs
- SystemTcpStatistics.cs
- HtmlInputSubmit.cs
- FreezableOperations.cs
- WizardStepBase.cs
- Vector3DAnimationBase.cs
- FactoryRecord.cs
- RadioButtonAutomationPeer.cs
- CatalogZoneBase.cs
- DataBindingCollection.cs
- AssemblyUtil.cs
- RoutedUICommand.cs
- OleDbDataReader.cs
- CapiHashAlgorithm.cs
- MsdtcWrapper.cs
- ValidatingReaderNodeData.cs
- HttpHeaderCollection.cs
- KeyPullup.cs
- AstNode.cs
- RadioButton.cs
- EventToken.cs
- Inflater.cs
- PeerNameRecord.cs
- DataGridColumnHeaderItemAutomationPeer.cs
- GuidTagList.cs
- ContentPropertyAttribute.cs
- DelegateSerializationHolder.cs
- DataGridViewRowsRemovedEventArgs.cs
- PolicyStatement.cs
- EasingFunctionBase.cs
- StringCollectionEditor.cs
- DrawingVisual.cs
- SqlSelectStatement.cs
- DataTableNewRowEvent.cs
- XmlSchemaValidationException.cs
- DbParameterCollection.cs
- HeaderUtility.cs
- DesignRelationCollection.cs
- MenuCommands.cs
- BindingGroup.cs
- BaseDataBoundControl.cs
- XmlWhitespace.cs
- XmlKeywords.cs
- TextBoxBase.cs
- ETagAttribute.cs
- TextViewBase.cs
- ParserStreamGeometryContext.cs
- ExpressionNormalizer.cs
- ByteBufferPool.cs
- AttributeEmitter.cs
- RelationshipEnd.cs
- SBCSCodePageEncoding.cs
- UnicodeEncoding.cs
- MULTI_QI.cs
- FullTextBreakpoint.cs
- AnnotationAdorner.cs
- RectAnimationBase.cs
- ProtocolsConfiguration.cs
- XmlException.cs
- BrowserCapabilitiesFactoryBase.cs
- PropertyTab.cs
- ConnectionPoint.cs
- Solver.cs
- JsonReader.cs
- GeometryDrawing.cs
- BrowserCapabilitiesFactory.cs
- ValidatingPropertiesEventArgs.cs
- EntityRecordInfo.cs
- ExternalFile.cs
- recordstatefactory.cs
- UriSection.cs
- ContainerFilterService.cs
- FixedTextView.cs
- WindowPatternIdentifiers.cs
- EntityFunctions.cs
- AuthenticationModulesSection.cs
- CommonBehaviorsSection.cs
- CodeDirectionExpression.cs
- CustomPopupPlacement.cs
- CurrencyManager.cs
- XmlDataSourceNodeDescriptor.cs
- DbProviderSpecificTypePropertyAttribute.cs