Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / System / Security / Cryptography / CngProperty.cs / 1305376 / CngProperty.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
using System;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
namespace System.Security.Cryptography {
///
/// Wrapper represeting an arbitrary property of a CNG key or provider
///
[System.Security.Permissions.HostProtection(MayLeakOnAbort = true)]
public struct CngProperty : IEquatable {
private string m_name;
private CngPropertyOptions m_propertyOptions;
private byte[] m_value;
private int? m_hashCode;
public CngProperty(string name, byte[] value, CngPropertyOptions options) {
if (name == null)
throw new ArgumentNullException("name");
// @
m_name = name;
m_propertyOptions = options;
m_hashCode = null;
if (value != null) {
m_value = value.Clone() as byte[];
}
else {
m_value = null;
}
}
///
/// Name of the property
///
public string Name {
get {
Contract.Ensures(Contract.Result() != null);
return m_name;
}
}
///
/// Options used to set / get the property
///
public CngPropertyOptions Options {
get { return m_propertyOptions; }
}
///
/// Direct value of the property -- if the value will be returned to user code or modified, use
/// GetValue() instead.
///
internal byte[] Value {
get { return m_value; }
}
///
/// Contents of the property
///
///
public byte[] GetValue() {
byte[] value = null;
if (m_value != null) {
value = m_value.Clone() as byte[];
}
return value;
}
public static bool operator ==(CngProperty left, CngProperty right) {
return left.Equals(right);
}
public static bool operator !=(CngProperty left, CngProperty right) {
return !left.Equals(right);
}
public override bool Equals(object obj) {
if (obj == null || !(obj is CngProperty)) {
return false;
}
return Equals((CngProperty)obj);
}
public bool Equals(CngProperty other) {
//
// We will consider CNG properties equal only if the name, options and value are all also equal
//
if (!String.Equals(Name, other.Name, StringComparison.Ordinal)) {
return false;
}
if (Options != other.Options) {
return false;
}
if (m_value == null) {
return other.m_value == null;
}
if (other.m_value == null) {
return false;
}
if (m_value.Length != other.m_value.Length) {
return false;
}
for (int i = 0; i < m_value.Length; i++) {
if (m_value[i] != other.m_value[i]) {
return false;
}
}
return true;
}
public override int GetHashCode() {
if (!m_hashCode.HasValue) {
int hashCode = Name.GetHashCode() ^ Options.GetHashCode();
// The hash code for a byte is just the value of that byte. Since this will only modify the
// lower bits of the hash code, we'll xor each byte into different sections of the hash code
if (m_value != null) {
for (int i = 0; i < m_value.Length; i++) {
// Shift each byte forward by one byte, so that every 4 bytes has to potential to update
// each of the calculated hash code's bytes.
int shifted = (int)(m_value[i] << ((i % 4) * 8));
hashCode ^= shifted;
}
}
m_hashCode = hashCode;
}
return m_hashCode.Value;
}
}
///
/// Strongly typed collection of CNG properties
///
[System.Security.Permissions.HostProtection(MayLeakOnAbort = true)]
public sealed class CngPropertyCollection : Collection {
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
using System;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
namespace System.Security.Cryptography {
///
/// Wrapper represeting an arbitrary property of a CNG key or provider
///
[System.Security.Permissions.HostProtection(MayLeakOnAbort = true)]
public struct CngProperty : IEquatable {
private string m_name;
private CngPropertyOptions m_propertyOptions;
private byte[] m_value;
private int? m_hashCode;
public CngProperty(string name, byte[] value, CngPropertyOptions options) {
if (name == null)
throw new ArgumentNullException("name");
// @
m_name = name;
m_propertyOptions = options;
m_hashCode = null;
if (value != null) {
m_value = value.Clone() as byte[];
}
else {
m_value = null;
}
}
///
/// Name of the property
///
public string Name {
get {
Contract.Ensures(Contract.Result() != null);
return m_name;
}
}
///
/// Options used to set / get the property
///
public CngPropertyOptions Options {
get { return m_propertyOptions; }
}
///
/// Direct value of the property -- if the value will be returned to user code or modified, use
/// GetValue() instead.
///
internal byte[] Value {
get { return m_value; }
}
///
/// Contents of the property
///
///
public byte[] GetValue() {
byte[] value = null;
if (m_value != null) {
value = m_value.Clone() as byte[];
}
return value;
}
public static bool operator ==(CngProperty left, CngProperty right) {
return left.Equals(right);
}
public static bool operator !=(CngProperty left, CngProperty right) {
return !left.Equals(right);
}
public override bool Equals(object obj) {
if (obj == null || !(obj is CngProperty)) {
return false;
}
return Equals((CngProperty)obj);
}
public bool Equals(CngProperty other) {
//
// We will consider CNG properties equal only if the name, options and value are all also equal
//
if (!String.Equals(Name, other.Name, StringComparison.Ordinal)) {
return false;
}
if (Options != other.Options) {
return false;
}
if (m_value == null) {
return other.m_value == null;
}
if (other.m_value == null) {
return false;
}
if (m_value.Length != other.m_value.Length) {
return false;
}
for (int i = 0; i < m_value.Length; i++) {
if (m_value[i] != other.m_value[i]) {
return false;
}
}
return true;
}
public override int GetHashCode() {
if (!m_hashCode.HasValue) {
int hashCode = Name.GetHashCode() ^ Options.GetHashCode();
// The hash code for a byte is just the value of that byte. Since this will only modify the
// lower bits of the hash code, we'll xor each byte into different sections of the hash code
if (m_value != null) {
for (int i = 0; i < m_value.Length; i++) {
// Shift each byte forward by one byte, so that every 4 bytes has to potential to update
// each of the calculated hash code's bytes.
int shifted = (int)(m_value[i] << ((i % 4) * 8));
hashCode ^= shifted;
}
}
m_hashCode = hashCode;
}
return m_hashCode.Value;
}
}
///
/// Strongly typed collection of CNG properties
///
[System.Security.Permissions.HostProtection(MayLeakOnAbort = true)]
public sealed class CngPropertyCollection : Collection {
}
}
// 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
- DebuggerAttributes.cs
- IProducerConsumerCollection.cs
- ExtensionFile.cs
- VoiceChangeEventArgs.cs
- PaperSize.cs
- RowToParametersTransformer.cs
- SimpleLine.cs
- HttpServerVarsCollection.cs
- RadioButton.cs
- CheckedPointers.cs
- BitmapDownload.cs
- UnionExpr.cs
- CompressEmulationStream.cs
- WorkflowDispatchContext.cs
- DbConnectionOptions.cs
- ToolTipAutomationPeer.cs
- WebConfigManager.cs
- ZipArchive.cs
- ThreadInterruptedException.cs
- SqlSelectStatement.cs
- TextWriterEngine.cs
- XmlNamespaceManager.cs
- EntityDataSourceSelectingEventArgs.cs
- MessageSecurityProtocol.cs
- JsonObjectDataContract.cs
- CollectionBase.cs
- TypeBinaryExpression.cs
- XmlQueryType.cs
- EventWaitHandle.cs
- Condition.cs
- TypedElement.cs
- CodeExpressionCollection.cs
- AlgoModule.cs
- ApplicationSecurityInfo.cs
- EmptyEnumerator.cs
- XslException.cs
- QilChoice.cs
- InheritanceService.cs
- TextPointer.cs
- SqlMetaData.cs
- CodeTypeConstructor.cs
- WebBrowserNavigatingEventHandler.cs
- TypefaceMetricsCache.cs
- TextMetrics.cs
- SrgsElementList.cs
- StreamResourceInfo.cs
- DataGridItemCollection.cs
- FormClosedEvent.cs
- WebPartConnection.cs
- EventLogPermissionEntry.cs
- WorkflowOperationContext.cs
- CodeTypeReference.cs
- WCFModelStrings.Designer.cs
- IWorkflowDebuggerService.cs
- safePerfProviderHandle.cs
- SmiRecordBuffer.cs
- ButtonBaseAdapter.cs
- SqlConnectionHelper.cs
- AncestorChangedEventArgs.cs
- PartialList.cs
- BinaryParser.cs
- ListViewGroupConverter.cs
- SQlBooleanStorage.cs
- Attributes.cs
- SiteMembershipCondition.cs
- CompletionCallbackWrapper.cs
- DataViewManager.cs
- RowParagraph.cs
- UInt64.cs
- MeasureItemEvent.cs
- DtrList.cs
- OneOfConst.cs
- Point3DCollectionValueSerializer.cs
- HandlerFactoryCache.cs
- CustomAttributeBuilder.cs
- EntityClientCacheKey.cs
- IISUnsafeMethods.cs
- TagPrefixCollection.cs
- VisualTreeUtils.cs
- EventLogPermission.cs
- Timer.cs
- Color.cs
- ApplicationDirectory.cs
- SQLBinary.cs
- MatrixConverter.cs
- SamlNameIdentifierClaimResource.cs
- externdll.cs
- ProxyWebPartManagerDesigner.cs
- Int16Converter.cs
- ExpressionWriter.cs
- SqlOuterApplyReducer.cs
- WebControl.cs
- EdmRelationshipRoleAttribute.cs
- OperatingSystemVersionCheck.cs
- DiscreteKeyFrames.cs
- TextDecoration.cs
- ColorMatrix.cs
- WhitespaceRuleLookup.cs
- RelatedView.cs
- TrackingStringDictionary.cs