Created
July 1, 2020 06:59
-
-
Save drygdryg/b813b3f83b998f0d7faa786735e5bd16 to your computer and use it in GitHub Desktop.
Validating and calculating WPS PIN checksum
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
# -*- coding: utf-8 -*- | |
def checksum(pin): | |
''' | |
Standard WPS checksum algorithm. | |
@pin — A 7 digit pin to calculate the checksum for. | |
Returns the checksum value. | |
''' | |
accum = 0 | |
while pin: | |
accum += (3 * (pin % 10)) | |
pin = int(pin / 10) | |
accum += (pin % 10) | |
pin = int(pin / 10) | |
return ((10 - accum % 10) % 10) | |
if __name__ == '__main__': | |
while True: | |
try: | |
pin = input('7-digit WPS PIN: ')[:7] | |
pin = int(pin) | |
except ValueError: | |
print('Invalid input') | |
continue | |
chs = checksum(pin) | |
print(f'Checksum: {chs}\n8-digit PIN: {(str(pin) + str(chs)).zfill(8)}\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment