首页 > 解决方案 > 剪刀石头布 HMAC(SHA-3 或 SHA-2)加密

问题描述

我有一个游戏(石头剪刀布)。如何使用 HMAC(SHA-2 或 SHA-3)加密计算机的答案并在游戏结束时与用户的答案进行比较?

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')

标签: pythonhmacsha2sha-3

解决方案


推荐阅读