1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-03-22 06:38:37 -05:00

cryptsuite.py: Python 3 compatibility fixes.

I intended cryptsuite to be Python 2/3 agnostic when I first wrote it,
but of course since then I've been testing on whichever Python was
handy and not continuing to check that both actually worked.
This commit is contained in:
Simon Tatham 2019-01-15 22:29:11 +00:00
parent c9f673ac12
commit 85633ac4bd

View File

@ -5,6 +5,7 @@ import struct
import itertools import itertools
import contextlib import contextlib
import hashlib import hashlib
import binascii
try: try:
from math import gcd from math import gcd
except ImportError: except ImportError:
@ -28,7 +29,7 @@ def nbits(n):
return toret return toret
def unhex(s): def unhex(s):
return s.replace(" ", "").replace("\n", "").decode("hex") return binascii.unhexlify(s.replace(" ", "").replace("\n", ""))
def ssh_uint32(n): def ssh_uint32(n):
return struct.pack(">L", n) return struct.pack(">L", n)
@ -140,7 +141,7 @@ class MyTestBase(unittest.TestCase):
def assertEqualBin(self, x, y): def assertEqualBin(self, x, y):
# Like assertEqual, but produces more legible error reports # Like assertEqual, but produces more legible error reports
# for random-looking binary data. # for random-looking binary data.
self.assertEqual(x.encode('hex'), y.encode('hex')) self.assertEqual(binascii.hexlify(x), binascii.hexlify(y))
class mpint(MyTestBase): class mpint(MyTestBase):
def testCreation(self): def testCreation(self):
@ -910,7 +911,7 @@ class crypt(MyTestBase):
# A pile of conveniently available random-looking test data. # A pile of conveniently available random-looking test data.
test_ciphertext = ssh2_mpint(last(fibonacci_scattered(14))) test_ciphertext = ssh2_mpint(last(fibonacci_scattered(14)))
test_ciphertext += "x" * (15 & -len(test_ciphertext)) # pad to a block test_ciphertext += b"x" * (15 & -len(test_ciphertext)) # pad to a block
# Test key and IV. # Test key and IV.
test_key = b"foobarbazquxquuxFooBarBazQuxQuux" test_key = b"foobarbazquxquuxFooBarBazQuxQuux"
@ -925,7 +926,7 @@ class crypt(MyTestBase):
ssh2_cipher_setkey(c, test_key[:keylen//8]) ssh2_cipher_setkey(c, test_key[:keylen//8])
for chunklen in range(16, 16*12, 16): for chunklen in range(16, 16*12, 16):
ssh2_cipher_setiv(c, test_iv) ssh2_cipher_setiv(c, test_iv)
decryption = "" decryption = b""
for pos in range(0, len(test_ciphertext), chunklen): for pos in range(0, len(test_ciphertext), chunklen):
chunk = test_ciphertext[pos:pos+chunklen] chunk = test_ciphertext[pos:pos+chunklen]
decryption += ssh2_cipher_decrypt(c, chunk) decryption += ssh2_cipher_decrypt(c, chunk)