Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Security / Cryptography / HashAlgorithm.cs / 1305376 / HashAlgorithm.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// [....]
//
//
// HashAlgorithm.cs
//
namespace System.Security.Cryptography {
using System.IO;
using System.Diagnostics.Contracts;
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class HashAlgorithm : IDisposable, ICryptoTransform {
protected int HashSizeValue;
protected internal byte[] HashValue;
protected int State = 0;
private bool m_bDisposed = false;
protected HashAlgorithm() {}
//
// public properties
//
public virtual int HashSize {
get { return HashSizeValue; }
}
public virtual byte[] Hash {
get {
if (m_bDisposed)
throw new ObjectDisposedException(null);
if (State != 0)
throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_HashNotYetFinalized"));
return (byte[]) HashValue.Clone();
}
}
//
// public methods
//
[System.Security.SecuritySafeCritical] // auto-generated
static public HashAlgorithm Create() {
return Create("System.Security.Cryptography.HashAlgorithm");
}
[System.Security.SecuritySafeCritical] // auto-generated
static public HashAlgorithm Create(String hashName) {
return (HashAlgorithm) CryptoConfig.CreateFromName(hashName);
}
public byte[] ComputeHash(Stream inputStream) {
if (m_bDisposed)
throw new ObjectDisposedException(null);
// Default the buffer size to 4K.
byte[] buffer = new byte[4096];
int bytesRead;
do {
bytesRead = inputStream.Read(buffer, 0, 4096);
if (bytesRead > 0) {
HashCore(buffer, 0, bytesRead);
}
} while (bytesRead > 0);
HashValue = HashFinal();
byte[] Tmp = (byte[]) HashValue.Clone();
Initialize();
return(Tmp);
}
public byte[] ComputeHash(byte[] buffer) {
if (m_bDisposed)
throw new ObjectDisposedException(null);
// Do some validation
if (buffer == null) throw new ArgumentNullException("buffer");
HashCore(buffer, 0, buffer.Length);
HashValue = HashFinal();
byte[] Tmp = (byte[]) HashValue.Clone();
Initialize();
return(Tmp);
}
public byte[] ComputeHash(byte[] buffer, int offset, int count) {
// Do some validation
if (buffer == null)
throw new ArgumentNullException("buffer");
if (offset < 0)
throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (count < 0 || (count > buffer.Length))
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
if ((buffer.Length - count) < offset)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
Contract.EndContractBlock();
if (m_bDisposed)
throw new ObjectDisposedException(null);
HashCore(buffer, offset, count);
HashValue = HashFinal();
byte[] Tmp = (byte[]) HashValue.Clone();
Initialize();
return(Tmp);
}
// ICryptoTransform methods
// we assume any HashAlgorithm can take input a byte at a time
public virtual int InputBlockSize {
get { return(1); }
}
public virtual int OutputBlockSize {
get { return(1); }
}
public virtual bool CanTransformMultipleBlocks {
get { return(true); }
}
public virtual bool CanReuseTransform {
get { return(true); }
}
// We implement TransformBlock and TransformFinalBlock here
[System.Security.SecuritySafeCritical] // auto-generated
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) {
// Do some validation, we let BlockCopy do the destination array 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 (m_bDisposed)
throw new ObjectDisposedException(null);
// Change the State value
State = 1;
HashCore(inputBuffer, inputOffset, inputCount);
if ((outputBuffer != null) && ((inputBuffer != outputBuffer) || (inputOffset != outputOffset)))
Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
return inputCount;
}
[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 (m_bDisposed)
throw new ObjectDisposedException(null);
HashCore(inputBuffer, inputOffset, inputCount);
HashValue = HashFinal();
byte[] outputBytes = new byte[inputCount];
if (inputCount != 0)
Buffer.InternalBlockCopy(inputBuffer, inputOffset, outputBytes, 0, inputCount);
// reset the State value
State = 0;
return outputBytes;
}
// IDisposable methods
// To keep mscorlib compatibility with Orcas, CoreCLR's HashAlgorithm has an explicit IDisposable
// implementation. Post-Orcas the desktop has an implicit IDispoable implementation.
#if FEATURE_CORECLR
void IDisposable.Dispose()
#if false
{ }
#endif // false
#else
[System.Security.SecuritySafeCritical] // auto-generated
public void Dispose()
#endif // FEATURE_CORECLR
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Clear() {
(this as IDisposable).Dispose();
}
protected virtual void Dispose(bool disposing) {
if (disposing) {
if (HashValue != null)
Array.Clear(HashValue, 0, HashValue.Length);
HashValue = null;
m_bDisposed = true;
}
}
//
// abstract public methods
//
public abstract void Initialize();
protected abstract void HashCore(byte[] array, int ibStart, int cbSize);
protected abstract byte[] HashFinal();
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// [....]
//
//
// HashAlgorithm.cs
//
namespace System.Security.Cryptography {
using System.IO;
using System.Diagnostics.Contracts;
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class HashAlgorithm : IDisposable, ICryptoTransform {
protected int HashSizeValue;
protected internal byte[] HashValue;
protected int State = 0;
private bool m_bDisposed = false;
protected HashAlgorithm() {}
//
// public properties
//
public virtual int HashSize {
get { return HashSizeValue; }
}
public virtual byte[] Hash {
get {
if (m_bDisposed)
throw new ObjectDisposedException(null);
if (State != 0)
throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_HashNotYetFinalized"));
return (byte[]) HashValue.Clone();
}
}
//
// public methods
//
[System.Security.SecuritySafeCritical] // auto-generated
static public HashAlgorithm Create() {
return Create("System.Security.Cryptography.HashAlgorithm");
}
[System.Security.SecuritySafeCritical] // auto-generated
static public HashAlgorithm Create(String hashName) {
return (HashAlgorithm) CryptoConfig.CreateFromName(hashName);
}
public byte[] ComputeHash(Stream inputStream) {
if (m_bDisposed)
throw new ObjectDisposedException(null);
// Default the buffer size to 4K.
byte[] buffer = new byte[4096];
int bytesRead;
do {
bytesRead = inputStream.Read(buffer, 0, 4096);
if (bytesRead > 0) {
HashCore(buffer, 0, bytesRead);
}
} while (bytesRead > 0);
HashValue = HashFinal();
byte[] Tmp = (byte[]) HashValue.Clone();
Initialize();
return(Tmp);
}
public byte[] ComputeHash(byte[] buffer) {
if (m_bDisposed)
throw new ObjectDisposedException(null);
// Do some validation
if (buffer == null) throw new ArgumentNullException("buffer");
HashCore(buffer, 0, buffer.Length);
HashValue = HashFinal();
byte[] Tmp = (byte[]) HashValue.Clone();
Initialize();
return(Tmp);
}
public byte[] ComputeHash(byte[] buffer, int offset, int count) {
// Do some validation
if (buffer == null)
throw new ArgumentNullException("buffer");
if (offset < 0)
throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (count < 0 || (count > buffer.Length))
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
if ((buffer.Length - count) < offset)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
Contract.EndContractBlock();
if (m_bDisposed)
throw new ObjectDisposedException(null);
HashCore(buffer, offset, count);
HashValue = HashFinal();
byte[] Tmp = (byte[]) HashValue.Clone();
Initialize();
return(Tmp);
}
// ICryptoTransform methods
// we assume any HashAlgorithm can take input a byte at a time
public virtual int InputBlockSize {
get { return(1); }
}
public virtual int OutputBlockSize {
get { return(1); }
}
public virtual bool CanTransformMultipleBlocks {
get { return(true); }
}
public virtual bool CanReuseTransform {
get { return(true); }
}
// We implement TransformBlock and TransformFinalBlock here
[System.Security.SecuritySafeCritical] // auto-generated
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) {
// Do some validation, we let BlockCopy do the destination array 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 (m_bDisposed)
throw new ObjectDisposedException(null);
// Change the State value
State = 1;
HashCore(inputBuffer, inputOffset, inputCount);
if ((outputBuffer != null) && ((inputBuffer != outputBuffer) || (inputOffset != outputOffset)))
Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
return inputCount;
}
[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 (m_bDisposed)
throw new ObjectDisposedException(null);
HashCore(inputBuffer, inputOffset, inputCount);
HashValue = HashFinal();
byte[] outputBytes = new byte[inputCount];
if (inputCount != 0)
Buffer.InternalBlockCopy(inputBuffer, inputOffset, outputBytes, 0, inputCount);
// reset the State value
State = 0;
return outputBytes;
}
// IDisposable methods
// To keep mscorlib compatibility with Orcas, CoreCLR's HashAlgorithm has an explicit IDisposable
// implementation. Post-Orcas the desktop has an implicit IDispoable implementation.
#if FEATURE_CORECLR
void IDisposable.Dispose()
#if false
{ }
#endif // false
#else
[System.Security.SecuritySafeCritical] // auto-generated
public void Dispose()
#endif // FEATURE_CORECLR
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Clear() {
(this as IDisposable).Dispose();
}
protected virtual void Dispose(bool disposing) {
if (disposing) {
if (HashValue != null)
Array.Clear(HashValue, 0, HashValue.Length);
HashValue = null;
m_bDisposed = true;
}
}
//
// abstract public methods
//
public abstract void Initialize();
protected abstract void HashCore(byte[] array, int ibStart, int cbSize);
protected abstract byte[] HashFinal();
}
}
// 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
- HiddenFieldPageStatePersister.cs
- PathSegmentCollection.cs
- parserscommon.cs
- RuntimeArgumentHandle.cs
- OutOfMemoryException.cs
- DataBoundControlHelper.cs
- DesignerTransaction.cs
- NetSectionGroup.cs
- QilFactory.cs
- RoleManagerSection.cs
- EmptyQuery.cs
- HatchBrush.cs
- ApplicationSecurityInfo.cs
- CursorConverter.cs
- DataServiceResponse.cs
- PanelContainerDesigner.cs
- PageThemeParser.cs
- Variant.cs
- itemelement.cs
- EntityConnectionStringBuilder.cs
- PassportIdentity.cs
- LocalizationParserHooks.cs
- ControlFilterExpression.cs
- DesignerSerializationOptionsAttribute.cs
- ProvideValueServiceProvider.cs
- AsynchronousChannelMergeEnumerator.cs
- InputScope.cs
- XmlProcessingInstruction.cs
- TCEAdapterGenerator.cs
- PiiTraceSource.cs
- ContextCorrelationInitializer.cs
- TcpClientChannel.cs
- AdPostCacheSubstitution.cs
- CalendarAutoFormat.cs
- WorkflowLayouts.cs
- IndexExpression.cs
- SupportsPreviewControlAttribute.cs
- CompilerTypeWithParams.cs
- PrtTicket_Public.cs
- x509utils.cs
- CompiledRegexRunner.cs
- OletxTransactionManager.cs
- ObjectPropertyMapping.cs
- Util.cs
- InternalConfigSettingsFactory.cs
- PersonalizationState.cs
- StrokeNodeData.cs
- XPathEmptyIterator.cs
- TargetException.cs
- XamlToRtfParser.cs
- StateWorkerRequest.cs
- TextRenderer.cs
- SoapIgnoreAttribute.cs
- Ipv6Element.cs
- WebPartVerb.cs
- DataObjectCopyingEventArgs.cs
- NativeStructs.cs
- ButtonColumn.cs
- TextHintingModeValidation.cs
- Process.cs
- TransactionFlowBindingElementImporter.cs
- ResourceWriter.cs
- XmlSerializerAssemblyAttribute.cs
- TypeListConverter.cs
- InputProcessorProfiles.cs
- ControlAdapter.cs
- TraceSection.cs
- ToolStripGripRenderEventArgs.cs
- ComplexBindingPropertiesAttribute.cs
- CollectionBase.cs
- ItemCollection.cs
- FontSizeConverter.cs
- FormattedText.cs
- WindowsListViewItemCheckBox.cs
- NativeMethods.cs
- X509CertificateInitiatorServiceCredential.cs
- SizeAnimation.cs
- GridViewRow.cs
- Selection.cs
- DataGrid.cs
- ForEachAction.cs
- UnconditionalPolicy.cs
- EntityModelBuildProvider.cs
- SqlAliaser.cs
- EndpointDiscoveryMetadataCD1.cs
- DockPanel.cs
- ProviderSettings.cs
- KeySpline.cs
- FlowDocumentView.cs
- TimeZone.cs
- CopyNamespacesAction.cs
- NativeMethods.cs
- ProfessionalColorTable.cs
- basenumberconverter.cs
- EditingMode.cs
- CodeSnippetStatement.cs
- ProfileSettings.cs
- NativeMethodsCLR.cs
- SemanticBasicElement.cs
- HTMLTagNameToTypeMapper.cs