Last active
September 3, 2018 05:55
-
-
Save otms61/5e87c62630abbaea537abdf81bcda6fe to your computer and use it in GitHub Desktop.
A command-line tool for google authenticator verify code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import hashlib | |
| import hmac | |
| import time | |
| import struct | |
| import base64 | |
| def compute_code(key): | |
| t = int(time.time()) | |
| ts = t / 30 | |
| ts_packed = struct.pack('>Q', ts) | |
| key_byte = base64.b32decode(key, casefold=True) | |
| hmac_result = bytearray(hmac.new(key_byte, ts_packed, hashlib.sha1).digest()) | |
| offset = hmac_result[19] & 0xf | |
| bin_code = ((hmac_result[offset] & 0x7f) << 24) \ | |
| | ((hmac_result[offset + 1] & 0xff) << 16) \ | |
| | ((hmac_result[offset + 2] & 0xff) << 8) \ | |
| | (hmac_result[offset + 3] & 0xff) | |
| bin_code = bin_code % 10**6 | |
| return bin_code | |
| # [guest@dd2bb0e7f6d0 ~]$ head -n 1 ~/.google_authenticator | |
| # 3NWUJKAV3X5UK36V | |
| key = "3NWUJKAV3X5UK36V" | |
| print compute_code(key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.