From 1e52ac21e4a31cee92bc6f0b328e30b6e9810e66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Tempel?= Date: Tue, 14 Jan 2020 11:00:21 +0100 Subject: [PATCH] tools: add tool for generating random hexadecimal values This tool generates a random hexadecimal value of a given maximum size. This is useful for generating random canary values during compile-time for the ssp module which currently uses a constant value. --- dist/tools/randhex/README.md | 7 +++++++ dist/tools/randhex/randhex.py | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 dist/tools/randhex/README.md create mode 100755 dist/tools/randhex/randhex.py diff --git a/dist/tools/randhex/README.md b/dist/tools/randhex/README.md new file mode 100644 index 0000000000..7a23a413dd --- /dev/null +++ b/dist/tools/randhex/README.md @@ -0,0 +1,7 @@ +randhex.py +---------- + +Usage: `randhex.py ` + +This script generates a random hexadecimal value of the given maximum +size in bits. If `MAXBITS` is not specified it defaults to 64. diff --git a/dist/tools/randhex/randhex.py b/dist/tools/randhex/randhex.py new file mode 100755 index 0000000000..270252a690 --- /dev/null +++ b/dist/tools/randhex/randhex.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 + +# +# Copyright (C) 2020 Sören Tempel +# +# This file is subject to the terms and conditions of the GNU Lesser +# General Public License v2.1. See the file LICENSE in the top level +# directory for more details. +# + +import sys +import secrets + +if len(sys.argv) >= 2: + maxbits = int(sys.argv[1]) +else: + maxbits = 64 + +maxval = 2 ** maxbits +print(hex(secrets.randbelow(maxval)))