// Pops out random know answer tests from the list, max number is 1000 public KnownAnswerTests RandomKATsSubset(KnownAnswerTests kats, uint subsetLength = 200) { if (subsetLength >= 2000) { throw new ArgumentOutOfRangeException("Maximum number of KATs available is 2000"); } subsetLength = 2000 - subsetLength; Random rnd = new Random(); for (int i = 0; i < subsetLength; i++) { kats.ModSHA256.RemoveAt(rnd.Next(kats.ModSHA256.Count)); kats.ModSHA512.RemoveAt(rnd.Next(kats.ModSHA512.Count)); } return(kats); }
public static KnownAnswerTests ParseKATFile() { try { if (!File.Exists(katpath)) { using (WebClient client = new WebClient()) { client.DownloadFile(katurl, "kat.txt"); } } string[] lines = File.ReadAllLines(katpath); KnownAnswerTests KATs = new KnownAnswerTests(); // Initialise regexes Regex kdf256regex = new Regex("KDF/SHA-256"); Regex kdf512regex = new Regex("KDF/SHA-512"); Regex modSHA256initregex = new Regex("2048-bit modulus, SHA-256"); Regex modSHA512initregex = new Regex("2048-bit modulus, SHA-512"); Regex modSHA256regex = new Regex(@"2048-bit modulus, SHA-256 input: ([a-f0-9]*)" + " salt: ([a-f0-9]*) pre-hashing: (.*) post-hashing: (.*) bin384: ([a-f0-9]*)" + " bin4096: ([a-f0-9]*) str384: ([A-Za-z0-9+/_]*) str4096: ([A-Za-z0-9+/_]*)"); Regex modSHA512regex = new Regex(@"2048-bit modulus, SHA-512 input: ([a-f0-9]*)" + " salt: ([a-f0-9]*) pre-hashing: (.*) post-hashing: (.*) bin384: ([a-f0-9]*)" + " bin4096: ([a-f0-9]*) str384: ([A-Za-z0-9+/_]*) str4096: ([A-Za-z0-9+/_]*)"); // Parse lines, seperate KATs into appropriate lists of dictionaries for (int i = 0; i < lines.Length; i++) { string line = lines[i]; // Check KAT type if (kdf256regex.Match(line).Success) { var dict = new Dictionary <string, string> { { "input", lines[i + 1].Replace("input: ", string.Empty) }, { "output", lines[i + 2].Replace("output: ", string.Empty) } }; KATs.KDF256.Add(dict); } else if (kdf512regex.Match(line).Success) { var dict = new Dictionary <string, string> { { "input", lines[i + 1].Replace("input: ", string.Empty) }, { "output", lines[i + 2].Replace("output: ", string.Empty) } }; KATs.KDF512.Add(dict); } else if (modSHA256initregex.Match(line).Success) { // Concatenate variables and extract regex captures String concat = String.Join(" ", lines.Skip(i).Take(9)); Match ModSHA256Match = modSHA256regex.Match(concat); if (ModSHA256Match.Success) { var dict = new Dictionary <string, string>(); dict.Add("hashfunction", "SHA256"); dict.Add("input", ModSHA256Match.Groups[1].ToString()); dict.Add("salt", ModSHA256Match.Groups[2].ToString()); dict.Add("pre-hashing", ModSHA256Match.Groups[3].ToString()); dict.Add("post-hashing", ModSHA256Match.Groups[4].ToString()); dict.Add("bin384", ModSHA256Match.Groups[5].ToString()); dict.Add("bin4096", ModSHA256Match.Groups[6].ToString()); dict.Add("str384", ModSHA256Match.Groups[7].ToString()); dict.Add("str4096", ModSHA256Match.Groups[8].ToString()); KATs.ModSHA256.Add(dict); } } else if (modSHA512initregex.Match(line).Success) { String concat = String.Join(" ", lines.Skip(i).Take(9)); Match ModSHA512Match = modSHA512regex.Match(concat); if (ModSHA512Match.Success) { var dict = new Dictionary <string, string>(); dict.Add("hashfunction", "SHA512"); dict.Add("input", ModSHA512Match.Groups[1].ToString()); dict.Add("salt", ModSHA512Match.Groups[2].ToString()); dict.Add("pre-hashing", ModSHA512Match.Groups[3].ToString()); dict.Add("post-hashing", ModSHA512Match.Groups[4].ToString()); dict.Add("bin384", ModSHA512Match.Groups[5].ToString()); dict.Add("bin4096", ModSHA512Match.Groups[6].ToString()); dict.Add("str384", ModSHA512Match.Groups[7].ToString()); dict.Add("str4096", ModSHA512Match.Groups[8].ToString()); KATs.ModSHA512.Add(dict); } } //string[] katKeys = { "input", "salt", "pre-hashing", "post-hashing", "bin384", "bin4096", "str384", "str4096" }; //var dict = new Dictionary<string, string>{{ "hashfunction", "SHA512" } }; //for (int j = 0; i < 8; i++ ) //{ // dict.Add(katKeys[j], ModSHA512Match.Groups[j+1].ToString()); ) //} } return(KATs); } catch (FileNotFoundException) { throw new FileNotFoundException("No KAT file and unable to download," + " kat.txt can be found here:" + " https://github.com/bsdphk/PHC/blob/master/Makwa/kat.txt. " + "Place in KnownAnswerTests folder"); } }