Skip to content

Instantly share code, notes, and snippets.

@xmichaelx
Last active March 31, 2017 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xmichaelx/cbbbac1be07125bd2784c46c6d5aba31 to your computer and use it in GitHub Desktop.
Save xmichaelx/cbbbac1be07125bd2784c46c6d5aba31 to your computer and use it in GitHub Desktop.
password generation based on https://www.eff.org/dice
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