Created
September 15, 2013 18:31
-
-
Save scvnc/6573229 to your computer and use it in GitHub Desktop.
Adds an extra layer of tinfoil to google's two-factor authentication backup. Google provides a list of security codes to log in to your account if you cannot obtain access to your device which satisfies two-factor authentication. They suggest to generate this code list and print them out to keep in your wallet or something of the like. This modu…
This file contains 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
#!/usr/bin/env python | |
import re | |
def code_parser(file_text): | |
""" Returns a list (as numbers) of all the google security codes | |
found in a text file. """ | |
codes = re.findall('[0-9]{3} [0-9]{2} [0-9]{3}', file_text) | |
for idx,code in enumerate(codes): | |
codes[idx] = int(code.replace(' ','')) | |
return codes | |
def normalize_code(code): | |
""" Converts a number or string to be | |
formatted like a google security code. """ | |
strCode = str(code) | |
codeLen = len(strCode) | |
while len(strCode) < 8: | |
strCode = '0' + strCode | |
strCode = "%s %s %s" % (strCode[:3], strCode[3:5], strCode[5:8]) | |
return strCode | |
def obfuscate_number(number): | |
# Change me to your own formula | |
return number - 12345 | |
if __name__ == "__main__": | |
import sys | |
# | |
# Processing of file if path provided on the command line | |
# | |
if len(sys.argv) == 2: | |
import os | |
codePath = os.path.abspath(sys.argv[1]) | |
with open(codePath, 'r') as codeFile: | |
codeText = codeFile.read() | |
codes = code_parser(codeText) | |
for code in codes: | |
code = obfuscate_number(code) | |
code = normalize_code(code) | |
print code | |
exit() | |
# | |
# Run tests when no command line arguments provided | |
# | |
import unittest | |
class TestNormalizer(unittest.TestCase): | |
def test_that_it_correctly_spaces_out_the_code_groups(self): | |
text = "12312123" | |
normalizedCode = normalize_code(text) | |
self.assertTrue('123 12 123' == normalizedCode) | |
def test_that_it_appends_zeros_when_code_is_too_short(self): | |
code = 45 | |
normalizedCode = normalize_code(code) | |
self.assertTrue('000 00 045' == normalizedCode) | |
class TestParser(unittest.TestCase): | |
def setUp(self): | |
self.testContents = """ | |
1. 903 32 392 6. 303 30 023 | |
2. 039 23 932 7. 303 20 455 """ | |
self.codeList = code_parser(self.testContents) | |
def test_that_it_converts_the_codes_into_numbers(self): | |
for code in [90332392, 30330023, 3923932, 30320455]: | |
self.assertTrue(code in self.codeList) | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment