mmgen-passchg 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C) 2013 by 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-passchg: Change a mmgen deterministic wallet's passphrase, label or
  20. hash preset
  21. """
  22. import sys
  23. import mmgen.Opts as Opts
  24. from mmgen.utils import *
  25. from mmgen.config import *
  26. help_data = {
  27. 'desc': """Change the passphrase, hash preset or label of a {}
  28. deterministic wallet""".format(proj_name),
  29. 'usage': "[opts] [filename]",
  30. 'options': """
  31. -h, --help Print this help message
  32. -d, --outdir d Specify an alternate directory 'd' for output
  33. -H, --show-hash-presets Show information on available hash presets
  34. -k, --keep-old-passphrase Keep old passphrase (use when changing hash
  35. strength or label only)
  36. -L, --label l Change the wallet's label to 'l'
  37. -p, --hash-preset p Change scrypt.hash() parameters to preset 'p'
  38. (default: '{}')
  39. -P, --passwd-file f Get new passphrase from file 'f'
  40. -v, --verbose Produce more verbose output
  41. NOTE: The key ID will change if either the passphrase or hash preset
  42. are changed
  43. """.format(hash_preset)
  44. }
  45. short_opts = "hd:HkL:p:P:v"
  46. long_opts = "help","outdir=","show_hash_presets","keep_old_passphrase",\
  47. "label=","hash_preset=","passwd_file=","verbose"
  48. opts,cmd_args = Opts.process_opts(sys.argv,help_data,short_opts,long_opts)
  49. if 'show_hash_presets' in opts: show_hash_presets()
  50. set_if_unset_and_typeconvert(opts,(('hash_preset',hash_preset,'str'),))
  51. check_opts(opts,('hash_preset','outdir','label'))
  52. if len(cmd_args) != 1:
  53. msg("One input file must be specified")
  54. sys.exit(2)
  55. infile = cmd_args[0]
  56. # Old key:
  57. label,metadata,hash_preset,salt,enc_seed = get_data_from_wallet(infile,opts)
  58. seed_id,key_id = metadata[:2]
  59. # Repeat on incorrect pw entry
  60. prompt = "Enter %spassphrase: " % (""
  61. if 'keep_old_passphrase' in opts else "old ")
  62. while True:
  63. passwd = get_mmgen_passphrase(prompt,{})
  64. key = make_key(passwd, salt, hash_preset)
  65. seed = decrypt_seed(enc_seed, key, seed_id, key_id)
  66. if seed: break
  67. changed = {}
  68. if 'label' in opts:
  69. if opts['label'] != label:
  70. msg("Label changed: '%s' -> '%s'" % (label, opts['label']))
  71. changed['label'] = True
  72. else:
  73. msg("Label is unchanged: '%s'" % (label))
  74. else: opts['label'] = label # Copy the old label
  75. if 'hash_preset' in opts:
  76. if hash_preset != opts['hash_preset']:
  77. msg("Hash preset has changed (%s -> %s)" %
  78. (hash_preset, opts['hash_preset']))
  79. changed['preset'] = True
  80. else:
  81. msg("Hash preset is unchanged")
  82. else:
  83. opts['hash_preset'] = hash_preset
  84. if 'keep_old_passphrase' in opts:
  85. msg("Keeping old passphrase by user request")
  86. else:
  87. new_passwd = get_new_passphrase("new passphrase", opts)
  88. if new_passwd == passwd:
  89. msg("Passphrase is unchanged")
  90. else:
  91. msg("Passphrase has changed")
  92. passwd = new_passwd
  93. changed['passwd'] = True
  94. if 'preset' in changed or 'passwd' in changed: # Update key ID, salt
  95. msg("Will update salt and key ID")
  96. from hashlib import sha256
  97. from Crypto import Random
  98. salt = sha256(salt + Random.new().read(128)).digest()[:salt_len]
  99. key = make_key(passwd, salt, opts['hash_preset'])
  100. new_key_id = make_chksum_8(key)
  101. msg("Key ID changed: %s -> %s" % (key_id,new_key_id))
  102. key_id = new_key_id
  103. enc_seed = encrypt_seed(seed, key, opts)
  104. elif not 'label' in changed:
  105. msg("Data unchanged. No file will be written")
  106. sys.exit(2)
  107. write_wallet_to_file(seed, passwd, key_id, salt, enc_seed, opts)