|
| 1 | +#!/usr/bin/env python |
| 2 | +# vim:ts=4:sts=4:sw=4:et |
| 3 | +# |
| 4 | +# Author: Hari Sekhon |
| 5 | +# Date: 2020-01-02 17:08:32 +0000 (Thu, 02 Jan 2020) |
| 6 | +# |
| 7 | +# https://github.com/harisekhon/devops-python-tools |
| 8 | +# |
| 9 | +# License: GNU GPL version 2 (this file only), rest of this repo is licensed as per the adjacent LICENSE file |
| 10 | +# |
| 11 | +# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback |
| 12 | +# to help improve or steer this or other code I publish |
| 13 | +# |
| 14 | +# http://www.linkedin.com/in/harisekhon |
| 15 | +# |
| 16 | + |
| 17 | +""" |
| 18 | +
|
| 19 | +Tool to anonymize hex input but keeping the structure of the positions of numbers and digits |
| 20 | +
|
| 21 | +Useful to retain the structure of ID formats |
| 22 | +
|
| 23 | +Reads any given files or standard input and replaces each hex character with an incrementing number of letter (a-f) |
| 24 | +printing to stdout for piping or redirecting to a file as per unix filter command standards |
| 25 | +
|
| 26 | +Works like a standard unix filter program - if no files are passed as arguments or '-' is passed then reads from |
| 27 | +standard input |
| 28 | +
|
| 29 | +""" |
| 30 | + |
| 31 | +from __future__ import absolute_import |
| 32 | +from __future__ import division |
| 33 | +from __future__ import print_function |
| 34 | +#from __future__ import unicode_literals |
| 35 | + |
| 36 | +import os |
| 37 | +import sys |
| 38 | +libdir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'pylib')) |
| 39 | +sys.path.append(libdir) |
| 40 | +try: |
| 41 | + # pylint: disable=wrong-import-position |
| 42 | + from harisekhon import CLI |
| 43 | +except ImportError as _: |
| 44 | + print('module import failed: %s' % _, file=sys.stderr) |
| 45 | + print("Did you remember to build the project by running 'make'?", file=sys.stderr) |
| 46 | + print("Alternatively perhaps you tried to copy this program out without it's adjacent libraries?", file=sys.stderr) |
| 47 | + sys.exit(4) |
| 48 | + |
| 49 | +__author__ = 'Hari Sekhon' |
| 50 | +__version__ = '0.1.0' |
| 51 | + |
| 52 | + |
| 53 | +class HexAnonymize(CLI): |
| 54 | + |
| 55 | + def __init__(self): |
| 56 | + # Python 2.x |
| 57 | + super(HexAnonymize, self).__init__() |
| 58 | + # Python 3.x |
| 59 | + # super().__init__() |
| 60 | + self.preserve_case = False |
| 61 | + self.only_hex_alphas = False |
| 62 | + |
| 63 | + def add_options(self): |
| 64 | + super(HexAnonymize, self).add_options() |
| 65 | + self.add_opt('-c', '--case', action='store_true', help='Preserve case') |
| 66 | + self.add_opt('-o', '--only-hex', action='store_true', |
| 67 | + help='Only replace hex alpha chars (A-F, a-f), otherwise replaces all alphanumerics for safety') |
| 68 | + |
| 69 | + def process_options(self): |
| 70 | + super(HexAnonymize, self).process_options() |
| 71 | + self.preserve_case = self.get_opt('case') |
| 72 | + self.only_hex_alphas = self.get_opt('only_hex') |
| 73 | + |
| 74 | + def hexanonymize(self, filehandle): |
| 75 | + preserve_case = self.preserve_case |
| 76 | + only_hex_alphas = self.only_hex_alphas |
| 77 | + hex_alphas = ['a', 'b', 'c', 'd', 'e', 'f'] |
| 78 | + for line in filehandle: |
| 79 | + integer = 1 |
| 80 | + letter = 'a' |
| 81 | + for char in line: |
| 82 | + if char.isdigit(): |
| 83 | + char = integer |
| 84 | + integer += 1 |
| 85 | + if integer > 9: |
| 86 | + integer = 0 |
| 87 | + elif char.lower() in hex_alphas or (not only_hex_alphas and char.isalpha()): |
| 88 | + if preserve_case and char.isupper(): |
| 89 | + char = letter.upper() |
| 90 | + else: |
| 91 | + char = letter |
| 92 | + letter = chr(ord(char) + 1) |
| 93 | + if letter not in hex_alphas: |
| 94 | + letter = 'a' |
| 95 | + print(char, end='') |
| 96 | + |
| 97 | + |
| 98 | + def run(self): |
| 99 | + if not self.args: |
| 100 | + self.args.append('-') |
| 101 | + for arg in self.args: |
| 102 | + if arg == '-': |
| 103 | + continue |
| 104 | + if not os.path.exists(arg): |
| 105 | + print("'%s' not found" % arg) |
| 106 | + sys.exit(1) |
| 107 | + for arg in self.args: |
| 108 | + if arg == '-': |
| 109 | + self.hexanonymize(sys.stdin) |
| 110 | + else: |
| 111 | + with open(arg) as filehandle: |
| 112 | + self.hexanonymize(filehandle) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + HexAnonymize().main() |
0 commit comments