main_passgen.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2017 Philemon <mmgen-py@yandex.com>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. mmgen-passgen: Generate a series or range of passwords from an MMGen
  20. deterministic wallet
  21. """
  22. from mmgen.common import *
  23. from mmgen.crypto import *
  24. from mmgen.addr import PasswordList,AddrIdxList
  25. from mmgen.seed import SeedSource
  26. from mmgen.obj import MMGenPWIDString
  27. dfl_len = {
  28. 'b58': PasswordList.pw_info['b58']['dfl_len'],
  29. 'b32': PasswordList.pw_info['b32']['dfl_len']
  30. }
  31. opts_data = lambda: {
  32. 'sets': [('print_checksum',True,'quiet',True)],
  33. 'desc': """Generate a range or list of passwords from an {pnm} wallet,
  34. mnemonic, seed or brainwallet for the given ID string""".format(pnm=g.proj_name),
  35. 'usage':'[opts] [seed source] <ID string> <index list or range(s)>',
  36. 'options': """
  37. -h, --help Print this help message
  38. --, --longhelp Print help message for long options (common options)
  39. -b, --base32 Generate passwords in Base32 format instead of Base58
  40. -d, --outdir= d Output files to directory 'd' instead of working dir
  41. -e, --echo-passphrase Echo passphrase or mnemonic to screen upon entry
  42. -i, --in-fmt= f Input is from wallet format 'f' (see FMT CODES below)
  43. -H, --hidden-incog-input-params=f,o Read hidden incognito data from file
  44. 'f' at offset 'o' (comma-separated)
  45. -O, --old-incog-fmt Specify old-format incognito input
  46. -L, --passwd-len= l Specify length of generated passwords
  47. (default: {d58} chars [base58], {d32} chars [base32]).
  48. An argument of 'h' will generate passwords of half
  49. the default length.
  50. -l, --seed-len= l Specify wallet seed length of 'l' bits. This option
  51. is required only for brainwallet and incognito inputs
  52. with non-standard (< {g.seed_len}-bit) seed lengths
  53. -p, --hash-preset= p Use the scrypt hash parameters defined by preset 'p'
  54. for password hashing (default: '{g.hash_preset}')
  55. -z, --show-hash-presets Show information on available hash presets
  56. -P, --passwd-file= f Get wallet passphrase from file 'f'
  57. -q, --quiet Produce quieter output; suppress some warnings
  58. -r, --usr-randchars=n Get 'n' characters of additional randomness from user
  59. (min={g.min_urandchars}, max={g.max_urandchars}, default={g.usr_randchars})
  60. -S, --stdout Print passwords to stdout
  61. -v, --verbose Produce more verbose output
  62. """.format(
  63. seed_lens=', '.join([str(i) for i in g.seed_lens]),
  64. g=g,pnm=g.proj_name,d58=dfl_len['b58'],d32=dfl_len['b32'],
  65. kgs=' '.join(['{}:{}'.format(n,k) for n,k in enumerate(g.key_generators,1)])
  66. ),
  67. 'notes': """
  68. NOTES FOR THIS COMMAND
  69. ID string must be a valid UTF-8 string not longer than {ml} characters and
  70. not containing the symbols '{fs}'.
  71. Password indexes are given as a comma-separated list and/or hyphen-separated
  72. range(s).
  73. Changing either the password format (base32,base58) or length alters the seed
  74. and thus generates a completely new set of passwords.
  75. EXAMPLE:
  76. Generate ten base58 passwords of length {d58} for Alice's email account:
  77. {g.prog_name} alice@nowhere.com 1-10
  78. Generate ten base58 passwords of length 16 for Alice's email account:
  79. {g.prog_name} -L16 alice@nowhere.com 1-10
  80. Generate ten base32 passwords of length {d32} for Alice's email account:
  81. {g.prog_name} -b alice@nowhere.com 1-10
  82. The three sets of passwords are completely unrelated to each other, so
  83. Alice doesn't need to worry about password reuse.
  84. NOTES FOR ALL GENERATOR COMMANDS
  85. {n_pw}
  86. {n_bw}
  87. FMT CODES:
  88. {n_fmt}
  89. """.format(
  90. o=opts,g=g,d58=dfl_len['b58'],d32=dfl_len['b32'],
  91. ml=MMGenPWIDString.max_len,
  92. fs="', '".join(MMGenPWIDString.forbidden),
  93. n_pw=help_notes('passwd'),
  94. n_bw=help_notes('brainwallet'),
  95. n_fmt='\n '.join(SeedSource.format_fmt_codes().splitlines())
  96. )
  97. }
  98. cmd_args = opts.init(opts_data,add_opts=['b16'])
  99. if len(cmd_args) < 2: opts.usage()
  100. pw_idxs = AddrIdxList(fmt_str=cmd_args.pop())
  101. pw_id_str = cmd_args.pop()
  102. sf = get_seed_file(cmd_args,1)
  103. pw_fmt = ('b58','b32')[bool(opt.base32)]
  104. pw_len = (opt.passwd_len,dfl_len[pw_fmt]/2)[opt.passwd_len in ('h','H')]
  105. PasswordList(pw_id_str=pw_id_str,pw_len=pw_len,pw_fmt=pw_fmt,chk_params_only=True)
  106. do_license_msg()
  107. ss = SeedSource(sf)
  108. al = PasswordList(seed=ss.seed,pw_idxs=pw_idxs,pw_id_str=pw_id_str,pw_len=pw_len,pw_fmt=pw_fmt)
  109. al.format()
  110. if keypress_confirm('Encrypt password list?'):
  111. al.encrypt(desc='password list')
  112. al.write_to_file(binary=True,desc='encrypted password list')
  113. else:
  114. al.write_to_file(desc='password list')