本文整理汇总了C#中Crc32类的典型用法代码示例。如果您正苦于以下问题:C# Crc32类的具体用法?C# Crc32怎么用?C# Crc32使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Crc32类属于命名空间,在下文中一共展示了Crc32类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CheckSum
public static uint CheckSum(Stream s)
{
var crc32 = new Crc32();
crc32.ComputeHash(s);
return crc32.CrcValue;
}
示例2: 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();
}
}
示例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_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;
}
示例5: zip
private void zip(string strFile, ZipOutputStream s, string staticFile)
{
if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
Crc32 crc = new Crc32();
string[] filenames = Directory.GetFileSystemEntries(strFile);
foreach (string file in filenames)
{
if (Directory.Exists(file))
{
zip(file, s, staticFile);
}
else // 否则直接压缩文件
{
//打开压缩文件
FileStream fs = File.OpenRead(file);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(tempfile);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
s.PutNextEntry(entry);
s.Write(buffer, 0, buffer.Length);
}
}
}
示例6: Hash
public string Hash(string input)
{
using (var crc = new Crc32())
{
var byteArray = crc.ComputeHash(Encoding.Unicode.GetBytes(input));
return byteArray.Aggregate("", (current, b) => current + b.ToString("x2"));
}
}
示例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: Main
static void Main(string[] args)
{
Crc32 crc32 = new Crc32();
String hash = String.Empty;
Console.Write("Enter Path: ");
string path = Console.ReadLine();
var dir = new DirectoryInfo(path);
var files = new List<string>();
var CRC = new List<string>();
foreach (FileInfo file in dir.GetFiles())
{
using (FileStream fs = File.Open(file.FullName, FileMode.Open))
foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower();
files.Add(file.FullName);
CRC.Add(file.FullName + " - " + hash);
}
foreach (var element in CRC)
{
Console.WriteLine(element);
}
path = path + "/CRC.txt";
if (File.Exists(path))
{
File.Delete(path);
}
using (FileStream file = File.Create(path))
{
Byte[] info = new UTF8Encoding(true).GetBytes("");
file.Write(info, 0, info.Length);
}
using (System.IO.StreamWriter file_dump =
new System.IO.StreamWriter(@path, true))
{
foreach (var element in CRC)
{
file_dump.WriteLine(element);
}
file_dump.WriteLine();
}
Console.WriteLine(" \t \t Dump Done");
//Комменты у меня (с) Константин Лебейко
Console.ReadKey();
}
示例10: AddStreamAsync
public async Task AddStreamAsync(string key, Stream stream)
{
var crc = new Crc32();
var useStream = stream as BufferedStream ?? new BufferedStream(stream);
byte[] buffer = new byte[0x1000];
int bytesRead;
while ((bytesRead = await useStream.ReadAsync(buffer, 0, 0x1000)) > 0)
crc.Update(buffer, 0, bytesRead);
_entries.Add(key, crc.Value);
}
示例11: Tests
public void Tests()
{
var crc = new Crc32();
Assert.AreEqual(0, crc.Value);
crc.Update(145);
Assert.AreEqual(1426738271, crc.Value);
crc.Update(123456789);
Assert.AreEqual(1147030863, crc.Value);
byte[] data = new byte[] { 145, 234, 156 };
crc.Update(data);
Assert.AreEqual(3967437022, crc.Value);
}
示例12: Main
public static int Main(string[] args)
{
if (args.Length == 0) {
ShowHelp();
return 1;
}
var parser = new ArgumentParser(args);
if (!File.Exists(file_)) {
Console.Error.WriteLine("Cannot find file {0}", file_);
ShowHelp();
return 1;
}
using (FileStream checksumStream = File.OpenRead(file_)) {
byte[] buffer = new byte[4096];
int bytesRead;
switch (parser.Command) {
case Command.Help:
ShowHelp();
break;
case Command.Crc32:
var currentCrc = new Crc32();
while ((bytesRead = checksumStream.Read(buffer, 0, buffer.Length)) > 0) {
currentCrc.Update(buffer, 0, bytesRead);
}
Console.WriteLine("CRC32 for {0} is 0x{1:X8}", args[0], currentCrc.Value);
break;
case Command.BZip2:
var currentBZip2Crc = new BZip2Crc();
while ((bytesRead = checksumStream.Read(buffer, 0, buffer.Length)) > 0) {
currentBZip2Crc.Update(buffer, 0, bytesRead);
}
Console.WriteLine("BZip2CRC32 for {0} is 0x{1:X8}", args[0], currentBZip2Crc.Value);
break;
case Command.Adler:
var currentAdler = new Adler32();
while ((bytesRead = checksumStream.Read(buffer, 0, buffer.Length)) > 0) {
currentAdler.Update(buffer, 0, bytesRead);
}
Console.WriteLine("Adler32 for {0} is 0x{1:X8}", args[0], currentAdler.Value);
break;
}
}
return 0;
}
示例13: 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);
}
示例14: WriteZipFile
public static void WriteZipFile(List<FileDetails> filesToZip, string path,string manifestPath,string manifest )
{
int compression = 9;
FileDetails fd = new FileDetails(manifest, manifestPath,manifestPath);
filesToZip.Insert(0,fd);
foreach (FileDetails obj in filesToZip)
if (!File.Exists(obj.FilePath))
throw new ArgumentException(string.Format("The File {0} does not exist!", obj.FileName));
Object _locker=new Object();
lock(_locker)
{
Crc32 crc32 = new Crc32();
ZipOutputStream stream = new ZipOutputStream(File.Create(path));
stream.SetLevel(compression);
for (int i = 0; i < filesToZip.Count; i++)
{
ZipEntry entry = new ZipEntry(filesToZip[i].FolderInfo + "/" + filesToZip[i].FileName);
entry.DateTime = DateTime.Now;
if (i == 0)
{
entry = new ZipEntry(manifest);
}
using (FileStream fs = File.OpenRead(filesToZip[i].FilePath))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
entry.Size = fs.Length;
fs.Close();
crc32.Reset();
crc32.Update(buffer);
entry.Crc = crc32.Value;
stream.PutNextEntry(entry);
stream.Write(buffer, 0, buffer.Length);
}
}
stream.Finish();
stream.Close();
DeleteManifest(manifestPath);
}
}
示例15: TestOperatorEquality
public void TestOperatorEquality()
{
Crc32 empty = new Crc32();
Crc32 value = new Crc32("Hello");
Crc32 copy = new Crc32(value.Value);
Assert.IsTrue(value == copy);
Assert.IsFalse(value == empty);
Assert.IsTrue(value == copy.Value);
Assert.IsFalse(value == empty.Value);
Assert.IsFalse(value != copy);
Assert.IsTrue(value != empty);
Assert.IsFalse(value != copy.Value);
Assert.IsTrue(value != empty.Value);
}