Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. #!/usr/bin/python2
  2.  
  3. import hashlib, getpass, random, argparse, json, sys
  4.  
  5. p = 0xD34E833410143D2582BF1F863E468A19DDDEBF15648402111C49414418A3256C6E3AA7B2FAC164DD1F5113D4E45E6D24D605F7D580E5F8233119C9D6103F6F2ED75C4D4CA7E9B73090260CB0D80029C194C2A81AE71732C393ACF6B2B10BC612CDF1C02F1F522B25633E26EB0AEE97D8086FF2397CADE66017213D6D2E9E441E939B888089E9C5EE8D4B797E5F5D5C2B35149C23138130E046174625F1449A481493A4C45BD8D508102E891E68141B4469E0624F4A6D615427C13FAAC8E2AE203E4631DFD368A83838A201716CEFC811EE0CB17C9C0B5464C654FFE97B1CCC868D71080EA0AD57709FE89DD39BA314B82E687AED27498377D2049891F14FDF03
  6.  
  7. prompt = getpass.getpass
  8. # If you want the password prompt to be in the clear, uncomment this next line.
  9. #prompt = raw_input
  10.  
  11. try:
  12.     import gmpy
  13.     assert gmpy.is_prime(p) and gmpy.is_prime(p // 2)
  14. except:
  15.     print "Couldn't verify primality!"
  16.  
  17. if len(sys.argv) != 2 or sys.argv[1] not in ("1", "2"):
  18.     print "Usage: %s [1|2]" % sys.argv[0]
  19.     print
  20.     print "For the first phase of SPEKE, run with argument 1."
  21.     print "This will write out a file ./secrets"
  22.     print "For the second phase, run with the path to ./secrets as an argument."
  23.     exit(1)
  24.  
  25. if sys.argv[1] == "1":
  26.     password  = prompt("        Entropy: ")
  27.     password2 = prompt("Entropy (again): ")
  28.     assert password == password2
  29.     g = int(hashlib.sha512(password).hexdigest(), 16)
  30.     a = random.SystemRandom().randint(1, p)
  31.     with open("secrets", "w") as f:
  32.         print >>f, json.dumps({"g": g, "a": a}, indent=2)
  33.     ga = pow(g, a, p)
  34.     print
  35.     print "g^a = 0x%x" % ga
  36. else:
  37.     with open("secrets") as f:
  38.         secrets = json.load(f)
  39.     gb = input("Other party's g^a: ")
  40.     gab = pow(gb, secrets["a"], p)
  41.     symmetric_secret = hashlib.sha512(str(gab)).hexdigest()[:32]
  42.     confirmation = hashlib.sha512(symmetric_secret).hexdigest()[:32]
  43.     print "Symmetric secret:", symmetric_secret
  44.     print "Confirmation:", confirmation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement