Skip to content

Instantly share code, notes, and snippets.

@joshkunz
Created February 5, 2015 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshkunz/ed5c9ab02d05922a8208 to your computer and use it in GitHub Desktop.
Save joshkunz/ed5c9ab02d05922a8208 to your computer and use it in GitHub Desktop.
A new ssn generator
# SSNPad.py - creates a SSN scratch pad. This is used to map one SSN value to
# a random replacement value. Useful to protect a SSN number while preserving
# the format of the data. Reasonably secure if the scratch pad is kept secure.
# This script creates a text file with the following structure:
# XXXXXXXXX|YYYYYYYYY
# where XXXXXXXXX is the source SSN and
# YYYYYYYYY is a psuedo(semi)-random alternate SSN
# Sample:
# SSN numbers are composed of three parts:
# AAA-GG-IIII
# AAA = The Area (used to map to a state, doesn't anymore)
# GG = Group 01-99
# IIII a zero padded number between 0001-9999
# This process takes a specific source Area and Group (i.e. 123-45) and maps it
# to a randomly chosen different replacement Area and Group (i.e. 763-22) and
# then generates a random replacement ID for each source ID.
# A sample of the output:
# 001010001|686075132
# 001010002|686074919
# 001010003|686077114
# 001010004|686078786
import random
from itertools import product
G_SIZE = len(range(1, 899 +1)) * len(range(1, 99+1))
ID_SIZE = len(range(1, 9999+1))
def ssn_str(agid):
(area, group), id = agid
return "{0:0>3}{1:0>2}{2:0>4}".format(area, group, id)
def ssn_pair(ssns):
ssn_a, ssn_b = ssns
return "|".join((ssn_str(ssn_a),
ssn_str(ssn_b)))
def gen_groups():
return product(range(1, 899 + 1), range(1, 99+1))
master_groups = gen_groups()
random_groups = list(gen_groups())
random.shuffle(random_groups)
master_ids = range(1, ID_SIZE+1)
random_ids = random.sample(range(1, ID_SIZE+1), ID_SIZE)
ssn_pairs = map(ssn_pair, zip(product(master_groups, master_ids),
product(random_groups, random_ids)))
for pair in ssn_pairs:
print(pair)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment