private byte[] GetHash512(string fileName) { var fl = new FileInfo(fileName).Length; if (fl < 72 * 1024 * 1024) { var data = File.ReadAllBytes(fileName); return(new keccak.SHA3(data.Length).getHash512(data)); } VisibleProgressBar(); var buff = new byte[72 * 1024 * 1024]; var hash = new byte[64]; var sha3 = new keccak.SHA3(buff.Length); using (var or = File.OpenRead(fileName)) { int tmp = 0; long all = 0; bool isInit = false; do { tmp = or.Read(buff, 0, buff.Length); all += tmp; sha3.getHash512(buff, tmp, isInit, tmp != buff.Length, hash); isInit = true; ProgressToProgressBar(fl, all); }while (tmp > 0 && tmp == buff.Length); } ProgressBarUnvisible(); return(hash); }
public readonly static string MainMutexName; // = "relaxtime.8vs.ru|" + BitConverter.ToString(System.Text.Encoding.Unicode.GetBytes(Application.StartupPath)).Replace("-", ""); static Program() { var sha = new keccak.SHA3(1024); MainMutexName = "relaxtime.8vs.ru|" + BitConverter.ToString(sha.getHash224(System.Text.Encoding.Unicode.GetBytes(AppDomain.CurrentDomain.BaseDirectory))).Replace("-", ""); m = new System.Threading.Semaphore(1, 1, MainMutexName); }
public void StartTests() { const int ModNumber = 4; // const int Count = 4096; const int Count = 1024 * 1024; counts = new int[ModNumber]; // goto End; // TODO: УБрать это при тестах for (int i = 0; i < counts.Length; i++) { counts[i] = 0; } using (var l = new LightRandomGenerator(Count)) { l.WaitForGenerator(); var len = l.GeneratedBytes.len; var a = l.GeneratedBytes.array; lock (this) for (int i = 0; i < len; i++) { var mod = a[i] % ModNumber; counts[mod]++; a[i] = (byte)mod; } var sb = new StringBuilder(ModNumber * 256); sb.AppendLine($"Right average is {Count / ModNumber}"); sb.AppendLine(); for (int i = 0; i < ModNumber; i++) { sb.AppendLine(counts[i].ToString("D4")); } task.error.Add(new Error() { Message = sb.ToString() }); File.WriteAllText(@"Z:/LightRandomGeneratorTest_wait.txt", sb.ToString() + "\r\n\r\n" + l.GeneratedBytes.ToString(64 * 1024)); Save2ToBitmap(Count, len, a, @"Z:\LightRandomGeneratorTest_wait.bmp"); } for (int i = 0; i < counts.Length; i++) { counts[i] = 0; } using (var l = new LightRandomGenerator(Count)) { l.doWaitR = false; l.doWaitW = false; l.WaitForGenerator(); Thread.Sleep(500); l.doWaitR = true; l.doWaitW = true; var len = l.GeneratedBytes.len; var a = l.GeneratedBytes.array; lock (this) for (int i = 0; i < len; i++) { var mod = a[i] % ModNumber; counts[mod]++; a[i] = (byte)mod; } var sb = new StringBuilder(ModNumber * 256); for (int i = 0; i < ModNumber; i++) { sb.AppendLine(counts[i].ToString("D4")); } task.error.Add(new Error() { Message = sb.ToString() }); lock (l) File.WriteAllText(@"Z:/LightRandomGeneratorTest_nowait.txt", sb.ToString() + "\r\n\r\n" + l.GeneratedBytes.ToString(64 * 1024)); Save2ToBitmap(Count, len, a, @"Z:\LightRandomGeneratorTest_nowait.bmp"); } for (int i = 0; i < counts.Length; i++) { counts[i] = 0; } using (var l = new LightRandomGenerator_forTests(Count)) { //l.doWaitR = false; //l.doWaitW = false; l.WaitForGenerator(); Thread.Sleep(2000); //l.doWaitR = true; //l.doWaitW = true; var len = l.GeneratedBytes.len; var a = l.GeneratedBytes.array; lock (this) for (int i = 0; i < len; i++) { var mod = a[i] % ModNumber; counts[mod]++; a[i] = (byte)mod; } var sb = new StringBuilder(ModNumber * 256); for (int i = 0; i < ModNumber; i++) { sb.AppendLine(counts[i].ToString("D4")); } task.error.Add(new Error() { Message = sb.ToString() }); lock (l) File.WriteAllText(@"Z:/LightRandomGeneratorTest_forTests_wait.txt", sb.ToString() + "\r\n\r\n" + l.GeneratedBytes.ToString(64 * 1024)); Save2ToBitmap(Count, len, a, @"Z:\LightRandomGeneratorTest_forTests_wait.bmp"); } for (int i = 0; i < counts.Length; i++) { counts[i] = 0; } using (var gen = new VinKekFish_k1_base_20210419_keyGeneration()) { gen.Init1(VinKekFishBase_etalonK1.NORMAL_ROUNDS, null, 0); var key = new byte[] { 1 }; fixed(byte *k = key) gen.Init2(k, 1, null); gen.EnterToBackgroundCycle(doWaitR: true, doWaitW: false); Thread.Sleep(500); gen.ExitFromBackgroundCycle(); Thread.Sleep(500); var a = gen.GetNewKey(Count, 512, null, 4); for (int i = 0; i < a.len; i++) { var mod = a.array[i] % ModNumber; counts[mod]++; a.array[i] = (byte)mod; } var sb = new StringBuilder(ModNumber * 256); for (int i = 0; i < ModNumber; i++) { sb.AppendLine(counts[i].ToString("D4")); } task.error.Add(new Error() { Message = sb.ToString() }); File.WriteAllText(@"Z:/LightRandomGeneratorTest_vinkekfish.txt", sb.ToString() + "\r\n\r\n" + a.ToString(64 * 1024)); Save2ToBitmap(Count, a.len, a, @"Z:\LightRandomGeneratorTest_vinkekfish.bmp"); a.Dispose(); } for (int i = 0; i < counts.Length; i++) { counts[i] = 0; } var sha = new keccak.SHA3(8192); { sha.prepareGamma(new byte[] { 1 }, new byte[] { 2 }); var ar = sha.getGamma(Count); var fAllocator = new cryptoprime.BytesBuilderForPointers.Fixed_AllocatorForUnsafeMemory(); var a = fAllocator.FixMemory(ar); for (int i = 0; i < a.len; i++) { var mod = a.array[i] % ModNumber; counts[mod]++; a.array[i] = (byte)mod; } var sb = new StringBuilder(ModNumber * 256); for (int i = 0; i < ModNumber; i++) { sb.AppendLine(counts[i].ToString("D4")); } task.error.Add(new Error() { Message = sb.ToString() }); File.WriteAllText(@"Z:/LightRandomGeneratorTest_keccak.txt", sb.ToString() + "\r\n\r\n" + a.ToString(64 * 1024)); Save2ToBitmap(Count, a.len, a, @"Z:\LightRandomGeneratorTest_keccak.bmp"); a.Dispose(); } // End: for (int i = 0; i < counts.Length; i++) { counts[i] = 0; } // Здесь вообще нельзя генерировать много - зависнет. Даёт одну генерацию секунд в 1-5, генерация - 3 байта (генерация зависит от дисковой активности) using (var l = new LightRandomGenerator_DiskSlow(Count)) { l.WaitForGenerator(); var len = l.GeneratedBytes.len; var a = l.GeneratedBytes.array; lock (this) for (int i = 0; i < len; i++) { var mod = a[i] % ModNumber; counts[mod]++; a[i] = (byte)mod; } var sb = new StringBuilder(ModNumber * 256); sb.AppendLine($"Right average is {Count / ModNumber}"); sb.AppendLine(); for (int i = 0; i < ModNumber; i++) { sb.AppendLine(counts[i].ToString("D4")); } task.error.Add(new Error() { Message = sb.ToString() }); File.WriteAllText(@"Z:/LightRandomGeneratorTest_diskslow.txt", sb.ToString() + "\r\n\r\n" + l.GeneratedBytes.ToString(64 * 1024)); Save2ToBitmap(Count, len, a, @"Z:\LightRandomGeneratorTest_diskslow.bmp"); } }
private void CalcFileHashes(List <fileNameAndStream> list) { toCalc = list.Count; this.Invoke ( new VOID ( delegate { lock (sync) { logBox.AppendText("\r\nВсего добавлено " + list.Count); if (addErrors > 0) { logBox.AppendText("\r\nОшибок при добавлении " + addErrors); } logBox.AppendText("\r\n\t" + DateTime.Now.ToLongTimeString()); logBox.AppendText("\r\n"); logBox.AppendText("\r\nНачинаются вычисления. Результат добавляется в файл " + fName); } } ) ); loopCnt = 0; int procCount = 0; for (var i = 0; i < 1 /*Environment.ProcessorCount + 1*/; i++) { var procNum = i; System.Threading.ThreadPool.QueueUserWorkItem ( delegate { Interlocked.Increment(ref procCount); try { int len; if (hashType == "512") { len = keccak.SHA3.rNumbers[3] >> 3; } else if (hashType == "384") { len = keccak.SHA3.rNumbers[2] >> 3; } else if (hashType == "256") { len = keccak.SHA3.rNumbers[1] >> 3; } else { len = keccak.SHA3.rNumbers[0] >> 3; } var a = new keccak.SHA3(len + 1024); string FileName = ""; fileNameAndStream fns; int lastNum = 0; // FileInfo fi = null; byte[] buff; /*if (procNum > 0) * buff = new byte[64*1024*1024]; * else*/ buff = new byte[len]; byte[] hash = new byte[64]; Start: FileName = ""; long aft = 0, act = 0; long n1; try { long fl; lock (sync) { lastNum = list.Count - 1; /*File: * fsi = null; * fi = null; * di = null;*/ if (toCalc <= 0 || (lastNum < 0 && procNum > 0)) { return; } //if (lastNum >= list.Count) if (list.Count <= 0) { while (procCount > 1) { Monitor.Wait(sync); } if (procNum == 0) { foreach (var afh in lfh) { File.AppendAllText(fName, afh.FileName + "\r\n" + afh.hash + "\r\n"); } lfh.Clear(); } return; } fns = list[lastNum]; FileName = fns.fileName + fns.fileStream; n1 = DateTime.Now.Ticks; fl = fns.size; /*fi = new FileInfo(fns.fileName); * * fl = 0; * if (fi.Exists) * { * fl = fi.Length; * fsi = fi; * } * else * { * di = new DirectoryInfo(FileName); * if (di.Exists) * { * fsi = di; * } * }*/ aft += DateTime.Now.Ticks - n1; /*if (fl > buff.Length) * // if (procNum > 0 || fl > 1024 * 1024 * 256) * { * // if (fl > 1024 * 1024 * 256) * //lock (sync) * { * n1 = DateTime.Now.Ticks; * File.AppendAllText(fName, FileName + "\r\n" + "skipped " + fl + "\r\n"); * aft += DateTime.Now.Ticks - n1; * * n1 = DateTime.Now.Ticks; * list.RemoveAt(lastNum); * act += DateTime.Now.Ticks - n1; * calcCountS++; * } * * lastNum--; * goto File; * }*/ n1 = DateTime.Now.Ticks; list.RemoveAt(lastNum); act += DateTime.Now.Ticks - n1; /* * if (fsi == null) * goto Start;*/ } /* * if (di != null) * { * var dac = di.GetAccessControl(); * * var sb = new StringBuilder(); * * // new FileIOPermission(FileIOPermissionAccess.Read, di.FullName).Demand(); * * var dars = dac.GetAccessRules(true, false, WindowsIdentity.GetCurrent().GetType()); * foreach (var dar in dars) * { * * sb.AppendLine(); * } * * di = null; * goto Start; * } */ // var fac = fi.GetAccessControl(); bool isInit = false; int tmp = 0; // byte[] fc = null; // Эта блокировка нужна только для того, чтобы к диску потоки обращались последовательно try { lock (list) { // fc = File.ReadAllBytes(FileName); n1 = DateTime.Now.Ticks; var fnb = utf16.GetBytes(FileName); int bin = Form1.CreateFileW(fnb, 0x80000000, 0x00000002 | 0x00000001, 0, 3, 0x20000000 | 0x80000000, 0); // FILE_FLAG_NO_BUFFERING FILE_FLAG_WRITE_THROUGH if (bin <= 0) { throw new Exception("CreateFileW in readFile GetLast error " + Form1.GetLastError() + " for file " + FileName); } try { aft += DateTime.Now.Ticks - n1; do { n1 = DateTime.Now.Ticks; FormCrypt.ReadFile(bin, buff, buff.Length, out tmp, 0); if (hash == null && tmp <= 0) { throw new Exception("ReadFile in readFile GetLast error " + Form1.GetLastError() + " for file " + FileName); } aft += DateTime.Now.Ticks - n1; n1 = DateTime.Now.Ticks; if (hashType == "512") { hash = a.getHash512(buff, tmp, isInit, tmp != buff.Length, hash); } else if (hashType == "384") { hash = a.getHash384(buff, tmp, isInit, tmp != buff.Length, hash); } else if (hashType == "256") { hash = a.getHash256(buff, tmp, isInit, tmp != buff.Length, hash); } else { hash = a.getHash224(buff, tmp, isInit, tmp != buff.Length, hash); } isInit = true; act += DateTime.Now.Ticks - n1; }while (tmp > 0 && tmp == buff.Length); } finally { Form1.CloseHandle(bin); } } } catch (Exception ex) { lock (sync) { calcCountE++; } lock (sync) { try { File.AppendAllText(fName, FileName + "\r\n" + "error " + ex.Message + "\r\n"); } catch {} } goto Start; } n1 = DateTime.Now.Ticks; var fh = new FileHash(FileName, Convert.ToBase64String(hash)); lock (sync) { allFTime += aft; lfh.Add(fh); act += DateTime.Now.Ticks - n1; allCTime += act; calcCount++; } } catch (Exception ex) { lock (sync) { calcCountE++; } lock (sync) { try { File.AppendAllText(fName, FileName + "\r\n" + "error " + ex.Message + "\r\n"); } catch {} } try { this.Invoke ( new VOID ( delegate { lock (sync) { logBox.AppendText("\r\nОшибка " + ex.Message); logBox.AppendText("\r\nОшибка " + FileName); } } ) ); } catch {} } goto Start; } finally { Interlocked.Decrement(ref procCount); lock (sync) Monitor.Pulse(sync); } } ); } }
// GOST5 = GostRegime - 21 public unsafe byte[] getGamma(long gammaLength, int GOST5 = 0) { if (workingKey == null) { throw new Exception("GOST 28147 mod. gamma not prepared"); } if (gammaLength <= 0) { throw new ArgumentOutOfRangeException("GOST 28147 mod. gammaLenght is incorrect"); } var bb = new BytesBuilder(); int SL = snc.Length & 0x7FFFFFF8; int BL = SL; if (SL > 72) { BL = 64; } bool BL72 = false; byte[] S = null; byte[] SA = null; int tmp72 = 0; int kl = 0; byte[] tmpp = null; keccak.SHA3 sha = null, shaR = null; if (GOST5 > 0) { BL72 = (SL % 72 > 0) && (SL > 71); if (GOST5 < 13) { tmp72 = (SL / 72) * (GOST5 == 1 ? 64 : 16); } else { tmp72 = (SL / 72) * 8; // Хеш возвращает только 8 байтов } S = BytesBuilder.CloneBytes(snc, 0, SL); SA = new byte[SL]; tmpp = new byte[SL + 72]; sha = new SHA3(SL); shaP = new SHA3.SHA3Random(snc); // это не лишнее, получает ключ для инициализирующих перестановок if (GOST5 >= 13) { shaPP = new SHA3.SHA3Random(shaP.nextBytes64()); shaR = new SHA3(SL); } if (SL < 64) { throw new ArgumentException("GOST 28147 mod. sync lenght is incorrect"); } } snc = null; fixed(byte *S1 = currentSBox, S2 = currentSBox2, S3 = currentSBox3, S4 = currentSBox4, S5 = currentSBox5, S6 = currentSBox6) { BytesBuilder.CopyTo(N1, _N3); BytesBuilder.CopyTo(N2, _N3, 4); byte[] k = { 0, 1, 2, 3, 4 }; byte *[] b = { S1, S2, S3, S4, S5, S6 }; if (GOST5 > 0) { if (GOST5 <= 2) { processBlocksForGamma(SL, S, SA, k, b); processBlocksForGamma(SL, SA, S, k, b, true); processBlocksForGamma(SL, S, SA, k, b, true); processBlocksForGamma(SL, SA, S, k, b, true); } else { processBlocksForGamma2(SL, S, SA, k, b, false, GOST5); processBlocksForGamma2(SL, SA, S, k, b, true, GOST5); processBlocksForGamma2(SL, S, SA, k, b, true, GOST5); processBlocksForGamma2(SL, SA, S, k, b, true, GOST5); } sha.getDuplex(S, false, -1, false); if (GOST5 >= 13) { shaR.getDuplex(S, false, -1, false); } } else { // Делаем несколько раз шифрование, на всяк пожарный, хотя это и не по ГОСТу ProcessBlock(_N3, 0, _N4, 0, workingKey[0], S2); ProcessBlock(_N4, 0, _N3, 0, workingKey[1], S3); ProcessBlock(_N3, 0, _N4, 0, workingKey[2], S4); ProcessBlock(_N4, 0, _N3, 0, workingKey[3], S1); ProcessBlock(_N3, 0, _N4, 0, workingKey[2], S3); ProcessBlock(_N4, 0, _N3, 0, workingKey[1], S1); ProcessBlock(_N3, 0, _N4, 0, workingKey[3], S2); ProcessBlock(_N4, 0, _N3, 0, workingKey[0], S4); } BytesBuilder.CopyTo(_N3, N1); BytesBuilder.CopyTo(_N3, N2, 0, 4, 4); long n1 = bytesToint(N1, 0), n2 = bytesToint(N2, 0); int _n1, _n2; int ik = 0; do { if (GOST5 > 0) { byte[] tmp1, tmp2, tmp3; fixed(byte *s = S, sa = SA, tmpp_ = tmpp) { if (GOST5 <= 2) { processBlocksForGamma(SL, S, SA, k, b); } else { processBlocksForGamma2(SL, S, SA, k, b, false, GOST5); } if (GOST5 < 13) { tmp1 = sha.getDuplex(SA, true); } else { tmp1 = shaR.getDuplex(SA, true); } addConstant(s, S.Length); tmp3 = BytesBuilder.CloneBytes(sa, 0, SA.Length); if (GOST5 <= 2) { processBlocksForGamma(SL, S, SA, k, b); } else { processBlocksForGamma2(SL, S, SA, k, b, false, GOST5); } if (GOST5 < 13) fixed(byte *tmp1_ = tmp1, tmp3_ = tmp3) { if (GOST5 > 1) { MergePermutation.permutationMergeBytes(tmp3_, tmp3.Length, sa, tmpp_, SL, ref kl, true); } SHA3.xorBytesWithGamma(SA, tmp1); MergePermutation.permutationMergeBytes(tmp1_, tmp1.Length, sa, tmpp_, SL, ref kl, true); BytesBuilder.ToNull(tmp1.Length, tmp1_); BytesBuilder.ToNull(tmp3.Length, tmp3_); } if (GOST5 < 13) { tmp2 = sha.getDuplex(SA, true, GOST5 == 1 ? 64 : 16); } else { tmp2 = sha.getDuplexMod(SA, tmp1, true, 8, 0); BytesBuilder.ToNull(tmp1); BytesBuilder.ToNull(tmp3); } addConstant(s, S.Length); } if (BL72) { bb.add(BytesBuilder.CloneBytes(tmp2, tmp2.Length - tmp72, tmp72)); BytesBuilder.ToNull(tmp2); } else { bb.add(tmp2); } } else { n1 += 0x1010101; n2 += 0x1010104; if (n2 >= 0x100000000L) { n2 -= 0xFFFFFFFFL; } if (n1 >= 0x100000000L) { n1 -= 0x100000000L; } _n1 = (int)n1; _n2 = (int)n2; intTobytes(_n1, _N3, 0); intTobytes(_n2, _N3, 4); // Получаем новые значения N1; опять не по ГОСТу - 4 преобразования вместо одного ProcessBlock(_N3, 0, _N4, 0, workingKey[2], S1); ProcessBlock(_N4, 0, _N3, 0, workingKey[(ik + 1) & 1], S3); ProcessBlock(_N3, 0, _N4, 0, workingKey[ik & 1], S4); ProcessBlock(_N4, 0, _N3, 0, workingKey[3], S2); bb.addCopy(_N3); } ik++; }while (bb.Count < gammaLength); n1 = 0; n2 = 0; _n1 = 0; _n2 = 0; } for (int i = 0; i < workingKey.Length; i++) { for (int j = 0; j < workingKey[i].Length; j++) { workingKey[i][j] = 0; } } workingKey = null; if (workingKeyA != null) { BytesBuilder.ToNull(workingKeyA); workingKeyA = null; } BytesBuilder.ToNull(N1); BytesBuilder.ToNull(N2); BytesBuilder.ToNull(_N3); BytesBuilder.ToNull(_N4); var result = bb.getBytes(gammaLength); bb.clear(); if (S != null) { BytesBuilder.ToNull(S); BytesBuilder.ToNull(SA); BytesBuilder.ToNull(tmpp); } if (sha != null) { sha.Clear(true); shaP.Clear(); sha = null; shaP = null; if (shaPP != null) { shaPP.Clear(); shaPP = null; } } return(result); }