from random import randint

def sysrand(sides=6, rolls=5):
	return ''.join(map(str, [randint(1, sides) for i in range(rolls)]))

def randorg(sides=6, rolls=5):
	raise NotImplemented

def generate(suggestions=1, words=6, apikey=''):
	with open('diceware.wordlist.asc.txt', 'r') as f:
		wordlist = dict([map(str.strip, line.split()) for line in f if line.strip() != ''])

	for i in range(suggestions):
		password = []

		getkey = randorg if apikey else sysrand

		while len(password) < words:
			key = None
			while key not in wordlist:
				key = getkey()

			password.append(wordlist[key])

		yield ' '.join(password)
from __future__ import print_function

from httplib import HTTPSConnection
from random import randint
from uuid import uuid4

import json, sys


def sysrand(suggestions, words, rolls=5, sides=6, **kwargs):
    print('sysrand', file=sys.stderr)

    for i in range(suggestions):
        yield [''.join(map(str, [randint(1, sides) for i in range(rolls)])) for j in range(words)]


def randorg(suggestions, words, rolls=5, sides=6, apiKey=''):
    conn = HTTPSConnection('api.random.org')
    
    body = json.dumps({
        'jsonrpc': '2.0',
        'id': str(uuid4()),
        'method': 'generateIntegers',
        'params': {
            'apiKey': apiKey,
            'n' : rolls * words * suggestions,
            'min': 1,
            'max': sides
        }
    })

    headers = {
        'Content-Type': 'raw'
    }

    conn.request('POST', '/json-rpc/1/invoke', body, headers)
    resp = conn.getresponse()
    data = json.loads(resp.read())

    conn.close()

    digits = map(str, data['result']['random']['data'])

    for i in range(suggestions):
        start = i * words * rolls
        yield [''.join(digits[start + (j * rolls):start + ((j + 1) * rolls)]) for j in range(words)]


def generate(suggestions=1, words=6, apikey=''):
    with open('diceware.wordlist.asc.txt', 'r') as f:
        wordlist = dict([map(str.strip, line.split()) for line in f if line.strip() != ''])

    getkey = randorg if apikey else sysrand

    for keys in getkey(suggestions, words, apiKey=apikey):
        yield ' '.join([wordlist[k] for k in keys])

Add random.org support for generating keys
Add random.org support for generating keys

* Replace tabs with spaces (d'oh!)
* Implement random key functions as generators so that we can retrieve
  numbers necessary for all suggestions in one request from random.org
  without burning through requests
* Send a request to the random.org API and parse the response into an
  array of keys

+ from __future__ import print_function
+ 
+ from httplib import HTTPSConnection
  from random import randint
+ from uuid import uuid4
  
+ import json, sys
- def sysrand(sides=6, rolls=5):
- 	return ''.join(map(str, [randint(1, sides) for i in range(rolls)]))
  
- def randorg(sides=6, rolls=5):
- 	raise NotImplemented
+ 
+ def sysrand(suggestions, words, rolls=5, sides=6, **kwargs):
+     print('sysrand', file=sys.stderr)
+ 
+     for i in range(suggestions):
+         yield [''.join(map(str, [randint(1, sides) for i in range(rolls)])) for j in range(words)]
+ 
+ 
+ def randorg(suggestions, words, rolls=5, sides=6, apiKey=''):
+     conn = HTTPSConnection('api.random.org')
+     
+     body = json.dumps({
+         'jsonrpc': '2.0',
+         'id': str(uuid4()),
+         'method': 'generateIntegers',
+         'params': {
+             'apiKey': apiKey,
+             'n' : rolls * words * suggestions,
+             'min': 1,
+             'max': sides
+         }
+     })
+ 
+     headers = {
+         'Content-Type': 'raw'
+     }
+ 
+     conn.request('POST', '/json-rpc/1/invoke', body, headers)
+     resp = conn.getresponse()
+     data = json.loads(resp.read())
+ 
+     conn.close()
+ 
+     digits = map(str, data['result']['random']['data'])
+ 
+     for i in range(suggestions):
+         start = i * words * rolls
+         yield [''.join(digits[start + (j * rolls):start + ((j + 1) * rolls)]) for j in range(words)]
+ 
  
  def generate(suggestions=1, words=6, apikey=''):
- 	with open('diceware.wordlist.asc.txt', 'r') as f:
+     with open('diceware.wordlist.asc.txt', 'r') as f:
- 		wordlist = dict([map(str.strip, line.split()) for line in f if line.strip() != ''])
+         wordlist = dict([map(str.strip, line.split()) for line in f if line.strip() != ''])
  
+     getkey = randorg if apikey else sysrand
- 	for i in range(suggestions):
- 		password = []
  
- 		getkey = randorg if apikey else sysrand
+     for keys in getkey(suggestions, words, apiKey=apikey):
+         yield ' '.join([wordlist[k] for k in keys])
  
- 		while len(password) < words:
- 			key = None
- 			while key not in wordlist:
- 				key = getkey()
- 
- 			password.append(wordlist[key])
- 
- 		yield ' '.join(password)
Add random.org support for generating keys
## Code Before:
from random import randint

def sysrand(sides=6, rolls=5):
	return ''.join(map(str, [randint(1, sides) for i in range(rolls)]))

def randorg(sides=6, rolls=5):
	raise NotImplemented

def generate(suggestions=1, words=6, apikey=''):
	with open('diceware.wordlist.asc.txt', 'r') as f:
		wordlist = dict([map(str.strip, line.split()) for line in f if line.strip() != ''])

	for i in range(suggestions):
		password = []

		getkey = randorg if apikey else sysrand

		while len(password) < words:
			key = None
			while key not in wordlist:
				key = getkey()

			password.append(wordlist[key])

		yield ' '.join(password)
## Instruction:
Add random.org support for generating keys
## Code After:
from __future__ import print_function

from httplib import HTTPSConnection
from random import randint
from uuid import uuid4

import json, sys


def sysrand(suggestions, words, rolls=5, sides=6, **kwargs):
    print('sysrand', file=sys.stderr)

    for i in range(suggestions):
        yield [''.join(map(str, [randint(1, sides) for i in range(rolls)])) for j in range(words)]


def randorg(suggestions, words, rolls=5, sides=6, apiKey=''):
    conn = HTTPSConnection('api.random.org')
    
    body = json.dumps({
        'jsonrpc': '2.0',
        'id': str(uuid4()),
        'method': 'generateIntegers',
        'params': {
            'apiKey': apiKey,
            'n' : rolls * words * suggestions,
            'min': 1,
            'max': sides
        }
    })

    headers = {
        'Content-Type': 'raw'
    }

    conn.request('POST', '/json-rpc/1/invoke', body, headers)
    resp = conn.getresponse()
    data = json.loads(resp.read())

    conn.close()

    digits = map(str, data['result']['random']['data'])

    for i in range(suggestions):
        start = i * words * rolls
        yield [''.join(digits[start + (j * rolls):start + ((j + 1) * rolls)]) for j in range(words)]


def generate(suggestions=1, words=6, apikey=''):
    with open('diceware.wordlist.asc.txt', 'r') as f:
        wordlist = dict([map(str.strip, line.split()) for line in f if line.strip() != ''])

    getkey = randorg if apikey else sysrand

    for keys in getkey(suggestions, words, apiKey=apikey):
        yield ' '.join([wordlist[k] for k in keys])
