public Task <RedisValue[]> SetRandomMembersAsync(RedisKey key, long count) { RedisValue[] values; DbSet.TryGetValue(key, out values); RedisValue[] result; if (values != null) { ThreadSafeRandom.Shuffle(values); if (values.Length < count) { result = values; } else { result = values.ToList().GetRange(0, (int)count).ToArray(); } } else { result = new RedisValue[0]; } return(Task.FromResult(result)); }
/// <summary> /// Overwrite the file based on the Peter Gutmann's algorithm. /// </summary> /// <param name="file">The file.</param> internal static void OverwriteFile_Gutmann(FileInfo file) { byte[][] pattern = new byte[][] { new byte[] { 0x55, 0x55, 0x55 }, new byte[] { 0xAA, 0xAA, 0xAA }, new byte[] { 0x92, 0x49, 0x24 }, new byte[] { 0x49, 0x24, 0x92 }, new byte[] { 0x24, 0x92, 0x49 }, new byte[] { 0x00, 0x00, 0x00 }, new byte[] { 0x11, 0x11, 0x11 }, new byte[] { 0x22, 0x22, 0x22 }, new byte[] { 0x33, 0x33, 0x33 }, new byte[] { 0x44, 0x44, 0x44 }, new byte[] { 0x55, 0x55, 0x55 }, new byte[] { 0x66, 0x66, 0x66 }, new byte[] { 0x77, 0x77, 0x77 }, new byte[] { 0x88, 0x88, 0x88 }, new byte[] { 0x99, 0x99, 0x99 }, new byte[] { 0xAA, 0xAA, 0xAA }, new byte[] { 0xBB, 0xBB, 0xBB }, new byte[] { 0xCC, 0xCC, 0xCC }, new byte[] { 0xDD, 0xDD, 0xDD }, new byte[] { 0xEE, 0xEE, 0xEE }, new byte[] { 0xFF, 0xFF, 0xFF }, new byte[] { 0x92, 0x49, 0x24 }, new byte[] { 0x49, 0x24, 0x92 }, new byte[] { 0x24, 0x92, 0x49 }, new byte[] { 0x6D, 0xB6, 0xDB }, new byte[] { 0xB6, 0xDB, 0x6D }, new byte[] { 0xDB, 0x6D, 0xB6 } }; ThreadSafeRandom.Shuffle <byte[]>(pattern); Random random = ThreadSafeRandom.Random; FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Write, FileShare.None); Console.WriteLine("Overwrite this file: "); for (int pass = 1; pass <= 35; ++pass) { Console.Write(((pass < 10)?"0":"") + pass + ((pass % 7 == 0)?" \n":" ")); for (int index = 0; index < 3; index++) { fs.Position = 0; for (long size = fs.Length; size > 0; size -= MAX_BUFFER_SIZE) { long bufferSize = (size < MAX_BUFFER_SIZE) ? size : MAX_BUFFER_SIZE; byte[] buffer = new byte[bufferSize]; if (pass > 4 && pass < 32) { for (int bufferIndex = 0; bufferIndex < bufferSize; ++bufferIndex) { buffer[bufferIndex] = pattern[pass - 5][index]; } } else { for (int bufferIndex = 0; bufferIndex < bufferSize; ++bufferIndex) { buffer[bufferIndex] = (byte)(random.Next() % 256); } } fs.Write(buffer, 0, buffer.Length); fs.Flush(true); } } } Console.WriteLine(); fs.Close(); fs.Dispose(); fs = null; }
private void ResolveLocations() { if (_resolvedMachineIds == null) { _resolvedMachineIds = new List <MachineId>(Count); _resolvedMachineIds.AddRange(_locations.EnumerateMachineIds()); if (_randomize) { ThreadSafeRandom.Shuffle(_resolvedMachineIds); } // Sorting resolved machine ids by reputation: machines with good reputation should be used first. _resolvedMachineIds = _resolvedMachineIds.OrderBy(id => (int)_reputationTracker.GetReputation(id)).ToList(); } }
/// <summary> /// start /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void button1_Click(object sender, EventArgs e) { richTextBox2.Text = ""; if (Variables.Punkte.Count == 0) { var response = ImportHelper.CsvContentToPraxen(TierärtzteDatei); if (response == null) { button1.Enabled = true; return; } richTextBox2.Text += response; try { var count = 0; var praxisdic = SaveHelper.RetrieveScore(ScoreJsonDatei); foreach (var key in praxisdic.Keys) { var praxis = Variables.Praxen.FirstOrDefault(p => p.Id == key); if (praxis != null) { count++; praxis.LastYearScore = praxisdic[key]; } } richTextBox2.Text += count + " Praxen konnten ein Score zugeordnet werden\n"; } catch (Exception ex) { var message = "score.json Datei fehlerhaft, das Einlesen musste abgebrochen werden. \nGenauer Fehler: " + ex; MessageBox.Show(message); richTextBox2.Text += message; return; } try { Variables.History = SaveHelper.RetrieveHistory(HistoryJsonDatei); richTextBox2.Text += "history.json hat " + Variables.History.Count + " Einträge\n"; } catch (Exception ex) { var message = "history.json Datei fehlerhaft, das Einlesen musste abgebrochen werden. \nGenauer Fehler: " + ex; MessageBox.Show(message); richTextBox2.Text += message; return; } } //init Variables.Minanzahl = new int[4] { 0, 0, 0, 0 }; //Feiertage werden eingelesen string[] daten = this.feiertage.Text.Split(new char[] { Convert.ToChar("\n") }); for (int i = 0; i < daten.Count(); i++) { if (daten[i].Count() > 2) { try { Variables.Feiertage.Add(Convert.ToDateTime(daten[i].Trim())); } catch { MessageBox.Show("Feiertag auf der Zeile " + (i + 1) + " konnte nicht eingelesen werden"); return; } } } DateTime start = Start.Value.Date; DateTime ende = Ende.Value.Date; if (ende - start < TimeSpan.FromDays(0)) { MessageBox.Show("Bitte wählen Sie gültige Anfangs- und Enddaten"); string.Format("hallo {0}!", "ww"); return; } int normaldays = 0; int saturdays = 0; int sundays = 0; int holidays = 0; //bestimmung anzahl der jeweiligen gruppe while (start < ende) { if (DateTimeHelper.IsFeiertag(start)) { holidays++; } else if (start.DayOfWeek == DayOfWeek.Saturday) { saturdays++; } else if (start.DayOfWeek == DayOfWeek.Sunday) { sundays++; } else { normaldays++; } start = start.AddDays(1); } int totaldays = holidays + normaldays + saturdays + sundays; richTextBox2.Text += totaldays + " Tage zu verteilen\n"; //feiertage VerteilerHelper.VerteileTageZuPunkte(holidays, 0); Variables.Punkte = Variables.Punkte.OrderBy(d => d.Score).ToList(); //sonntag VerteilerHelper.VerteileTageZuPunkte(sundays, 1); Variables.Punkte = Variables.Punkte.OrderBy(d => d.Score).ToList(); //samstag VerteilerHelper.VerteileTageZuPunkte(saturdays, 2); Variables.Punkte = Variables.Punkte.OrderBy(d => d.Score).ToList(); //wochentag VerteilerHelper.VerteileTageZuPunkte(normaldays, 3); Variables.Punkte = Variables.Punkte.OrderByDescending(d => d.Score).ToList(); //sotiere nahc feiertag, damit niemand zwei hat Variables.Punkte = Variables.Punkte.OrderByDescending(p => p.Anzahl[0]).ToList(); var firsttime = true; //Punkte an Praxen verteilen while (Variables.Punkte.Count > 0) { if (!firsttime) { Variables.Punkte = Variables.Punkte.OrderByDescending(d => d.Score).ToList(); } else { firsttime = false; } //Punkte nach score sortieren (highest -> last) Variables.Praxen = Variables.Praxen.OrderBy(p => p.ScorePerPoint).ToList(); //Punkte einfügen for (int i = 0; i < Variables.Praxen.Count; i++) { if (Variables.Praxen[i].Punkte > Variables.Praxen[i].AssignedPoints.Count) { Variables.Punkte[0].Praxis = Variables.Praxen[i]; Variables.Praxen[i].AssignedPoints.Add(Variables.Punkte[0]); Variables.Punkte.RemoveAt(0); } } } //beste Verteilung (einmalig) List <Punkt> punkteliste = new List <Punkt>(); foreach (var item in Variables.Praxen) { int count = item.AssignedPoints.Count; int totalcount = punkteliste.Count; int step = totalcount / count; count--; while (true) { punkteliste.Insert(totalcount, item.AssignedPoints[count]); count--; totalcount -= step; if (count <= -1) { break; } } } start = Start.Value.Date; ende = Ende.Value.Date; //daten verteilen List <Punkt> punkte = CloneHelper.DeepClone(punkteliste); var tries = 0; var maxtries = Convert.ToInt32(triesproabstand.Text); var minabst = Convert.ToInt32(minabstand.Text); richTextBox2.Text += "versuche mit mindestabstand " + minabst + "\n"; var richtextboxtext = richTextBox2.Text; bool found = false; while (!found) { found = await Task.Run(() => VerteilerHelper.VerteileNotfalldienstPunkteZuDaten(punkte, start, ende, Variables.History, minabst)); if (!found) { punkte = CloneHelper.DeepClone(punkteliste); ThreadSafeRandom.Shuffle(punkte); if (++tries > maxtries) { tries = 0; minabst--; if (minabst == 0) { MessageBox.Show( "Vorgang kann nicht abgeschlossen werden. Die Punkte können nicht gemäss den Regeln verteilt werden. Durch das Zufallsprinzip kann das vorkommen, bitte versuchen Sie es einfach nocheinmal."); return; } richTextBox2.Text += "\nversuche mit mindestabstand " + minabst + "\n"; richtextboxtext = richTextBox2.Text; } else { richTextBox2.Text = richtextboxtext + "gescheitert für mindestabstand " + minabst + " (" + tries + " / " + maxtries + ")\n"; } } } richTextBox2.Text += "\n\nLösung gefunden!\n"; richTextBox2.Text += "mindestabstand: " + minabst + " Tage\n"; //add correct points to praxen foreach (var praxise in Variables.Praxen) { praxise.AssignedPoints = punkte.Where(p => p.Praxis == praxise).ToList(); } VerteilerHelper.VerteileWochentelefone(Variables.Praxen.OrderBy(p => p.Id).ToList(), start, ende, Convert.ToInt32(textBox2.Text.Trim())); double tiefstes = Variables.Praxen.Where(d => d.Punkte > 0).OrderBy(d => d.ScorePerPoint).First().ScorePerPoint; double durchschnitt = Variables.Praxen.Sum(d => d.ScorePerPoint * d.Punkte) / Variables.Praxen.Sum(p => p.Punkte); double höchstens = Variables.Praxen.Where(d => d.Punkte > 0).OrderByDescending(d => d.ScorePerPoint).First().ScorePerPoint; richTextBox2.Text += "tiefstes: " + tiefstes + "\n"; richTextBox2.Text += "höchstens: " + höchstens + "\n"; richTextBox2.Text += "durchschnitt: " + Math.Round(durchschnitt, 3) + "\n"; richTextBox2.Text += "dateien werden generiert...\n"; ExportHelper.ExportStats(savePath.Text); var dic = Variables.Praxen.ToDictionary(praxis => praxis.Id, praxis => praxis.ScorePerPoint * praxis.Punkte - (praxis.Punkte * durchschnitt)); ExportHelper.ExportSave(dic, savePath.Text); ExportHelper.ExportSql(punkte, Variables.Praxen, savePath.Text); ExportHelper.ExportAnalythicsAndHistory(punkte, savePath.Text); richTextBox2.Text += "erfolgreich abgeschlossen"; }
/// <summary> /// Method to purge a volume /// Based on the U.S. Department of Defense's standard 'National Industrial Security Program Operating Manual' (DoD 5220.22-M ECE). /// </summary> /// <param name="selectedVolume"></param> public static async Task eraseVolume(CancellationToken ct, Volume selectedVolume, DeleteAlgorithm.DeleteAlgorithmEnum algorithm = DeleteAlgorithm.DeleteAlgorithmEnum.DoD_7) { // Variables var dummyFilesCount = Math.Floor((double)selectedVolume.AvailableFreeSpace / MAX_BUFFER_SIZE); byte[] pattern = new byte[] { 0x00, 0xFF, 0x72, 0x96, 0x00, 0xFF, 0x72 }; int loops = 6; if (algorithm == DeleteAlgorithm.DeleteAlgorithmEnum.DoD_3) { loops = 2; pattern = new byte[] { 0x00, 0xFF, 0x72 }; } ThreadSafeRandom.Shuffle <byte>(pattern); Random random = ThreadSafeRandom.Random; // Set progressbar MainWindow.PGBar.Maximum = selectedVolume.AvailableFreeSpace; MainWindow.PGBar.Value = 0; // available free space will be filled with random files for (int i = 1; i <= dummyFilesCount; i++) { // Write asynchronous to implement cancel button using (FileStream fs = File.Create(selectedVolume.Name + i, MAX_BUFFER_SIZE, FileOptions.Asynchronous)) { // Loop 3-7 times (DoD 5220.22-M / ECE) for (int pass = 0; pass <= loops; ++pass) { fs.Position = 0; if (ct.IsCancellationRequested) { ct.ThrowIfCancellationRequested(); return; } long bufferSize = MAX_BUFFER_SIZE; byte[] buffer = new byte[bufferSize]; if (pass != 1 && (algorithm == DeleteAlgorithm.DeleteAlgorithmEnum.DoD_7 && pass != 5)) { for (int bufferIndex = 0; bufferIndex < bufferSize; ++bufferIndex) { buffer[bufferIndex] = pattern[pass]; } } else { for (int bufferIndex = 0; bufferIndex < bufferSize; ++bufferIndex) { buffer[bufferIndex] = (byte)(random.Next() % 256); } } // Create file await fs.WriteAsync(buffer, 0, buffer.Length, ct); // Delegate event for progressbar MainWindow.PGBar.Dispatcher.Invoke(EmptyDelegate, DispatcherPriority.Background); MainWindow.LabelProgress.Dispatcher.Invoke(EmptyDelegate, DispatcherPriority.Background); fs.Flush(true); } // Increase progressbar MainWindow.LabelProgress.Content = Math.Round(i / dummyFilesCount, 3) * 100 + " %"; MainWindow.PGBar.Value += MAX_BUFFER_SIZE; } } // After everything is done, delete everything again VolumeController.deleteVolume(selectedVolume); }