Last active
March 31, 2017 23:11
-
-
Save xmichaelx/cbbbac1be07125bd2784c46c6d5aba31 to your computer and use it in GitHub Desktop.
password generation based on https://www.eff.org/dice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Security.Cryptography; | |
namespace PasswordGenerator | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var words = args.Length > 0 ? int.Parse(args[0]) : 5; | |
var dict = GenerateDictionary(); | |
var password = string.Join(" ", GenerateRandomStrings().Take(words).Select(x => dict[x])); | |
Console.WriteLine(password); | |
} | |
private static Dictionary<string,string> GenerateDictionary() | |
{ | |
var url = "https://www.eff.org/files/2016/07/18/eff_large_wordlist.txt"; | |
WebClient client = new WebClient(); | |
return client.DownloadString(url) | |
.Split('\n') | |
.Where(line => !string.IsNullOrWhiteSpace(line)) | |
.Select(line => line.Split('\t')) | |
.ToDictionary(x => x[0], x => x[1]); | |
} | |
public static IEnumerable<string> GenerateRandomStrings() | |
{ | |
var rng = RandomNumberGenerator.Create(); | |
while (true) | |
{ | |
var buffer = new byte[1]; | |
var password = new byte[5]; | |
for (var i = 0; i < 5; i++) | |
{ | |
rng.GetNonZeroBytes(buffer); | |
while ((buffer[0] < 1) || (buffer[0] > 6)) rng.GetNonZeroBytes(buffer); | |
password[i] = buffer[0]; | |
} | |
yield return string.Join("", password.Select(x => x.ToString())); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment