Guest User

Untitled

a guest
Nov 30th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. """
  2. Will either compute a passwordmaker style key from `sha256(mp + site +
  3. username)` or open a symmetrically keyed gpg file and lookup a specific
  4. site-key for the username and password. The keyfile must follow the following
  5. format::
  6.  
  7. foobar.com:
  8. username: johnuser
  9. password: megaleet
  10.  
  11. """
  12.  
  13. from argparse import ArgumentParser
  14. import getpass
  15. import hashlib
  16. import math
  17.  
  18. import gnupg
  19. import yaml
  20.  
  21. CHARSET = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  22. "abcdefghijklmnopqrstuvwxyz"
  23. "0123456789`~!@#$%^&*()_-+={}|[]\\:\";\'<>?,./")
  24.  
  25.  
  26. # `copy_url` from lodgeit.py
  27. # :license: 3-Clause BSD
  28. # :authors: 2007-2008 Georg Brandl <georg@python.org>,
  29. # 2006 Armin Ronacher <armin.ronacher@active-4.com>,
  30. # 2006 Matt Good <matt@matt-good.net>,
  31. # 2005 Raphael Slinckx <raphael@slinckx.net>
  32. def clipboard_copy(item):
  33. """Copy the item into the clipboard."""
  34. # try windows first
  35. try:
  36. import win32clipboard
  37. except ImportError:
  38. # then give pbcopy a try. do that before gtk because
  39. # gtk might be installed on os x but nobody is interested
  40. # in the X11 clipboard there.
  41. from subprocess import Popen, PIPE
  42. for prog in 'pbcopy', 'xclip':
  43. try:
  44. client = Popen([prog], stdin=PIPE)
  45. except OSError:
  46. continue
  47. else:
  48. client.stdin.write(item)
  49. client.stdin.close()
  50. client.wait()
  51. break
  52. else:
  53. try:
  54. import pygtk
  55. pygtk.require('2.0')
  56. import gtk
  57. import gobject
  58. except ImportError:
  59. return
  60. gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD).set_text(item)
  61. gobject.idle_add(gtk.main_quit)
  62. gtk.main()
  63. else:
  64. win32clipboard.OpenClipboard()
  65. win32clipboard.EmptyClipboard()
  66. win32clipboard.SetClipboardText(item)
  67. win32clipboard.CloseClipboard()
  68.  
  69.  
  70. def rstr2any(input, encoding, trim=True):
  71. """Convert a raw string to an arbitrary string encoding. Set trim
  72. to false for keeping leading zeros
  73.  
  74. """
  75. divisor = len(encoding)
  76. remainders = []
  77.  
  78. # Convert to an array of 16-bit big-endian values, forming the dividend
  79. dividend = []
  80. # pad this
  81. while len(dividend) < math.ceil(len(input) / 2):
  82. dividend.append(0)
  83. for i in range(len(dividend)):
  84. dividend[i] = (ord(input[i * 2]) << 8) | ord(input[i * 2 + 1])
  85. # Repeatedly perform a long division. The binary array forms the dividend,
  86. # the length of the encoding is the divisor. Once computed, the quotient
  87. # forms the dividend for the next step. We stop when the dividend is zero.
  88. # All remainders are stored for later use.
  89. while len(dividend) > 0:
  90. quotient = []
  91. x = 0
  92. for i in range(len(dividend)):
  93. x = (int(x) << 16) + dividend[i]
  94. q = math.floor(x / divisor)
  95. x -= q * divisor
  96. if len(quotient) > 0 or q > 0:
  97. quotient.append(q)
  98. remainders.append(x)
  99. dividend = quotient
  100.  
  101. # Convert the remainders to the output string
  102. output = ""
  103. for i in range(len(remainders) - 1, 0, -1):
  104. output += encoding[int(remainders[i])]
  105. return output
  106.  
  107.  
  108. def main():
  109. parser = ArgumentParser()
  110. parser.add_argument('-f', '--file',
  111. help='GPG file to use for looking up keys',
  112. type=file)
  113. parser.add_argument('-u', '--user',
  114. help='Username default',
  115. default=getpass.getuser())
  116. mutex_group = parser.add_mutually_exclusive_group()
  117. mutex_group.add_argument('-l', '--list', action='store_true')
  118. mutex_group.add_argument('-g', '--generate')
  119. mutex_group.add_argument('-s', '--select')
  120. namespace = parser.parse_args()
  121.  
  122. if namespace.list or namespace.select:
  123. symmetric_phrase = getpass.getpass('passphrase: ')
  124. gpg = gnupg.GPG()
  125. crypt = gpg.decrypt_file(namespace.file, passphrase=symmetric_phrase)
  126. key_data = yaml.load(crypt.data)
  127. if namespace.list:
  128. print key_data.keys()
  129. return
  130. elif namespace.select:
  131. # TODO:dc: check for the key in key_data
  132. username = key_data[namespace.select].get('username', '')
  133. pass_ = key_data[namespace.select].get('password', '')
  134. else:
  135. mp = getpass.getpass('master key: ')
  136. site = raw_input('site: ')
  137. username = namespace.user
  138. hash_ = hashlib.sha256(mp + site + username).digest()
  139. pass_ = rstr2any(hash_, CHARSET)[:20]
  140.  
  141. clipboard_copy(pass_)
  142. print username
  143.  
  144. if __name__ == '__main__':
  145. main()
Add Comment
Please, Sign In to add comment