Skip to content

Instantly share code, notes, and snippets.

@Earnestly
Created February 20, 2022 17:11
Show Gist options
  • Save Earnestly/7cd5f1a52f4a6d299fca68d25dd6fa6b to your computer and use it in GitHub Desktop.
Save Earnestly/7cd5f1a52f4a6d299fca68d25dd6fa6b to your computer and use it in GitHub Desktop.
#!/bin/python
import os, sys, hashlib
def main():
progname = 'epub-font-obfuscation'
algorithms = {'idpf': 1040, 'adobe': 1024}
if len(sys.argv) > 2:
uid = sys.argv[1]
else:
sys.exit(f'usage: {progname} key algorithm://font...')
for fname in sys.argv[2:]:
for algorithm in algorithms:
if fname.startswith(algorithm + '://'):
a = algorithm
if not a:
sys.exit('unable to determine obfuscation method\n')
fname = fname.removeprefix(a + '://')
if a == 'adobe':
key = bytearray.fromhex(uid.removeprefix('urn:uuid:').replace('-', ''))
else:
key = bytearray(hashlib.sha1(uid.encode()).digest())
s = len(key)
if not os.path.exists(fname):
print(f'{progname}: warning: {fname}: no such file', file=sys.stderr)
continue
with open(fname, 'rb+') as file:
i = 0
size = algorithms[a]
for _ in range(size):
byte = file.read(1)
file.seek(file.tell() - 1)
if byte:
file.write((byte[0] ^ key[i % s]).to_bytes(1, 'little'))
i += 1
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment