main_passgen.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
  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 .common import *
  23. from .addrlist import AddrIdxList
  24. from .passwdlist import PasswordList
  25. from .wallet import Wallet
  26. from .obj import MMGenPWIDString
  27. pwi = PasswordList.pw_info
  28. opts_data = {
  29. 'sets': [('print_checksum',True,'quiet',True)],
  30. 'text': {
  31. 'desc': f"""
  32. Generate a range or list of passwords from an {g.proj_name} wallet,
  33. mnemonic, seed or brainwallet for the given ID string
  34. """,
  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. -d, --outdir= d Output files to directory 'd' instead of working dir
  40. -e, --echo-passphrase Echo passphrase or mnemonic to screen upon entry
  41. -f, --passwd-fmt= f Generate passwords of format 'f'. Default: {dpf}.
  42. See PASSWORD FORMATS below
  43. -i, --in-fmt= f Input is from wallet format 'f' (see FMT CODES below)
  44. -H, --hidden-incog-input-params=f,o Read hidden incognito data from file
  45. 'f' at offset 'o' (comma-separated)
  46. -O, --old-incog-fmt Specify old-format incognito input
  47. -L, --passwd-len= l Specify length of generated passwords. For defaults,
  48. see PASSWORD FORMATS below. An argument of 'h' will
  49. generate passwords of half 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 (< {dsl}-bit) seed lengths.
  53. -p, --hash-preset= p Use the scrypt hash parameters defined by preset 'p'
  54. for password hashing (default: '{g.dfl_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. """,
  63. 'notes': """
  64. NOTES FOR THIS COMMAND
  65. ID string must be a valid UTF-8 string not longer than {ml} characters and
  66. not containing the symbols '{fs}'.
  67. Password indexes are given as a comma-separated list and/or hyphen-separated
  68. range(s).
  69. Changing either the password format (base32,base58) or length alters the seed
  70. and thus generates a completely new set of passwords.
  71. PASSWORD FORMATS:
  72. {pfi}
  73. EXAMPLES:
  74. Generate ten base58 passwords of length {i58.dfl_len} for Alice's email account:
  75. {g.prog_name} alice@nowhere.com 1-10
  76. Generate ten base58 passwords of length 16 for Alice's email account:
  77. {g.prog_name} --passwd-len=16 alice@nowhere.com 1-10
  78. Generate ten base32 passwords of length {i32.dfl_len} for Alice's email account:
  79. {g.prog_name} --passwd-fmt=b32 alice@nowhere.com 1-10
  80. Generate three BIP39 mnemonic seed phrases of length {i39.dfl_len} for Alice's
  81. Trezor device:
  82. {g.prog_name} --passwd-fmt=bip39 mytrezor 1-3
  83. All passwords are cryptographically unlinkable with each other, including
  84. passwords with the same format but different length, so Alice needn't worry
  85. about inadvertent reuse of private data.
  86. NOTES FOR ALL GENERATOR COMMANDS
  87. {n_pw}
  88. {n_bw}
  89. FMT CODES:
  90. {n_fmt}
  91. """
  92. },
  93. 'code': {
  94. 'options': lambda help_notes,s: s.format(
  95. g=g,pnm=g.proj_name,
  96. dsl=help_notes('dfl_seed_len'),
  97. dpf=PasswordList.dfl_pw_fmt,
  98. ),
  99. 'notes': lambda help_notes,s: s.format(
  100. o=opts,g=g,i58=pwi['b58'],i32=pwi['b32'],i39=pwi['bip39'],
  101. ml=MMGenPWIDString.max_len,
  102. fs="', '".join(MMGenPWIDString.forbidden),
  103. n_pw=help_notes('passwd'),
  104. n_bw=help_notes('brainwallet'),
  105. pfi=help_notes('password_formats'),
  106. n_fmt=help_notes('fmt_codes'),
  107. )
  108. }
  109. }
  110. cmd_args = opts.init(opts_data)
  111. if len(cmd_args) < 2: opts.usage()
  112. pw_idxs = AddrIdxList(fmt_str=cmd_args.pop())
  113. pw_id_str = cmd_args.pop()
  114. from .fileutil import get_seed_file
  115. sf = get_seed_file(cmd_args,1)
  116. pw_fmt = opt.passwd_fmt or PasswordList.dfl_pw_fmt
  117. pw_len = pwi[pw_fmt].dfl_len // 2 if opt.passwd_len in ('h','H') else opt.passwd_len
  118. from .protocol import init_proto
  119. proto = init_proto('btc') # TODO: get rid of dummy proto
  120. PasswordList(
  121. proto = proto,
  122. pw_id_str = pw_id_str,
  123. pw_len = pw_len,
  124. pw_fmt = pw_fmt,
  125. chk_params_only = True )
  126. do_license_msg()
  127. ss = Wallet(sf)
  128. al = PasswordList(
  129. proto = proto,
  130. seed = ss.seed,
  131. pw_idxs = pw_idxs,
  132. pw_id_str = pw_id_str,
  133. pw_len = pw_len,
  134. pw_fmt = pw_fmt )
  135. af = al.get_file()
  136. af.format()
  137. if keypress_confirm('Encrypt password list?'):
  138. af.encrypt()
  139. af.write(binary=True,desc='encrypted password list')
  140. else:
  141. if g.test_suite_popen_spawn and g.platform == 'win':
  142. time.sleep(0.1)
  143. af.write(desc='password list')