Skip to content

Instantly share code, notes, and snippets.

@ThaiWood
Created May 28, 2013 02:33
Show Gist options
  • Save ThaiWood/5660182 to your computer and use it in GitHub Desktop.
Save ThaiWood/5660182 to your computer and use it in GitHub Desktop.
A small Ruby script to generate passwords a la XKCD (http://xkcd.com/936/) using a wordlist seeded from a news article and words chosen using random atmospheric noise
#!/usr/bin/ruby
require 'net/http'
require 'nokogiri'
require 'rss'
require 'open-uri'
google_news_query = 'http://news.google.com/news/feeds?q=apologize&output=rss'
google_news_result_url = ''
wordlist = Array.new
open(google_news_query) do |rss|
feed = RSS::Parser.parse(rss)
item = feed.items[0]
google_news_result_url = item.link
end
doc = Nokogiri::HTML(open(google_news_result_url))
all_words = ""
doc.traverse{ |node|
if node.text? and not node.text =~/^\s*$/
all_words << node.text
end
}
wordlist = all_words.split("\s")
wordlist.reject!(&:empty?)
wordlist.each { |word| word.gsub! /\W/, "" }
random_output = ""
random_data = open("http://www.random.org/integers/?num=4&min=0&max=#{wordlist.length}&col=1&base=10&format=plain&rnd=new") { |data|
generated_number = data.read
random_output << generated_number.chomp
}
random_numbers = random_output.split(/\s/).map(&:to_i)
puts "#{wordlist[random_numbers[0]]} #{wordlist[random_numbers[1]]} #{wordlist[random_numbers[2]]} #{wordlist[random_numbers[3]]}"
@bknowles
Copy link

Hmm. Doesn't look like this code has been passed through rubocop. I'll fix that and re-post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment