diff --git a/conftest.py b/conftest.py index fa070d2..53ad858 100644 --- a/conftest.py +++ b/conftest.py @@ -12,10 +12,3 @@ 'jaraco/crypto/cert.py', ] ) - - -collect_ignore.extend( - [ - 'jaraco/crypto/blowfish.py', - ] -) diff --git a/jaraco/crypto/blowfish.py b/jaraco/crypto/blowfish.py index 39dbf81..1ce8da3 100644 --- a/jaraco/crypto/blowfish.py +++ b/jaraco/crypto/blowfish.py @@ -69,30 +69,29 @@ >>> text = "testtext" ->>> crypted = cipher.encrypt(text) +>>> crypted = cipher.encrypt(text.encode()) >>> crypted == text False ->>> decrypted = cipher.decrypt(crypted) ->>> decrypted == text -True +>>> decrypted = cipher.decrypt(crypted).decode() +>>> decrypted +'testtext' Test CTR encrypt >>> text = "If the offer's shunned, you might as well be walking on the sun" >>> cipher.initCTR() ->>> crypted = cipher.encryptCTR(text) +>>> crypted = cipher.encryptCTR(text.encode()) >>> crypted == text False >>> cipher.initCTR() ->>> decrypted = cipher.decryptCTR(crypted) ->>> decrypted == text -True +>>> decrypted = cipher.decryptCTR(crypted).decode() +>>> decrypted +"If the offer's shunned, you might as well be walking on the sun" """ import struct -import types __author__ = "Michael Gilfix " @@ -1302,30 +1301,20 @@ def encrypt(self, data): ) # Use big endianess since that's what everyone else uses - xl = ( - ord(data[3]) - | (ord(data[2]) << 8) - | (ord(data[1]) << 16) - | (ord(data[0]) << 24) - ) - xr = ( - ord(data[7]) - | (ord(data[6]) << 8) - | (ord(data[5]) << 16) - | (ord(data[4]) << 24) - ) + xl = data[3] | (data[2] << 8) | (data[1] << 16) | (data[0] << 24) + xr = data[7] | (data[6] << 8) | (data[5] << 16) | (data[4] << 24) cl, cr = self.cipher(xl, xr, self.ENCRYPT) - chars = ''.join( + chars = bytes( [ - chr((cl >> 24) & 0xFF), - chr((cl >> 16) & 0xFF), - chr((cl >> 8) & 0xFF), - chr(cl & 0xFF), - chr((cr >> 24) & 0xFF), - chr((cr >> 16) & 0xFF), - chr((cr >> 8) & 0xFF), - chr(cr & 0xFF), + (cl >> 24) & 0xFF, + (cl >> 16) & 0xFF, + (cl >> 8) & 0xFF, + cl & 0xFF, + (cr >> 24) & 0xFF, + (cr >> 16) & 0xFF, + (cr >> 8) & 0xFF, + cr & 0xFF, ] ) return chars @@ -1337,30 +1326,20 @@ def decrypt(self, data): ) # Use big endianess since that's what everyone else uses - cl = ( - ord(data[3]) - | (ord(data[2]) << 8) - | (ord(data[1]) << 16) - | (ord(data[0]) << 24) - ) - cr = ( - ord(data[7]) - | (ord(data[6]) << 8) - | (ord(data[5]) << 16) - | (ord(data[4]) << 24) - ) + cl = data[3] | (data[2] << 8) | (data[1] << 16) | (data[0] << 24) + cr = data[7] | (data[6] << 8) | (data[5] << 16) | (data[4] << 24) xl, xr = self.cipher(cl, cr, self.DECRYPT) - chars = ''.join( + chars = bytes( [ - chr((xl >> 24) & 0xFF), - chr((xl >> 16) & 0xFF), - chr((xl >> 8) & 0xFF), - chr(xl & 0xFF), - chr((xr >> 24) & 0xFF), - chr((xr >> 16) & 0xFF), - chr((xr >> 8) & 0xFF), - chr(xr & 0xFF), + (xl >> 24) & 0xFF, + (xl >> 16) & 0xFF, + (xl >> 8) & 0xFF, + xl & 0xFF, + (xr >> 24) & 0xFF, + (xr >> 16) & 0xFF, + (xr >> 8) & 0xFF, + xr & 0xFF, ] ) return chars @@ -1380,7 +1359,7 @@ def _calcCTRBUF(self): def _nextCTRByte(self): """Returns one byte of CTR keystream""" - b = ord(self.ctr_cks[self.ctr_pos]) + b = self.ctr_cks[self.ctr_pos] self.ctr_pos += 1 if self.ctr_pos >= len(self.ctr_cks): self._calcCTRBUF() @@ -1392,12 +1371,7 @@ def encryptCTR(self, data): (belonging to the same logical stream of buffers) can be encrypted with this method one after the other without any intermediate work. """ - if not isinstance(data, types.StringType): - raise RuntimeError("Can only work on 8-bit strings") - result = [] - for ch in data: - result.append(chr(ord(ch) ^ self._nextCTRByte())) - return "".join(result) + return bytes(ch ^ self._nextCTRByte() for ch in data) def decryptCTR(self, data): return self.encryptCTR(data) diff --git a/newsfragments/+0952bdf2.feature.rst b/newsfragments/+0952bdf2.feature.rst new file mode 100644 index 0000000..c81e2a0 --- /dev/null +++ b/newsfragments/+0952bdf2.feature.rst @@ -0,0 +1 @@ +Updated blowfish implementation for Python 3 \ No newline at end of file