Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / IdentityModel / System / IdentityModel / Psha1DerivedKeyGenerator.cs / 1305376 / Psha1DerivedKeyGenerator.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.IdentityModel { using System; using System.IO; using System.Security.Cryptography; using System.Text; using System.ServiceModel.Diagnostics; sealed class Psha1DerivedKeyGenerator { byte[] key; public Psha1DerivedKeyGenerator(byte[] key) { if (key == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("key"); } this.key = key; } public byte[] GenerateDerivedKey(byte[] label, byte[] nonce, int derivedKeySize, int position) { if (label == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("label"); } if (nonce == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("nonce"); } ManagedPsha1 dkcp = new ManagedPsha1(key, label, nonce); return dkcp.GetDerivedKey(derivedKeySize, position); } // private class to do the real work // Note: Though named ManagedPsha1, this works for both fips and non-fips compliance sealed class ManagedPsha1 { byte[] aValue; byte[] buffer; byte[] chunk; KeyedHashAlgorithm hmac; int index; int position; byte[] secret; byte[] seed; // assume arguments are already validated public ManagedPsha1(byte[] secret, byte[] label, byte[] seed) { this.secret = secret; this.seed = DiagnosticUtility.Utility.AllocateByteArray(checked(label.Length + seed.Length)); label.CopyTo(this.seed, 0); seed.CopyTo(this.seed, label.Length); this.aValue = this.seed; this.chunk = new byte[0]; this.index = 0; this.position = 0; this.hmac = CryptoHelper.NewHmacSha1KeyedHashAlgorithm(secret); this.buffer = DiagnosticUtility.Utility.AllocateByteArray(checked(this.hmac.HashSize / 8 + this.seed.Length)); } public byte[] GetDerivedKey(int derivedKeySize, int position) { if (derivedKeySize < 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("derivedKeySize", SR.GetString(SR.ValueMustBeNonNegative))); } if (this.position > position) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("position", SR.GetString(SR.ValueMustBeInRange, 0, this.position))); } // Seek to the desired position in the pseudo-random stream. while (this.position < position) { GetByte(); } int sizeInBytes = derivedKeySize / 8; byte[] derivedKey = new byte[sizeInBytes]; for (int i = 0; i < sizeInBytes; i++) { derivedKey[i] = GetByte(); } return derivedKey; } byte GetByte() { if (index >= chunk.Length) { // Calculate A(i) = HMAC_SHA1(secret, A(i-1)). hmac.Initialize(); this.aValue = hmac.ComputeHash(this.aValue); // Calculate P_SHA1(secret, seed)[j] = HMAC_SHA1(secret, A(j+1) || seed). this.aValue.CopyTo(buffer, 0); this.seed.CopyTo(buffer, this.aValue.Length); hmac.Initialize(); this.chunk = hmac.ComputeHash(buffer); index = 0; } position++; return chunk[index++]; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.IdentityModel { using System; using System.IO; using System.Security.Cryptography; using System.Text; using System.ServiceModel.Diagnostics; sealed class Psha1DerivedKeyGenerator { byte[] key; public Psha1DerivedKeyGenerator(byte[] key) { if (key == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("key"); } this.key = key; } public byte[] GenerateDerivedKey(byte[] label, byte[] nonce, int derivedKeySize, int position) { if (label == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("label"); } if (nonce == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("nonce"); } ManagedPsha1 dkcp = new ManagedPsha1(key, label, nonce); return dkcp.GetDerivedKey(derivedKeySize, position); } // private class to do the real work // Note: Though named ManagedPsha1, this works for both fips and non-fips compliance sealed class ManagedPsha1 { byte[] aValue; byte[] buffer; byte[] chunk; KeyedHashAlgorithm hmac; int index; int position; byte[] secret; byte[] seed; // assume arguments are already validated public ManagedPsha1(byte[] secret, byte[] label, byte[] seed) { this.secret = secret; this.seed = DiagnosticUtility.Utility.AllocateByteArray(checked(label.Length + seed.Length)); label.CopyTo(this.seed, 0); seed.CopyTo(this.seed, label.Length); this.aValue = this.seed; this.chunk = new byte[0]; this.index = 0; this.position = 0; this.hmac = CryptoHelper.NewHmacSha1KeyedHashAlgorithm(secret); this.buffer = DiagnosticUtility.Utility.AllocateByteArray(checked(this.hmac.HashSize / 8 + this.seed.Length)); } public byte[] GetDerivedKey(int derivedKeySize, int position) { if (derivedKeySize < 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("derivedKeySize", SR.GetString(SR.ValueMustBeNonNegative))); } if (this.position > position) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("position", SR.GetString(SR.ValueMustBeInRange, 0, this.position))); } // Seek to the desired position in the pseudo-random stream. while (this.position < position) { GetByte(); } int sizeInBytes = derivedKeySize / 8; byte[] derivedKey = new byte[sizeInBytes]; for (int i = 0; i < sizeInBytes; i++) { derivedKey[i] = GetByte(); } return derivedKey; } byte GetByte() { if (index >= chunk.Length) { // Calculate A(i) = HMAC_SHA1(secret, A(i-1)). hmac.Initialize(); this.aValue = hmac.ComputeHash(this.aValue); // Calculate P_SHA1(secret, seed)[j] = HMAC_SHA1(secret, A(j+1) || seed). this.aValue.CopyTo(buffer, 0); this.seed.CopyTo(buffer, this.aValue.Length); hmac.Initialize(); this.chunk = hmac.ComputeHash(buffer); index = 0; } position++; return chunk[index++]; } } } } // 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
- TreeViewItem.cs
- CacheForPrimitiveTypes.cs
- DataGridCell.cs
- DataGridColumnCollection.cs
- DataGridColumn.cs
- SystemResourceHost.cs
- CompensatableTransactionScopeActivity.cs
- HTTPNotFoundHandler.cs
- ValueOfAction.cs
- ServiceObjectContainer.cs
- WebPartChrome.cs
- File.cs
- mediapermission.cs
- SafeNativeMethodsMilCoreApi.cs
- XmlSchemaAll.cs
- ExtendedProperty.cs
- RowUpdatingEventArgs.cs
- ToolStripContentPanelDesigner.cs
- ResourceDictionary.cs
- Stylus.cs
- WindowsStartMenu.cs
- SqlProfileProvider.cs
- WebPartCatalogAddVerb.cs
- StoragePropertyMapping.cs
- WindowsPen.cs
- XmlRawWriter.cs
- GACMembershipCondition.cs
- HGlobalSafeHandle.cs
- OrderedDictionary.cs
- HandledEventArgs.cs
- WebPartDisplayMode.cs
- PropertyCondition.cs
- TextSelectionHighlightLayer.cs
- ReadOnlyHierarchicalDataSource.cs
- PixelFormats.cs
- DrawToolTipEventArgs.cs
- FrameworkContentElement.cs
- FontCacheLogic.cs
- FormViewModeEventArgs.cs
- EdmRelationshipRoleAttribute.cs
- SafeSystemMetrics.cs
- IISMapPath.cs
- ParameterCollectionEditorForm.cs
- SkipStoryboardToFill.cs
- ReaderWriterLock.cs
- ChangePasswordAutoFormat.cs
- ProviderCommandInfoUtils.cs
- CheckBoxRenderer.cs
- ResourceReferenceExpression.cs
- AnonymousIdentificationSection.cs
- SamlAdvice.cs
- DayRenderEvent.cs
- Section.cs
- WindowCollection.cs
- PlatformCulture.cs
- __Error.cs
- ToolStripSplitStackLayout.cs
- ItemContainerProviderWrapper.cs
- QueryableDataSourceHelper.cs
- ParsedAttributeCollection.cs
- CapabilitiesState.cs
- RequestSecurityTokenResponse.cs
- CommonDialog.cs
- DragAssistanceManager.cs
- SQLChars.cs
- OleServicesContext.cs
- SchemaNames.cs
- BinaryNode.cs
- JsonXmlDataContract.cs
- SqlProcedureAttribute.cs
- WeakReferenceKey.cs
- SafeCryptoHandles.cs
- COM2ExtendedBrowsingHandler.cs
- MarkupCompiler.cs
- TabOrder.cs
- DateTimeFormatInfoScanner.cs
- SafeLibraryHandle.cs
- PersonalizationStateInfo.cs
- CmsUtils.cs
- SchemaMerger.cs
- ReturnType.cs
- StatusBarDrawItemEvent.cs
- GridViewHeaderRowPresenter.cs
- WebPartChrome.cs
- BufferBuilder.cs
- Rotation3DAnimationBase.cs
- SQLCharsStorage.cs
- RepeaterItemEventArgs.cs
- Queue.cs
- SplitterDesigner.cs
- BaseCollection.cs
- WorkflowOperationBehavior.cs
- ScriptIgnoreAttribute.cs
- ColumnTypeConverter.cs
- TableCellsCollectionEditor.cs
- Italic.cs
- DnsEndpointIdentity.cs
- WebPartCloseVerb.cs
- ApplicationManager.cs
- BooleanFunctions.cs