Skip to content

Instantly share code, notes, and snippets.

@vmx
Created May 15, 2024 11:37
Show Gist options
  • Save vmx/57e5545bcefbc200cf0ab77d90197139 to your computer and use it in GitHub Desktop.
Save vmx/57e5545bcefbc200cf0ab77d90197139 to your computer and use it in GitHub Desktop.
Poor man's fr32 padding (replacing the a full byte, not only two bits).
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
# Override every 32 byte with a zero. The input file is modified!
#
# Start e.g. with a file with random bytes:
#
# dd if=/dev/urandom of=/tmp/random.dat count=1 bs=1MiB
#
# And then override ever 32th byte with a zero:
#
# python3 fr32padding.py /tmp/random.dat
#
# You can check if it was correct with hexdump:
#
# hexdump --format '32/1 "%02X" "\n"' /tmp/random.dat
import os, sys
def main(argv=None):
if argv is None:
argv = sys.argv
if len(argv) != 2:
print("Usage: {} <input-file>".format(argv[0]))
filename = argv[1]
file_size = os.stat(filename).st_size
with open(filename, 'r+b') as ff:
while ff.tell() < file_size:
# Move to the 32 bytes further.
ff.seek(31, os.SEEK_CUR)
# Override the byte.
ff.write(b'\x00')
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment