Skip to content
This repository has been archived by the owner on Aug 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #12 from NBISweden/refactoring
Browse files Browse the repository at this point in the history
Refactoring header re-encryption
  • Loading branch information
silverdaz committed Jun 20, 2018
2 parents 47ae06b + 17d6ba6 commit 30aab75
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions legacryptor/crypt4gh.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,22 @@ def decrypt(privkey, infile, process_output=do_nothing, chunk_size=4096):
body_decrypt(r, infile, process_output=process_output, chunk_size=chunk_size)


def reencrypt_header(pubkey, privkey, encrypted_part):
'''Given the encrypted part of the header, re-encrypts it and returns a new header
The private key should be unlocked.'''
assert privkey.is_unlocked, "The private key should be unlocked"
return Header.decrypt(encrypted_part, privkey).encrypt(pubkey)


def reencrypt(pubkey, privkey, infile, process_output=do_nothing, chunk_size=4096):
'''Extract header and update with another one
The AES encrypted part is only copied'''
assert privkey.is_unlocked, "The private key should be unlocked"
assert chunk_size >= 32, "Chunk size larger than 32 bytes required"

_, encrypted_part = get_header(infile)
header = Header.decrypt(encrypted_part, privkey)
header_bytes = header.encrypt(pubkey)
header_bytes = reencrypt_header(pubkey, privkey, encrypted_part)
process_output(header_bytes)

LOG.info(f'Streaming the remainer of the file')
Expand Down

0 comments on commit 30aab75

Please sign in to comment.