61 lines
1.9 KiB
C#
61 lines
1.9 KiB
C#
using System.Collections.Generic;
|
|
using System.Security.Cryptography;
|
|
using DTLib.Filesystem;
|
|
|
|
namespace DTLib
|
|
{
|
|
//
|
|
// хеширует массивы байтов алшоритмом SHA256 и файлы алгоримом XXHash32
|
|
//
|
|
public class Hasher
|
|
{
|
|
readonly HashAlgorithm sha256 = SHA256.Create();
|
|
readonly HashAlgorithm xxh32 = XXHash32.Create();
|
|
|
|
public Hasher() { }
|
|
|
|
// хеш массива
|
|
public byte[] Hash(byte[] input) => sha256.ComputeHash(input);
|
|
|
|
// хеш из двух массивов
|
|
public byte[] Hash(byte[] input, byte[] salt)
|
|
{
|
|
List<byte> rez = new List<byte>();
|
|
rez.AddRange(input);
|
|
rez.AddRange(salt);
|
|
return sha256.ComputeHash(rez.ToArray());
|
|
}
|
|
|
|
// хеш двух массивов зацикленный
|
|
public byte[] HashCycled(byte[] input, byte[] salt, ushort cycles)
|
|
{
|
|
for (uint i = 0; i < cycles; i++)
|
|
{
|
|
input = Hash(input, salt);
|
|
}
|
|
return input;
|
|
}
|
|
// хеш зацикленный
|
|
public byte[] HashCycled(byte[] input, ushort cycles)
|
|
{
|
|
for (uint i = 0; i < cycles; i++)
|
|
{
|
|
input = Hash(input);
|
|
}
|
|
return input;
|
|
}
|
|
|
|
// хеш файла
|
|
public byte[] HashFile(string filename)
|
|
{
|
|
using System.IO.FileStream fileStream = File.OpenRead(filename);
|
|
//var then = DateTime.Now.Hour * 3600 + DateTime.Now.Minute * 60 + DateTime.Now.Second;
|
|
byte[] hash = xxh32.ComputeHash(fileStream);
|
|
//var now = DateTime.Now.Hour * 3600 + DateTime.Now.Minute * 60 + DateTime.Now.Second;
|
|
//PublicLog.Log($"xxh32 hash: {hash.HashToString()} time: {now - then}\n");
|
|
fileStream.Close();
|
|
return hash;
|
|
}
|
|
}
|
|
}
|