add JSON output option
This commit is contained in:
@@ -25,7 +25,7 @@
|
|||||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
import string, random
|
import string, random, json
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
MASTER_KEY_SRC = 'master-key.txt'
|
MASTER_KEY_SRC = 'master-key.txt'
|
||||||
@@ -47,6 +47,10 @@ def main():
|
|||||||
help='use a specific KSV expressed in hexadecimal',
|
help='use a specific KSV expressed in hexadecimal',
|
||||||
metavar='KSV', default=None)
|
metavar='KSV', default=None)
|
||||||
|
|
||||||
|
parser.add_option('-j', '--json', action='store_true',
|
||||||
|
dest='output_json', default=False,
|
||||||
|
help='output key and KSV as JSON')
|
||||||
|
|
||||||
(options, args) = parser.parse_args()
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
# read the master key file
|
# read the master key file
|
||||||
@@ -65,6 +69,9 @@ def main():
|
|||||||
key = gen_source_key(ksv, key_matrix)
|
key = gen_source_key(ksv, key_matrix)
|
||||||
|
|
||||||
# output the key
|
# output the key
|
||||||
|
if options.output_json:
|
||||||
|
output_json(ksv, key, options.gen_sink)
|
||||||
|
else:
|
||||||
output_human_readable(ksv, key, options.gen_sink)
|
output_human_readable(ksv, key, options.gen_sink)
|
||||||
|
|
||||||
def read_key_file(filelike):
|
def read_key_file(filelike):
|
||||||
@@ -164,14 +171,18 @@ def output_human_readable(ksv, key, is_sink):
|
|||||||
# output the key
|
# output the key
|
||||||
key_strs = map(lambda x: '%014x' % x, key)
|
key_strs = map(lambda x: '%014x' % x, key)
|
||||||
|
|
||||||
print('')
|
print('\n%s Key:' % ('Sink' if is_sink else 'Source'))
|
||||||
if is_sink:
|
|
||||||
print('Sink Key:')
|
|
||||||
else:
|
|
||||||
print('Source Key:')
|
|
||||||
|
|
||||||
print(string.join(map(lambda x: string.join(x, ' '), zip(*[key_strs]*5)), '\n'))
|
print(string.join(map(lambda x: string.join(x, ' '), zip(*[key_strs]*5)), '\n'))
|
||||||
|
|
||||||
|
def output_json(ksv, key, is_sink):
|
||||||
|
"""Print a JSON version of the KSV and key.
|
||||||
|
|
||||||
|
The KSV is a single integer. The key is a list of 40 integers."""
|
||||||
|
|
||||||
|
print(json.dumps(
|
||||||
|
{ 'ksv': ksv, 'key': key, 'type': 'sink' if is_sink else 'source' },
|
||||||
|
sort_keys=True, indent=True))
|
||||||
|
|
||||||
# run the 'main' function if this file is being executed directly
|
# run the 'main' function if this file is being executed directly
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user