Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / Microsoft / Win32 / SafeHandles / BCryptSafeHandles.cs / 1305376 / BCryptSafeHandles.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
using System;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Diagnostics.Contracts;
namespace Microsoft.Win32.SafeHandles {
///
/// SafeHandle representing a BCRYPT_ALG_HANDLE
///
//
//
//
#pragma warning disable 618 // Have not migrated to v4 transparency yet
[System.Security.SecurityCritical(System.Security.SecurityCriticalScope.Everything)]
#pragma warning restore 618
internal sealed class SafeBCryptAlgorithmHandle : SafeHandleZeroOrMinusOneIsInvalid {
private SafeBCryptAlgorithmHandle() : base(true) {
}
[DllImport("bcrypt")]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SuppressUnmanagedCodeSecurity]
private static extern BCryptNative.ErrorCode BCryptCloseAlgorithmProvider(IntPtr hAlgorithm, int flags);
protected override bool ReleaseHandle() {
return BCryptCloseAlgorithmProvider(handle, 0) == BCryptNative.ErrorCode.Success;
}
}
///
/// Safe handle representing a BCRYPT_HASH_HANDLE and the associated buffer holding the hash object
///
//
//
//
#pragma warning disable 618 // Have not migrated to v4 transparency yet
[System.Security.SecurityCritical(System.Security.SecurityCriticalScope.Everything)]
#pragma warning restore 618
internal sealed class SafeBCryptHashHandle : SafeHandleZeroOrMinusOneIsInvalid {
private IntPtr m_hashObject;
private SafeBCryptHashHandle() : base(true) {
}
///
/// Buffer holding the hash object. This buffer should be allocated with Marshal.AllocCoTaskMem.
///
internal IntPtr HashObject {
get { return m_hashObject; }
set {
Contract.Requires(value != IntPtr.Zero);
m_hashObject = value;
}
}
[DllImport("bcrypt")]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SuppressUnmanagedCodeSecurity]
private static extern BCryptNative.ErrorCode BCryptDestroyHash(IntPtr hHash);
protected override bool ReleaseHandle() {
bool success = BCryptDestroyHash(handle) == BCryptNative.ErrorCode.Success;
// The hash object buffer must be released only after destroying the hash handle
if (m_hashObject != IntPtr.Zero) {
Marshal.FreeCoTaskMem(m_hashObject);
}
return success;
}
}
}
// 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
- NativeMethods.cs
- VoiceSynthesis.cs
- FunctionMappingTranslator.cs
- CompiledQueryCacheKey.cs
- DataBinding.cs
- DoubleLinkListEnumerator.cs
- DesignTimeParseData.cs
- CodeSubDirectoriesCollection.cs
- VirtualDirectoryMappingCollection.cs
- ZoneButton.cs
- DataGridHeaderBorder.cs
- DefaultClaimSet.cs
- DataBindingList.cs
- CompatibleComparer.cs
- PropertyItemInternal.cs
- BaseParagraph.cs
- SystemColors.cs
- SchemaRegistration.cs
- TableCell.cs
- ToolStripDesignerAvailabilityAttribute.cs
- ObjectRef.cs
- SizeAnimationClockResource.cs
- handlecollector.cs
- GlobalEventManager.cs
- TextDecorationUnitValidation.cs
- WebPartDescriptionCollection.cs
- SourceElementsCollection.cs
- SQLDecimal.cs
- IDQuery.cs
- MediaContext.cs
- HttpInputStream.cs
- SiteMapNodeCollection.cs
- HtmlInputImage.cs
- HtmlInputControl.cs
- ContractMapping.cs
- RuntimeIdentifierPropertyAttribute.cs
- RichTextBoxAutomationPeer.cs
- QueryPageSettingsEventArgs.cs
- VBIdentifierTrimConverter.cs
- ColorPalette.cs
- JournalEntry.cs
- Walker.cs
- SchemaType.cs
- DataSourceXmlTextReader.cs
- FormConverter.cs
- DotExpr.cs
- IdentifierService.cs
- DelegateBodyWriter.cs
- DbInsertCommandTree.cs
- WebContext.cs
- InheritedPropertyChangedEventArgs.cs
- Size.cs
- PrintEvent.cs
- Wizard.cs
- AspNetSynchronizationContext.cs
- TextShapeableCharacters.cs
- TabPage.cs
- StorageInfo.cs
- UserInitiatedRoutedEventPermission.cs
- PropertyMapper.cs
- TemplateBindingExpression.cs
- Block.cs
- EndGetFileNameFromUserRequest.cs
- OleStrCAMarshaler.cs
- HMACSHA256.cs
- UniqueIdentifierService.cs
- StyleBamlRecordReader.cs
- XmlElement.cs
- SamlAdvice.cs
- ToolStripButton.cs
- DataListCommandEventArgs.cs
- DataGridState.cs
- MetadataItem.cs
- TransactionContextValidator.cs
- WebPartEventArgs.cs
- SectionXmlInfo.cs
- KeyBinding.cs
- SafeHandles.cs
- Point4DConverter.cs
- VectorAnimationBase.cs
- XamlSerializerUtil.cs
- ButtonBase.cs
- ParameterCollection.cs
- TranslateTransform.cs
- UmAlQuraCalendar.cs
- TextSelection.cs
- MouseDevice.cs
- TagElement.cs
- AppearanceEditorPart.cs
- ErrorActivity.cs
- MarginsConverter.cs
- WindowsEditBox.cs
- XmlSerializerAssemblyAttribute.cs
- LiteralLink.cs
- ViewSimplifier.cs
- HtmlEncodedRawTextWriter.cs
- DataContractSerializer.cs
- PathStreamGeometryContext.cs
- MexNamedPipeBindingCollectionElement.cs
- FamilyCollection.cs