Skip to content

Instantly share code, notes, and snippets.

@mtschirs
Created May 16, 2020 16:02
Show Gist options
  • Save mtschirs/11cc121d9ae1cb7d30953c22f17c540a to your computer and use it in GitHub Desktop.
Save mtschirs/11cc121d9ae1cb7d30953c22f17c540a to your computer and use it in GitHub Desktop.
Compute the current CuidAR security code. @see https://www.argentina.gob.ar/aplicaciones/coronavirus
#!/usr/bin/python3
import base64
import hmac
import struct
import time
def hotp(key, counter, digits=6, digest='sha1'):
key = base64.b32decode(key.upper() + '=' * ((8 - len(key)) % 8))
counter = struct.pack('>Q', counter)
mac = hmac.new(key, counter, digest).digest()
offset = mac[-1] & 0x0f
binary = struct.unpack('>L', mac[offset:offset+4])[0] & 0x7fffffff
return str(binary)[-digits:].rjust(digits, '0')
def totp(key, time_step=30, digits=6, digest='sha1'):
return hotp(key, int(time.time() / time_step), digits, digest)
val = int(totp("MFZWIRTHONSFGZZTGRDUQII=", 1500, 8))
print('{:X}{:X}{:X}'.format((val & 3840) // 256, (val & 240) // 16, val & 15))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment