forked from LBRYCommunity/lbry-sdk
fix encryption script
This commit is contained in:
parent
875d512680
commit
84bd4fdc3e
1 changed files with 20 additions and 25 deletions
|
@ -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__':
|
||||
|
|
Loading…
Add table
Reference in a new issue