Last active
January 26, 2026 02:48
-
-
Save timsavage/f4fb4ceca1042af1ddde98109882f291 to your computer and use it in GitHub Desktop.
Python script for generating random bytes for a go program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """ | |
| Simple script to generate formated random bytes for a go program. | |
| """ | |
| import argparse | |
| import random | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Generate a random bytes for go") | |
| parser.add_argument( | |
| "NUM_BYTES", type=int, choices=(16, 32, 64, 96, 128), | |
| help="Number of bytes to generate" | |
| ) | |
| parser.add_argument( | |
| "-s", "--split", type=int, choices=(8, 16, 32), default=16, | |
| help="Number of bytes to split each line into" | |
| ) | |
| parser.add_argument( | |
| "-v", "--var", type=str, default="myBytes", | |
| help="Name of the variable to put bytes into" | |
| ) | |
| args = parser.parse_args() | |
| hex_bytes = [f"0x{b:02X}" for b in random.randbytes(args.NUM_BYTES)] | |
| print(f"var {args.var} = []byte{{", end="") | |
| for idx, byte in enumerate(hex_bytes): | |
| if idx % args.split == 0: | |
| print("\n ", end="") | |
| print(f"{byte}, ", end="") | |
| print("\n}") | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment