hf mf fchk - output style
[RRG-proxmark3.git] / tools / pm3_key_file_diff.py
blob4069d668456ffcfaff0ceaa9c480c762a6f8c17e
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
4 ########################################################################
6 # Copyright 2018 Gerhard Klostermeier
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ########################################################################
23 # Usage: ./key-file-diff.py <file A> <file B>
25 ########################################################################
27 # Info:
28 # - Read two key files and show the keys that are in file B but not in A.
29 # - Keys must be 12 hex characters long and at the beginning of a line.
31 ########################################################################
34 import re
37 def main(args):
38 """
39 Read two key files and show the keys that are in file B but not in A.
40 :param args: Path to two key files A and B (positional shell parameters).
41 :return: 0 if everything went fine.
42 """
43 key_file_a = args[1]
44 key_file_b = args[2]
46 with open(key_file_a, 'r') as f:
47 keys_a = parse_keys(f)
48 with open(key_file_b, 'r') as f:
49 keys_b = parse_keys(f)
51 # Show all keys that are in B but not in A.
52 keys_diff = keys_b.difference(keys_a)
53 for key in keys_diff:
54 print(key)
56 return 0
59 def parse_keys(file):
60 """
61 Parse keys from a file and return them as a set.
62 Keys must be 12 hex characters long and at the beginning of a line.
63 :param file: Path to a file containing keys.
64 :return: A set of keys read from the file.
65 """
66 keys = set()
67 key_regex = re.compile('^[0-9a-fA-F]{12}')
68 for line in file:
69 key = key_regex.match(line)
70 try:
71 key = key.group(0).upper()
72 keys.add(key)
73 except AttributeError:
74 pass
75 return keys
78 if __name__ == '__main__':
79 import sys
80 sys.exit(main(sys.argv))