A simple and authentic online lucky draw

A simple and authentic online lucky draw

Cryptographic hash and salt from future signal


1 min read

Application senario

We want to organize an online lucky draw on social media. Users who post photos with a certain hash tags can join the lucky draw. The questions is, how to conduct a fair and authentic lucky draw?

One can do a live stream and incorporate some real time signals (e.g. TV new in backdrop) to show the audience of the process. However, this is resource consuming.

We want to do a simple random choice from a list of names. The result needs to depend on a signal that is publicly accessible and verifiable. One method is to use a cryptographic hash function to generate this random number given a salt from public future signal.

The code

Here is the reference implementation:

import hashlib

# Modify this to be real list of users
ls = '''
user 1
user 2
user 3

# Modify this to incorporate a future signal
salt = '43'

a = [x for x in ls.split('\n') if x]
l = len(a)
print(a, l)
s = ''.join(a) + salt
r = hashlib.sha256(s.encode('utf-8'))
h = r.hexdigest()
i = int(h, 16)
print(i % l)
print(a[i % l])

In our lucky draw, we selected the two digits after decimal point of BitCoin USD closing price of 10pm-11pm trading window, using coinmarketcap as the source of truth.