#!/usr/bin/env python3 # Python just-dice.com verifier, based on the JavaScript verifiers described on the "Fair?" pages at just-dice.com # # example usage: # python3 just-dice.py ia_zEVRJ_VqfHPDbqGEYm39d6d2yg58_t1brUjO2LwiDBVydO7sKh6Sulw8123ec 7ab7df124145d0fcf931c142aabcca2022f60775d6afa692be114c09ff4408cf 515035004112265853362847 12 # prints the last 10 lucky numbers, starting with roll 12 since the last randomize, which is this bet: https://just-dice.com/roll/23616559 import hashlib, hmac, os.path, sys # parse command line arguments if len(sys.argv) < 5: sys.stderr.write("usage: %s server-seed server-seed-hash client-seed last-roll [roll-count]\n" % (os.path.basename(sys.argv[0]))) exit(-1) serverSeed, serverSeedHash, clientSeed, lastNonce = sys.argv[1], sys.argv[2], sys.argv[3], int(sys.argv[4]) nonceCount = 10 if len(sys.argv) == 6: nonceCount = int(sys.argv[5]) # convert string to binary array def str2bin(string): return bytes(string, 'latin-1') # test server hash if hashlib.sha256(str2bin(serverSeed)).hexdigest() == serverSeedHash: print('Server secret hash is correct.') else: print('Server secret hash check failed!') # calculate and show lucky numbers for nonce in range(lastNonce, lastNonce - nonceCount, -1): hash = hmac.new(str2bin(serverSeed), str2bin(clientSeed + ":" + str(nonce)), hashlib.sha512).hexdigest() i = 0 while True: if i == 25: roll = int(hash[-3:], 16) / 10000 break else: f5p = int(hash[5 * i : 5 + 5 * i], 16) if f5p < 1000000: roll = f5p / 10000 break i += 1 print(str(nonce) + ": " + ('0' + '{:.4f}'.format(roll))[-7:])