Skip to content

Instantly share code, notes, and snippets.

@jnturton
Created September 8, 2021 11:06
Show Gist options
  • Save jnturton/9210be28c885ef25d7b76b06a2ef1b3a to your computer and use it in GitHub Desktop.
Save jnturton/9210be28c885ef25d7b76b06a2ef1b3a to your computer and use it in GitHub Desktop.
JSON obfuscator
#!/usr/bin/env python3
'''
Uses the IJSON library to parse JSON from stdin, replacing strings with salted
hashes that are internally consistent (i.e. "cat" is always replaced with the
same hash). Numbers are not modified. The output is finally writtent to
stdout.
'''
import sys
import logging
from json import dump
from hashlib import sha1
import ijson
from random import randbytes
logging.basicConfig(level=logging.INFO)
salt = randbytes(10)
logging.info(f'Hashing JSON strings with SHA-1 and a salt of {salt.hex()}')
def hashing_parser(basic_parser):
for event, value in basic_parser:
if type(value) is str:
value = sha1(salt + value.encode('utf-8')).hexdigest()
yield event, value
basic_parser = ijson.basic_parse(sys.stdin)
hp = hashing_parser(basic_parser)
item_stream = ijson.items(ijson.parse(hp), '')
for item in item_stream:
dump(item, sys.stdout, indent=2)
logging.info('Finished writing obfuscated JSON to stdout.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment