Skip to content

Diffie-Hellman coding challenge for secret information exchange using public/private keys

License

Notifications You must be signed in to change notification settings

Liam-Deacon/alice_and_bob

Repository files navigation

Python CI Docs CI             GitHub issues GitHub issues-closed GitHub pull-requests GitHub pull-requests closed

Alice and Bob information sharing code challenge

Diffie-Hellman coding challenge for secret information exchange using public/private keys

Background

Alice and Bob use the Diffie-Hellman key exchange algorithm to share secret information. Alice and Bob start with prime numbers, pick private keys, generate and share public keys, finally they then generate a shared secret key.

Your code module should take in two prime numbers, p and g and output the value of p and g, the private key a for Alice and b for Bob. Finally your program should print out the Shared Secret key for Alice and Bob.

If your implementation is correct, the Shared Secret keys should match.

Note: This challenge requires you to perform calculations on large numbers. Further information can be found at https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange

Code requirements

  • Ensure that your code is clean and uses good practice (e.g. error handling) and is commented well.
  • Ensure that your code can work cross platform and across different versions of Python.
  • Feel free to add as many bells and whistles as you so desired (e.g. Unit Tests)

Running the code

git clone https://github.com/Liam-Deacon/alice_and_bob
cd alice_and_bob
python3 -m alice_and_bob.key_share --help

CLI Example

$ python3 -m alice_and_bob.key_share -p 61 -g 53
Shared secret key: 60

It is also possible to specify the number of bits used for the generated private keys:

$ python3 -m alice_and_bob.key_share -p 88937 -g 104729  --bits=2048
Shared secret key: 39885

Python Example

>>> from alice_and_bob.key_share import PrivateKey, diffie_hellman, main as code

# Lets try using PrivateKey class and diffie_helman function to generate public and private keys from 2 primes
>>> PrivateKey.DEFAULT_BITS = 8  # Should be using at least 2048 bits in real life scenario
>>> diffie_helman(p=2, g=3)  # the primes used in Diffie-Hellman algorithm should be much larger
Keys(public_key_pair=(1, 1), private_key_a=16353971836403060303, private_key_b=9351112014020560943)

# output from diffie_hellman() function is a namedtuple, i.e.
>>> keys = diffie_hellman(p=16353971836403060303, g=9351112014020560943)
>>> keys[0]  # return public keys
(3313824243486361299, 5114973081711194950)
>>> keys[1] != keys[2]  # compare private keys
True

# finally the shared secret key is printed by using the main/code function, which wraps diffie_helman()
>>> code(p=2, g=3)  # only one possibility as primes very small
Shared secret key: 1
>>> code(61, 53)
Shared secret key: 37
>>> code(61, 53)
Shared secret key: 9
>>> code(61, 53)
Shared secret key: 11

Bells & Whistles 🔔

  • Unit tests 🧪
  • High code coverage (see shields above) 🔦
  • Repo badges! 📛
  • GitHub Actions for CI pipeline ⚙️
  • Published as python package to https://PyPI.org 📦
  • Example command line script provided (see CLI example above) 📜

TODO

Future improvements could be:

  • Add linter checks to CI pipeline (PEP8, style, docstrings, code compexity, type checks, etc.)
  • Add web UI and dockerise
  • Use GitHub Actions to auto increment and deploy PyPI releases on successful PR completion into master branch
  • Better documentation (devs will complain it is insufficient regardless - its what we do ;-))

About

Diffie-Hellman coding challenge for secret information exchange using public/private keys

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages