fix encryption script

This commit is contained in:
Victor Shyba 2017-07-17 02:11:36 -03:00 committed by Jack Robison
parent 875d512680
commit 84bd4fdc3e
No known key found for this signature in database
GPG key ID: 284699E7404E3CFF

View file

@ -1,17 +1,16 @@
"""Encrypt a single file using the given key and iv""" """Encrypt a single file using the given key and iv"""
import argparse import argparse
import binascii
import logging import logging
import StringIO
import sys import sys
from twisted.internet import defer from twisted.internet import defer
from twisted.internet import reactor from twisted.internet import reactor
from lbrynet import conf from lbrynet import conf
from lbrynet.cryptstream import CryptBlob
from lbrynet.core import log_support 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') log = logging.getLogger('decrypt_blob')
@ -26,7 +25,7 @@ def main():
args = parser.parse_args() args = parser.parse_args()
log_support.configure_console(level='DEBUG') log_support.configure_console(level='DEBUG')
d = run(args) run(args)
reactor.run() reactor.run()
@ -40,29 +39,25 @@ def run(args):
reactor.callLater(0, reactor.stop) reactor.callLater(0, reactor.stop)
@defer.inlineCallbacks
def encrypt_blob(filename, key, iv): def encrypt_blob(filename, key, iv):
blob = Blob() dummy_announcer = DummyHashAnnouncer()
blob_maker = CryptBlob.CryptStreamBlobMaker( manager = DiskBlobManager(dummy_announcer, '.', '.')
binascii.unhexlify(key), binascii.unhexlify(iv), 0, blob) yield manager.setup()
with open(filename) as fin: creator = CryptStreamCreator(manager, filename, key, iv_generator(iv))
blob_maker.write(fin.read()) with open(filename, 'r') as infile:
blob_maker.close() data = infile.read(2**14)
while data:
yield creator.write(data)
data = infile.read(2**14)
yield creator.stop()
class Blob(object): def iv_generator(iv):
def __init__(self): iv = int(iv, 16)
self.data = StringIO.StringIO() while 1:
iv += 1
def write(self, data): yield ("%016d" % iv)[-16:]
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)
if __name__ == '__main__': if __name__ == '__main__':