بتل کد Battle Codeدارد باز می‌شود…

جواب «Make the Code» چالش 3 از جلسه‌ی 10

سؤال

تابع one_time_code(secret, counter) را بنویس: هش secret + str(counter) را بگیر، با int(h, 16) عدد کن و % 1000000. برای راز my-secret و شمارنده‌های 1، 2، 3 چاپ کن round 1: <کد>.

راهنما

h = hashlib.sha256((secret + str(counter)).encode()).hexdigest() و return int(h, 16) % 1000000.

یک جواب درست

import hashlib

def one_time_code(secret, counter):
    h = hashlib.sha256((secret + str(counter)).encode()).hexdigest()
    return int(h, 16) % 1000000

for counter in [1, 2, 3]:
    print("round", counter, ":", one_time_code("my-secret", counter))

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن