lbry-sdk/scripts/encrypt_blob.py

65 lines
1.6 KiB
Python
Raw Normal View History

2017-01-20 21:50:10 +01:00
"""Encrypt a single file using the given key and iv"""
import argparse
import logging
import sys
from twisted.internet import defer
from twisted.internet import reactor
from lbrynet import conf
from lbrynet.core import log_support
2017-07-17 07:11:36 +02:00
from lbrynet.core.HashAnnouncer import DummyHashAnnouncer
from lbrynet.core.BlobManager import DiskBlobManager
from lbrynet.cryptstream.CryptStreamCreator import CryptStreamCreator
2017-01-20 21:50:10 +01:00
log = logging.getLogger('decrypt_blob')
def main():
conf.initialize_settings()
parser = argparse.ArgumentParser()
parser.add_argument('filename')
parser.add_argument('hex_key')
parser.add_argument('hex_iv')
args = parser.parse_args()
log_support.configure_console(level='DEBUG')
2017-07-17 07:11:36 +02:00
run(args)
2017-01-20 21:50:10 +01:00
reactor.run()
@defer.inlineCallbacks
def run(args):
try:
yield encrypt_blob(args.filename, args.hex_key, args.hex_iv)
except Exception:
log.exception('Failed to encrypt blob')
finally:
reactor.callLater(0, reactor.stop)
2017-07-17 07:11:36 +02:00
@defer.inlineCallbacks
2017-01-20 21:50:10 +01:00
def encrypt_blob(filename, key, iv):
2017-07-17 07:11:36 +02:00
dummy_announcer = DummyHashAnnouncer()
manager = DiskBlobManager(dummy_announcer, '.', '.')
yield manager.setup()
creator = CryptStreamCreator(manager, filename, key, iv_generator(iv))
with open(filename, 'r') as infile:
data = infile.read(2**14)
while data:
yield creator.write(data)
data = infile.read(2**14)
yield creator.stop()
def iv_generator(iv):
iv = int(iv, 16)
while 1:
iv += 1
yield ("%016d" % iv)[-16:]
2017-01-20 21:50:10 +01:00
if __name__ == '__main__':
sys.exit(main())