From 84bd4fdc3efdd8b4e05ec4c5e23f04448c0bf70f Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Mon, 17 Jul 2017 02:11:36 -0300 Subject: [PATCH] fix encryption script --- scripts/encrypt_blob.py | 45 ++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/scripts/encrypt_blob.py b/scripts/encrypt_blob.py index 440ea7a8d..a30fbcc98 100644 --- a/scripts/encrypt_blob.py +++ b/scripts/encrypt_blob.py @@ -1,17 +1,16 @@ """Encrypt a single file using the given key and iv""" import argparse -import binascii import logging -import StringIO import sys from twisted.internet import defer from twisted.internet import reactor from lbrynet import conf -from lbrynet.cryptstream import CryptBlob from lbrynet.core import log_support -from lbrynet.core import cryptoutils +from lbrynet.core.HashAnnouncer import DummyHashAnnouncer +from lbrynet.core.BlobManager import DiskBlobManager +from lbrynet.cryptstream.CryptStreamCreator import CryptStreamCreator log = logging.getLogger('decrypt_blob') @@ -26,7 +25,7 @@ def main(): args = parser.parse_args() log_support.configure_console(level='DEBUG') - d = run(args) + run(args) reactor.run() @@ -40,29 +39,25 @@ def run(args): reactor.callLater(0, reactor.stop) +@defer.inlineCallbacks def encrypt_blob(filename, key, iv): - blob = Blob() - blob_maker = CryptBlob.CryptStreamBlobMaker( - binascii.unhexlify(key), binascii.unhexlify(iv), 0, blob) - with open(filename) as fin: - blob_maker.write(fin.read()) - blob_maker.close() + 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() -class Blob(object): - def __init__(self): - self.data = StringIO.StringIO() - - def write(self, data): - self.data.write(data) - - def close(self): - hashsum = cryptoutils.get_lbry_hash_obj() - buffer = self.data.getvalue() - hashsum.update(buffer) - with open(hashsum.hexdigest(), 'w') as fout: - fout.write(buffer) - return defer.succeed(True) +def iv_generator(iv): + iv = int(iv, 16) + while 1: + iv += 1 + yield ("%016d" % iv)[-16:] if __name__ == '__main__':