Skip to content

Commit

Permalink
Decryption is now implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
oz123 committed Aug 9, 2016
1 parent 6face88 commit 24cb118
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
7 changes: 3 additions & 4 deletions fernet.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def decrypt(self, token, ttl=None):
except (TypeError, binascii.Error):
raise InvalidToken

if not data:
if not data or data[0] != 0x80:
raise InvalidToken

try:
Expand All @@ -84,17 +84,16 @@ def decrypt(self, token, ttl=None):

h = HMAC.new(self._signing_key, digestmod='sha256')
h.update(data[:-32])
import pdb; pdb.set_trace()
if not HMAC.compare_digest(h.digest(), data[-32:]):
raise InvalidToken

iv = data[9:25]
ciphertext = data[25:-32]
decryptor = Decrypter(AESModeOfOperationCBC(self._signing_key, iv))
decryptor = Decrypter(AESModeOfOperationCBC(self._encryption_key, iv))
try:
plaintext = decryptor.feed(ciphertext)
plaintext += decryptor.feed()
except Exception:
except ValueError:
raise InvalidToken

return plaintext
3 changes: 3 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,7 @@ def test_fernet():
fernet = Fernet(ckey)
cipher = fernet._encrypt_from_parts(b"Secret message!", current_time, iv)
assert cipher == ccipher
ctext = cfernet.decrypt(ccipher)
text = fernet.decrypt(cipher)

assert ctext == text

0 comments on commit 24cb118

Please sign in to comment.