0

I have a game( rock Paper Scissors). How to encrypt the answer of the computer using HMAC (SHA-2 or SHA-3) and at the end of the game compare with the answer of the user?

import random
import hmac
import hashlib
import base64

options = {"r": "rock", "p": "paper", "s": "scissors"}

while True:
    user = input('Choose r for rock, p for paper, s for scissors or q to quit: ')
    user = user.lower()

    if user == 'q':
        break
    if user not in options.keys():
        continue

    choice = random.choice(list(options.keys()))
    print('Computer picked:', options[choice])

    if choice == user:
        print('You tie against the computer\n')
    elif (user, choice) in (("r", "s"), ("p", "r"), ("s", "p")):
        print('You win against the computer\n')
    else:
        print('You lose against the computer\n')
jeremysprofile
  • 10,028
  • 4
  • 33
  • 53
Pluseg
  • 21
  • 4
  • 1
    Why do you need to encrypt it? – bereal Dec 11 '18 at 07:16
  • And if you really can come up with a reason, the hashing is trivial, but a significant challenge is how to encode (essentially, garble) the input so that an eavesdropper cannot simply observe the game for a few rounds and figure out which hash corresponds to which outcome. – tripleee Dec 11 '18 at 07:24
  • Check if the computer is cheating.And this is my assignment from the teacher – Pluseg Dec 11 '18 at 07:26
  • What do you mean by "computer is cheating", and how does encryption help? Clarify the assignment with the teacher. In any case, usage of HMAC in Python is documented [here](https://docs.python.org/3.7/library/hmac.html). – bereal Dec 11 '18 at 07:36
  • As I understand it, hmac is needed to encrypt the response of the computer. In the code, you need to generate a computer response from the arguments passed (1 a z ...), encrypt it with hmac, and then display After the user has entered his answer, we decrypt the answer, compare it with the user's answer, determine who will win – Pluseg Dec 11 '18 at 07:47
  • HMAC is not encryption algorithm, it's an authenticated hash (signature). You cannot decrypt it, you can only verify the message consistency. – bereal Dec 11 '18 at 08:13
  • And how can you create a hash and check the sequence of computer response messages? – Pluseg Dec 11 '18 at 08:24

0 Answers0