Repository: borgbackup/borg

Issue:
borg mount: TypeError in error handler vol2
Similar issue as in #1894 

```
/t/borg » ~/.local/bin/borg mount /mnt/data/gdrive/backup-borg /mnt/data
Enter passphrase for key /mnt/data/gdrive/backup-borg: 
Local Exception.
Traceback (most recent call last):
  File "/tmp/borg/borg/archiver.py", line 1957, in main
    exit_code = archiver.run(args)
  File "/tmp/borg/borg/archiver.py", line 1902, in run
    return args.func(args)
  File "/tmp/borg/borg/archiver.py", line 82, in wrapper
    kwargs['manifest'], kwargs['key'] = Manifest.load(repository)
  File "/tmp/borg/borg/helpers.py", line 119, in load
    data = key.decrypt(None, cdata)
  File "/tmp/borg/borg/key.py", line 150, in decrypt
    raise IntegrityError('Chunk %s: Encryption envelope checksum mismatch' % bin_to_hex(id))
  File "/tmp/borg/borg/helpers.py", line 768, in bin_to_hex
    return hexlify(binary).decode('ascii')
TypeError: a bytes-like object is required, not 'NoneType'

Platform: Linux protagonist 4.8.10-1-ARCH #1 SMP PREEMPT Mon Nov 21 11:55:43 CET 2016 x86_64 
Linux: arch  
Borg: 1.0.9rc2.dev11+ngb25de0a  Python: CPython 3.5.2
PID: 10175  CWD: /tmp/borg
sys.argv: ['/home/lpp/.local/bin/borg', 'mount', '/mnt/data/gdrive/backup-borg', '/mnt/data']
SSH_ORIGINAL_COMMAND: None
```

I've manually installed commit b25de0ab38e396854646c1e6e96b41402372308f which is One after Merge #1895  was merged into 1.0-maintenance


Discussion:
Oh, oops. Should've looked around for similar places... - will do now.
Does this happen with other borg commands on this repository as well (eg list)?
Yes:

```
~ » .local/bin/borg list /mnt/data/gdrive/backup-borg                                 master ✗
Enter passphrase for key /mnt/data/gdrive/backup-borg: 
Enter passphrase for key /mnt/data/gdrive/backup-borg: 
Local Exception.
Traceback (most recent call last):
  File "/tmp/borg/borg/archiver.py", line 1957, in main
    exit_code = archiver.run(args)
  File "/tmp/borg/borg/archiver.py", line 1902, in run
    return args.func(args)
  File "/tmp/borg/borg/archiver.py", line 82, in wrapper
    kwargs['manifest'], kwargs['key'] = Manifest.load(repository)
  File "/tmp/borg/borg/helpers.py", line 119, in load
    data = key.decrypt(None, cdata)
  File "/tmp/borg/borg/key.py", line 150, in decrypt
    raise IntegrityError('Chunk %s: Encryption envelope checksum mismatch' % bin_to_hex(id))
  File "/tmp/borg/borg/helpers.py", line 768, in bin_to_hex
    return hexlify(binary).decode('ascii')
TypeError: a bytes-like object is required, not 'NoneType'

Platform: Linux protagonist 4.8.10-1-ARCH #1 SMP PREEMPT Mon Nov 21 11:55:43 CET 2016 x86_64 
Linux: arch  
Borg: 1.0.9rc2.dev11+ngb25de0a  Python: CPython 3.5.2
PID: 15593  CWD: /home/lpp
sys.argv: ['.local/bin/borg', 'list', '/mnt/data/gdrive/backup-borg']
SSH_ORIGINAL_COMMAND: None

```

Resolving patch:
diff --git a/borg/key.py b/borg/key.py
--- a/borg/key.py
+++ b/borg/key.py
@@ -143,11 +143,13 @@ def encrypt(self, data):
     def decrypt(self, id, data):
         if not (data[0] == self.TYPE or
             data[0] == PassphraseKey.TYPE and isinstance(self, RepoKey)):
-            raise IntegrityError('Chunk %s: Invalid encryption envelope' % bin_to_hex(id))
+            id_str = bin_to_hex(id) if id is not None else '(unknown)'
+            raise IntegrityError('Chunk %s: Invalid encryption envelope' % id_str)
         hmac_given = memoryview(data)[1:33]
         hmac_computed = memoryview(HMAC(self.enc_hmac_key, memoryview(data)[33:], sha256).digest())
         if not compare_digest(hmac_computed, hmac_given):
-            raise IntegrityError('Chunk %s: Encryption envelope checksum mismatch' % bin_to_hex(id))
+            id_str = bin_to_hex(id) if id is not None else '(unknown)'
+            raise IntegrityError('Chunk %s: Encryption envelope checksum mismatch' % id_str)
         self.dec_cipher.reset(iv=PREFIX + data[33:41])
         data = self.compressor.decompress(self.dec_cipher.decrypt(data[41:]))
         if id:


Test patch: