Skip to content

Instantly share code, notes, and snippets.

@TobiX
Last active May 31, 2020 03:55
Show Gist options
  • Save TobiX/0106edd801c6c9f276e1fa920d8b0fd8 to your computer and use it in GitHub Desktop.
Save TobiX/0106edd801c6c9f276e1fa920d8b0fd8 to your computer and use it in GitHub Desktop.
Nintendo 3DS friendcode verification
#!/usr/bin/python3
# Released into the public domain, where possible (http://creativecommons.org/publicdomain/zero/1.0/)
import sys
import re
import hashlib
fc = sys.argv[1]
parts = re.match('^(\d{4})-(\d{4})-(\d{4})$', fc)
if not parts:
print("Wrong format!")
sys.exit(1)
fcint = int("".join(parts.group(1, 2, 3)))
principal = fcint & 0xffffffff
checksum = fcint >> 32
sha1 = hashlib.sha1()
sha1.update(principal.to_bytes(4, byteorder='little'))
calcsum = sha1.digest()[0] >> 1
print(fc, fcint, principal, checksum, calcsum)
assert checksum == calcsum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment