1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
//
// System.Security.Cryptography HashAlgorithm Class implementation
//
// Authors:
// Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)
// Sebastien Pouliot (sebastien@ximian.com)
//
// Copyright 2001 by Matthew S. Ford.
// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
namespace System.Security.Cryptography {
#if NET_2_0
[ComVisible (true)]
#endif
public abstract class HashAlgorithm : ICryptoTransform {
#if NET_2_0
protected internal byte[] HashValue;
#else
protected byte[] HashValue;
#endif
protected int HashSizeValue;
protected int State;
private bool disposed;
protected HashAlgorithm ()
{
disposed = false;
}
public virtual bool CanTransformMultipleBlocks {
get { return true; }
}
public virtual bool CanReuseTransform {
get { return true; }
}
public void Clear ()
{
// same as System.IDisposable.Dispose() which is documented
Dispose (true);
}
public byte[] ComputeHash (byte[] input)
{
if (input == null)
throw new ArgumentNullException ("input");
return ComputeHash (input, 0, input.Length);
}
public byte[] ComputeHash (byte[] buffer, int offset, int count)
{
if (disposed)
throw new ObjectDisposedException ("HashAlgorithm");
if (buffer == null)
throw new ArgumentNullException ("buffer");
if (offset < 0)
throw new ArgumentOutOfRangeException ("offset", "< 0");
if (count < 0)
throw new ArgumentException ("count", "< 0");
// ordered to avoid possible integer overflow
if (offset > buffer.Length - count) {
throw new ArgumentException ("offset + count",
Locale.GetText ("Overflow"));
}
HashCore (buffer, offset, count);
HashValue = HashFinal ();
Initialize ();
return HashValue;
}
public byte[] ComputeHash (Stream inputStream)
{
// don't read stream unless object is ready to use
if (disposed)
throw new ObjectDisposedException ("HashAlgorithm");
byte[] buffer = new byte [4096];
int len = inputStream.Read (buffer, 0, 4096);
while (len > 0) {
HashCore (buffer, 0, len);
len = inputStream.Read (buffer, 0, 4096);
}
HashValue = HashFinal ();
Initialize ();
return HashValue;
}
public static HashAlgorithm Create ()
{
return Create ("System.Security.Cryptography.HashAlgorithm");
}
public static HashAlgorithm Create (string hashName)
{
return (HashAlgorithm) CryptoConfig.CreateFromName (hashName);
}
public virtual byte[] Hash {
get {
if (HashValue == null) {
throw new CryptographicUnexpectedOperationException (
Locale.GetText ("No hash value computed."));
}
return HashValue;
}
}
protected abstract void HashCore (byte[] rgb, int start, int size);
protected abstract byte[] HashFinal ();
public virtual int HashSize {
get { return HashSizeValue; }
}
public abstract void Initialize ();
protected virtual void Dispose (bool disposing)
{
disposed = true;
}
public virtual int InputBlockSize {
get { return 1; }
}
public virtual int OutputBlockSize {
get { return 1; }
}
void IDisposable.Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this); // Finalization is now unnecessary
}
// LAMESPEC: outputBuffer is optional in 2.0 (i.e. can be null).
// However a null outputBuffer would throw a ExecutionEngineException under 1.x
public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
if (inputBuffer == null)
throw new ArgumentNullException ("inputBuffer");
if (inputOffset < 0)
throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
if (inputCount < 0)
throw new ArgumentException ("inputCount");
// ordered to avoid possible integer overflow
if ((inputOffset < 0) || (inputOffset > inputBuffer.Length - inputCount))
throw new ArgumentException ("inputBuffer");
if (outputBuffer != null) {
if (outputOffset < 0) {
#if NET_2_0
throw new ArgumentOutOfRangeException ("outputOffset", "< 0");
#else
throw new IndexOutOfRangeException ("outputBuffer");
#endif
}
// ordered to avoid possible integer overflow
if (outputOffset > outputBuffer.Length - inputCount) {
#if NET_2_0
throw new ArgumentException ("outputOffset + inputCount",
Locale.GetText ("Overflow"));
#else
throw new IndexOutOfRangeException ("outputBuffer");
#endif
}
}
HashCore (inputBuffer, inputOffset, inputCount);
if (outputBuffer != null)
Buffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
return inputCount;
}
public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
{
if (inputBuffer == null)
throw new ArgumentNullException ("inputBuffer");
if (inputCount < 0)
throw new ArgumentException ("inputCount");
// ordered to avoid possible integer overflow
if (inputOffset > inputBuffer.Length - inputCount) {
throw new ArgumentException ("inputOffset + inputCount",
Locale.GetText ("Overflow"));
}
byte[] outputBuffer = new byte [inputCount];
// note: other exceptions are handled by Buffer.BlockCopy
Buffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, 0, inputCount);
HashCore (inputBuffer, inputOffset, inputCount);
HashValue = HashFinal ();
Initialize ();
return outputBuffer;
}
}
}
|