本文整理汇总了C#中Crc32.ComputeHash方法的典型用法代码示例。如果您正苦于以下问题:C# Crc32.ComputeHash方法的具体用法?C# Crc32.ComputeHash怎么用?C# Crc32.ComputeHash使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crc32
的用法示例。
在下文中一共展示了Crc32.ComputeHash方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CheckPacketDataIntegrity
private static bool CheckPacketDataIntegrity(byte[] data)
{
if (data == null || data.Length < ChecksumWidth + PacketIdFieldWidth + ContentLengthFieldWidth)
return false;
var checksum = new byte[ChecksumWidth];
for (int i = 0; i < checksum.Length; i++)
checksum[i] = data[i];
byte[] idLengthContent = data.Skip(ChecksumWidth).ToArray();
byte[] computedHash;
using (var provider = new Crc32())
computedHash = provider.ComputeHash(idLengthContent, 0, idLengthContent.Length);
for (int i = 0; i < checksum.Length; i++)
if(checksum[i] != computedHash[i])
return false;
int contentLength = BitConverter.ToInt32(data, ChecksumWidth + PacketIdFieldWidth);
return contentLength + PacketHeaderSize == data.Length;
}
示例2: Calculate
public static uint Calculate(string data)
{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
Crc32 c = new Crc32();
c.ComputeHash(encoding.GetBytes(data));
return c.CrcValue;
}
示例3: Compress
public static byte[] Compress( byte[] buffer )
{
using ( MemoryStream ms = new MemoryStream() )
using ( BinaryWriter writer = new BinaryWriter( ms ) )
{
uint checkSum = 0;
using ( var crc = new Crc32() )
{
checkSum = BitConverter.ToUInt32( crc.ComputeHash( buffer ), 0 );
}
byte[] compressed = DeflateBuffer( buffer );
Int32 poslocal = WriteHeader( writer, LocalFileHeader );
WriteLocalFile( writer, "z", checkSum, ( UInt32 )buffer.Length, compressed );
Int32 posCDR = WriteHeader( writer, CentralDirectoryHeader );
UInt32 CDRSize = WriteCentralDirectory( writer, "z", checkSum, ( UInt32 )compressed.Length, ( UInt32 )buffer.Length, poslocal );
Int32 posEOD = WriteHeader( writer, EndOfDirectoryHeader );
WriteEndOfDirectory( writer, 1, CDRSize, posCDR );
return ms.ToArray();
}
}
示例4: CheckSum
public static uint CheckSum(Stream s)
{
var crc32 = new Crc32();
crc32.ComputeHash(s);
return crc32.CrcValue;
}
示例5: checksum_file
static int checksum_file(string file, ref byte[] p, ref uint size, ref uint crc)
{
int length;
byte[] data;
Stream f;
f = fopen(file, "rb");
if (f == null)
return -1;
length = (int)f.Length;
/* allocate space for entire file */
data = new byte[length];
/* read entire file into memory */
f.Read(data, 0, length);
size = (uint)length;
//crc = crc32(0L, data, length);
Crc32 crc32 = new Crc32();
string hash = "";
foreach (byte b in crc32.ComputeHash(data)) hash += b.ToString("x2").ToLower();
crc = Convert.ToUInt32(hash,16);
if (p != null)
p = data;
else
data = null;
fclose(f);
return 0;
}
示例6: CreateData
public static byte[] CreateData(IPacket packet)
{
if (packet == null)
throw new ArgumentNullException("packet");
var content = packet.GetContent();
var idLengthContent = new byte[content.Length + PacketIdFieldWidth + ContentLengthFieldWidth];
idLengthContent[0] = (byte)packet.Id;
var contentLength = BitConverter.GetBytes(content.Length);
idLengthContent[1] = contentLength[0]; // Not in the mood for a for loop
idLengthContent[2] = contentLength[1];
idLengthContent[3] = contentLength[2];
idLengthContent[4] = contentLength[3];
for (int i = (PacketIdFieldWidth + ContentLengthFieldWidth); i < idLengthContent.Length; ++i)
idLengthContent[i] = content[i - (PacketIdFieldWidth + ContentLengthFieldWidth)];
byte[] checksum;
using (var provider = new Crc32())
checksum = provider.ComputeHash(idLengthContent, 0, idLengthContent.Length);
using (var ms = new MemoryStream())
{
ms.Write(checksum, 0, ChecksumWidth);
ms.Write(idLengthContent, 0, idLengthContent.Length);
return ms.ToArray();
}
}
示例7: CalcSecureHash
public static int CalcSecureHash(string text)
{
Crc32 crc32 = new Crc32();
String hash = String.Empty;
byte[] bytes = System.Text.ASCIIEncoding.ASCII.GetBytes(text);
byte[] data = crc32.ComputeHash(bytes);
int res = data[0] + (data[1] * 256) + (data[2] * 65536) + ( data[3] * 16777216);
return res;
}
示例8: GetIndex
public int GetIndex(string key)
{
using (var crc32 = new Crc32())
{
var keyBytes = Encoding.UTF8.GetBytes(key);
var hashedKeyBytes = crc32.ComputeHash(keyBytes);
var hash = BitConverter.ToUInt32(hashedKeyBytes, 0);
return (int)hash & _mask;
}
}
示例9: getChecksum
public long getChecksum() {
byte[] bytes = new byte[32];
Crc32 checksum=new Crc32();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.BinaryWriter bytebuffer = new System.IO.BinaryWriter(ms);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
bytebuffer.Write(getPixel(x, y));
bytes=checksum.ComputeHash(ms);
}
}
return BitConverter.ToInt64(bytes,0);
}
示例10: CalculateCRC
/// <summary>
/// Calculate file CRC
/// </summary>
/// <param name="filePath">The complete file path</param>
/// <returns>File CRC</returns>
public static string CalculateCRC(string filePath)
{
if (File.Exists(filePath))
{
Stream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
byte[] fileBuffer = new byte[fileStream.Length];
fileStream.Read(fileBuffer, 0, (int)fileStream.Length);
fileStream.Close();
string crc = "";
Crc32 crc32 = new Crc32();
byte[] crc32Buffer = crc32.ComputeHash(fileBuffer);
foreach (byte b in crc32Buffer)
crc += b.ToString("x2").ToLower();
return crc;
}
return "";
}
示例11: GetHash
public static string GetHash(string filename)
{
Crc32 crc32 = new Crc32();
String hash = String.Empty;
try
{
if (!File.Exists(filename))
throw new IOException("Unknown File");
using (FileStream fs = File.Open(filename, FileMode.Open))
foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower();
}
catch (Exception ex)
{
return ex.ToString();
}
return hash;
}
示例12: GetCRC32
}//ComputeMD5
/// <summary>
/// 计算指定文件的CRC32值
/// </summary>
/// <param name="fileName">指定文件的完全限定名称</param>
/// <returns>返回值的字符串形式</returns>
public static String GetCRC32(String fileName)
{
String hashCRC32 = String.Empty;
//检查文件是否存在,如果文件存在则进行计算,否则返回空值
if (File.Exists(fileName))
{
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
//计算文件的CSC32值
Crc32 calculator = new Crc32();
Byte[] buffer = calculator.ComputeHash(fs);
calculator.Clear();
//将字节数组转换成十六进制的字符串形式
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < buffer.Length; i++)
{
stringBuilder.Append(buffer[i].ToString("x2"));
}
hashCRC32 = stringBuilder.ToString();
}//关闭文件流
}
return hashCRC32;
}//ComputeCRC32
示例13: Verify
/// <summary>
/// Computes the hash for the specified stream and compares
/// it to the value in this object. CRC hashes are not supported
/// because there is no built-in support in the .net framework and
/// a CRC implementation exceeds the scope of this project. If you
/// attempt to Verify() a CRC hash a NotImplemented() exception will
/// be thrown.
/// </summary>
/// <param name="istream">The stream to compute the hash for</param>
/// <returns>True if the computed hash matches what's stored in this object.</returns>
public bool Verify(Stream istream) {
if (IsValid) {
HashAlgorithm hashAlg = null;
switch (m_algorithm) {
case FtpHashAlgorithm.SHA1:
hashAlg = new SHA1CryptoServiceProvider();
break;
#if !NET2
case FtpHashAlgorithm.SHA256:
hashAlg = new SHA256CryptoServiceProvider();
break;
case FtpHashAlgorithm.SHA512:
hashAlg = new SHA512CryptoServiceProvider();
break;
#endif
case FtpHashAlgorithm.MD5:
hashAlg = new MD5CryptoServiceProvider();
break;
case FtpHashAlgorithm.CRC:
hashAlg = new Crc32();
break;
//throw new NotImplementedException("There is no built in support for computing CRC hashes.");
default:
throw new NotImplementedException("Unknown hash algorithm: " + m_algorithm.ToString());
}
try {
byte[] data = null;
string hash = "";
data = hashAlg.ComputeHash(istream);
if (data != null) {
foreach (byte b in data) {
hash += b.ToString("x2");
}
return (hash.ToUpper() == m_value.ToUpper());
}
}
finally {
#if !NET2 // .NET 2.0 doesn't provide access to Dispose() for HashAlgorithm
if (hashAlg != null)
hashAlg.Dispose();
#endif
}
}
return false;
}
示例14: GetColoredIdentityName
protected virtual TextMessagePartModel GetColoredIdentityName(
string idendityName, string normalized)
{
var name = new TextMessagePartModel(idendityName);
if (normalized == null) {
normalized = idendityName;
}
var crc = new Crc32();
crc.ComputeHash(Encoding.UTF8.GetBytes(normalized));
var hash = crc.CrcValue;
var upper24 = hash >> 8;
/*
var lower24 = hash & 0xFFFFFFU;
var merged = upper24 ^ lower24;
var rotated = (hash >> 16) | ((hash & 0xFFFFU) << 16);
*/
uint flippedHash = (hash >> 16) | (hash << 16);
var flippedMergedHash = (flippedHash >> 8) ^ (flippedHash & 0xFFFFFFU);
name.ForegroundColor = new TextColor(upper24);
name.BackgroundColor = new TextColor(flippedMergedHash);
/*
MD5CryptoServiceProvider csp = new MD5CryptoServiceProvider();
var md5hash = csp.ComputeHash(Encoding.UTF8.GetBytes(normalized));
var fgHash = BitConverter.ToUInt32(md5hash, 0);
var bgHash = BitConverter.ToUInt32(md5hash, 4);
name.ForegroundColor = new TextColor(fgHash >> 8);
name.BackgroundColor = new TextColor(bgHash >> 8);
*/
return name;
}
示例15: CRCHash
/// <summary>
/// Performs CRC32 on an input byte array using the CrcStandard.Crc32Bit parameters
/// </summary>
public static byte[] CRCHash( byte[] input )
{
using ( var crc = new Crc32() )
{
byte[] hash = crc.ComputeHash( input );
Array.Reverse( hash );
return hash;
}
}