-
-
Save dooglus/8412421 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import hmac | |
import hashlib | |
from sys import argv, exit, stderr | |
def hmac_sha512(key, message): | |
return hmac.new(key, message, hashlib.sha512).hexdigest() | |
def roll(server_seed, client_seed, nonce): | |
# Generate key and message strings. | |
key = '%s:%s:%s' % (nonce, server_seed, nonce) | |
message = '%s:%s:%s' % (nonce, client_seed, nonce) | |
# Generate hmac-sha512 hash. | |
hexdigest = hmac_sha512(key, message) | |
# Convert hexdigest into "lucky" number. | |
for i in xrange(25): | |
lucky = int(hexdigest[5*i:5*i+5], 16) | |
if lucky < 1000000: | |
return lucky/10000.0 | |
# If none of the 25 blocks were suitable, use the last 3 characters. | |
lucky = int(hexdigest[-3:], 16) | |
return lucky/10000.0 | |
def verify_server_seed(server_seed, server_seed_hash): | |
return hashlib.sha256(server_seed).hexdigest() == server_seed_hash | |
def usage(): | |
msg = 'usage: server_hash server_seed client_seed start_nonce [end_nonce]\n' | |
stderr.write(msg) | |
exit(1) | |
def main(): | |
if len(argv) < 5: | |
stderr.write('too few arguments\n') | |
usage() | |
server_hash = argv[1] | |
server_seed = argv[2] | |
client_seed = argv[3] | |
start_nonce = argv[4] | |
if len(argv) > 5: | |
end_nonce = argv[5] | |
else: | |
end_nonce = start_nonce | |
if verify_server_seed(server_seed, server_hash): | |
print 'server seed matches hash' | |
else: | |
print 'server seed does not match hash!' | |
exit(0) | |
print '%5s: %8s' % ('nonce', 'lucky') | |
for nonce in xrange(int(start_nonce), int(end_nonce)+1): | |
print '%5s: %8.4f' %(nonce, roll(server_seed, client_seed, nonce)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment