石头纸剪刀HMAC(SHA-3或SHA-2)加密

时间:2018-12-10 21:18:47

标签: python hmac sha2 sha-3

我有一个游戏(剪刀石头布)。 如何使用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')

1 个答案:

答案 0 :(得分:0)

目前尚不清楚您实际上要使用哪种算法,因为SHA-2和SHA-3都分别由5种算法组成(如果我没记错的话),但是它们都很简单:

def hash(data):
    return hashlib.sha1(data).digest()

其他algs:

def hash(data):
    return hashlib.sha224(data).digest()
def hash(data):
    return hashlib.sha256(data).digest()
def hash(data):
    return hashlib.sha384(data).digest()
def hash(data):
    return hashlib.sha512(data).digest()

现在我意识到这个答案有多大了。好吧,我希望它能对某人有所帮助...