Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Is first character of MD5 hash evenly distributed?
//It is! using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Security.Cryptography; using System.Text; namespace Rextester { public class Program { public static void Main(string[] args) { Dictionary<char, int> distribution = new Dictionary<char, int>(); for(int i=0; i<100000; i++) { char first_letter = CalculateMD5Hash(RandomString(_rng.Next(5, 30))).ToLower()[0]; if(distribution.ContainsKey(first_letter)) { distribution[first_letter]++; } else { distribution[first_letter] = 1; } } foreach(var k in distribution.Keys.OrderBy(f => f)) { Console.WriteLine("{0}: {1}", k, distribution[k]); } } private static readonly Random _rng = new Random(); private static string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private static string RandomString(int size) { char[] buffer = new char[size]; for (int i = 0; i < size; i++) { buffer[i] = _chars[_rng.Next(_chars.Length)]; } return new string(buffer); } private static string CalculateMD5Hash(string input) { var md5 = MD5.Create(); byte[] inputBytes = Encoding.Unicode.GetBytes(input); byte[] hash = md5.ComputeHash(inputBytes); // step 2, convert byte array to hex string StringBuilder sb = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { sb.Append(hash[i].ToString("X2")); } return sb.ToString(); } } }
run
|
edit
|
history
|
help
0
6
Math Operations
("Hello, world!\n");
Rijndael AES key generator C#
Remove Duplicates from a string
7.1. Asynchrony: the new approach
pared 1
Test equality...2
Ejercicio bucles
Golden Ratio Approximation