Hola, como puedo saber el CRC32 o MD5 desde un mismo ejecutable con c#
es para proteger una aplicacion, es decir al arracar mi programa que se
autoverifique el CRC32 o MD5
aqui dejo las funciones CRC32 y MD5
public static string GetMD5HashFromFile(string fileName)
{
FileStream file = new FileStream(fileName, FileMode.Open);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{ sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
public static string GetCrc32( string filePath)
{
int myTimer = Environment.TickCount;
FileStream myStream = new FileStream( filePath , FileMode.Open ,
FileAccess.Read );
long fileSize = myStream.Length;
long[] LookUpTable = new long[ 256 ];
long poly = 0xEDB88320;
long crc32 = 0;
long val;
for( int i = 0 ; i <= 0xFF ; ++i)
{
val = i;
for( int j = 8 ; j > 0 ; --j )
{
if( ( val & 1 ) == 1 )
{
val = ( val >> 1 ) ^ poly ;
}
else
val >>= 1 ;
}
LookUpTable[ i ] = val;
}
byte[] byteArray = new byte[ 4096 ];
int offset = 0 ;
crc32 = 0xFFFFFFFF;
while( offset != fileSize )
{
long dif = fileSize - offset;
if( dif >= 4096 )
{
myStream.Read( byteArray , 0 , 4096 );
offset += 4096;
}
else
{
byteArray = new byte[ dif ];
myStream.Read( byteArray , 0 , byteArray.Length );
offset += byteArray.Length;
}
myStream.Seek( offset , SeekOrigin.Begin );
for( int y = 0 ; y < byteArray.Length ; ++y)
{
crc32 = ( crc32 >> 8 ) ^ LookUpTable[ ( crc32 & 0xFF ) ^ byteArray[ y ] ];
}
}
myStream.Close();
int mySecondTimer = Environment.TickCount;
int ms = mySecondTimer - myTimer;
//MessageBox.Show( "CRC32 "+ ms.ToString() + " Millisec.","Info");
long nret = crc32 ^ 0xFFFFFFFF;
return nret.ToString("X");
}
Gracias
Agustin
Leer las respuestas