Skip to content

Commit

Permalink
python3
Browse files Browse the repository at this point in the history
  • Loading branch information
benallard committed Jun 14, 2021
1 parent e5fab2b commit 662849a
Show file tree
Hide file tree
Showing 27 changed files with 167 additions and 167 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
*~
3 changes: 0 additions & 3 deletions .hgignore

This file was deleted.

6 changes: 3 additions & 3 deletions pythoncard/framework/apdu.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
IN_BLOCKSIZE = 0x80
OUT_BLOCKSIZE = 0x100

from pythoncard.framework import APDUException, ISOException, ISO7816
from ..framework import APDUException, ISOException, ISO7816

from pythoncard.utils import u1
from ..utils import u1

# there can only be one (1) APDU at a time ...
_current = None
Expand Down Expand Up @@ -71,7 +71,7 @@ def __init__(self, bytesarr):
if (bytesarr[ISO7816.OFFSET_LC] == 0) and (len(bytesarr) > ISO7816.OFFSET_LC + 3):
# P3 is extended
P3len = 3
for i in xrange(ISO7816.OFFSET_LC, ISO7816.OFFSET_LC + P3len):
for i in range(ISO7816.OFFSET_LC, ISO7816.OFFSET_LC + P3len):
self.buffer[i] = bytesarr[i]
self._offsetincoming += P3len

Expand Down
6 changes: 3 additions & 3 deletions pythoncard/framework/jcsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ def commitTransaction():

def makeTransientShortArray(length, event):
warnings.warn("Array is not transient")
return [0 for i in xrange(length)]
return [0 for i in range(length)]

makeTransientByteArray = makeTransientShortArray

def makeTransientBooleanArray(length, event):
warnings.warn("Array is not transient")
return [False for i in xrange(length)]
return [False for i in range(length)]


def makeTransientObjectArray(length, event):
warnings.warn("Array is not transient")
return [None for i in xrange(length)]
return [None for i in range(length)]

def requestObjectDeletion():
pass
Expand Down
4 changes: 2 additions & 2 deletions pythoncard/framework/ownerpin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def check(self, pin, offset, length):
raise ArrayIndexOutOfBoundsException()

Pin = [0 for i in range(self._maxpinsize)]
if isinstance(pin, basestring):
if isinstance(pin, str):
i = 0
for char in pin[offset:offset+length]:
Pin[i] = ord(char)
Expand Down Expand Up @@ -68,7 +68,7 @@ def update(self, pin, offset, length):
if length+offset > len(pin):
raise ArrayIndexOutOfBoundsException()

if isinstance(pin, basestring):
if isinstance(pin, str):
i = 0
for char in pin[offset:offset+length]:
self.__pin[i] = ord(char)
Expand Down
4 changes: 2 additions & 2 deletions pythoncard/framework/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"""

from python.lang import ArrayIndexOutOfBoundsException
from pythoncard.framework import JCSystem
from ..framework import JCSystem

from pythoncard.utils import s1, s2
from ..utils import s1, s2

def arrayCompare(src, srcOff, dest, destOff, length):
try:
Expand Down
4 changes: 2 additions & 2 deletions pythoncard/security/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ def _arrayTolong(bytes):
>>> arrayTolong([3, 19])
4867L
"""
l = 0L
l = 0
for i in range(len(bytes)-1, -1, -1):
l = l << 8
l += bytes[i]
return l

def _binaryToarray(bytes):
return [s1(ord(c)) for c in bytes]
return [s1(c) for c in bytes]

def _arrayTobinary(array):
return ''.join([chr(i & 0xff) for i in array])
Expand Down
1 change: 1 addition & 0 deletions pythoncard/security/publickey.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pythoncard.security.key import Key, _longToArray, _arrayTolong

try:
# pycrypto
from Crypto.PublicKey import RSA as pyCryptoRSA
except ImportError:
pyCryptoRSA = None
Expand Down
6 changes: 3 additions & 3 deletions pythoncard/security/secretkey.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pythoncard.framework.util import arrayCopy
from pythoncard.security.key import Key, _arrayTobinary
from pythoncard.security.key import Key, _arrayTobinary, _binaryToarray

from pyDes import pyDes

Expand All @@ -19,8 +19,8 @@ def __init__(self, typ, size):
self._key = None

def setKey(self, keyData, keyOff):
self._key = _arrayTobinary(keyData[keyOff:keyOff+(self.size / 8)])
self._key = _arrayTobinary(keyData[keyOff:keyOff+(self.size // 8)])
self._setInitialized()

def getKey(self, keyData, keyOff):
arrayCopy(_binaryToarray(self._key), 0, keyData, keyOff, self.size / 8)
arrayCopy(_binaryToarray(self._key), 0, keyData, keyOff, self.size // 8)
8 changes: 4 additions & 4 deletions pythoncardx/crypto/cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def getInstance(*args):
Cipher.ALG_DES_CBC_NOPAD,
Cipher.ALG_DES_CBC_PKCS5]:
return _pyDesDESCipher(algorithm)
print "algorithm not known: %d" % algorithm
print("algorithm not known: %d" % algorithm)
raise CryptoException(CryptoException.NO_SUCH_ALGORITHM)

def __init__(self, algorithm):
Expand Down Expand Up @@ -167,7 +167,7 @@ def EME_PKCS1_v1_5_dec(self, buf):
def doFinal(self, inBuff, inOffset, inLength, outBuff, outOffset):
Cipher.doFinal(self, inBuff, inOffset, inLength, outBuff, outOffset)

data = [0 for i in xrange(inLength)]
data = [0 for i in range(inLength)]
Util.arrayCopy(inBuff, inOffset, data, 0, inLength)
if ((self.algorithm == self.ALG_RSA_PKCS1) and
(self.mode == self.MODE_ENCRYPT)):
Expand Down Expand Up @@ -230,7 +230,7 @@ def init(self, theKey, theMode, bArray = [0,0,0,0,0,0,0,0], bOff = 0, bLen = 8):
if bLen != 8:
raise CryptoException(CryptoException.ILLEGAL_VALUE)

iv = [0 for i in xrange(8)]
iv = [0 for i in range(8)]
Util.arrayCopy(bArray, bOff, iv, 0, bLen)

iv = _arrayTobinary(iv)
Expand All @@ -249,7 +249,7 @@ def init(self, theKey, theMode, bArray = [0,0,0,0,0,0,0,0], bOff = 0, bLen = 8):
def doFinal(self, inBuff, inOffset, inLength, outBuff, outOffset):
Cipher.doFinal(self, inBuff, inOffset, inLength, outBuff, outOffset)

data = [0 for i in xrange(inLength)]
data = [0 for i in range(inLength)]
Util.arrayCopy(inBuff, inOffset, data, 0, inLength)
data = _arrayTobinary(data)

Expand Down
4 changes: 2 additions & 2 deletions pythoncardx/framework/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ def init(self, bArray, bOff, bLen, arrayFormat):
raise NullPointerException()
if arrayFormat == self.FORMAT_BCD:
self._value = 0
for i in xrange(bLen):
for i in range(bLen):
self._value *= 100
self._value += from_bcd(bArray[bOff+i])
elif arrayFormat == self.FORMAT_HEX:
self._value = 0
for i in xrange(bLen):
for i in range(bLen):
self._value = self._value << 8
self._value += from_hex(bArray[bOff+i])
else:
Expand Down
6 changes: 3 additions & 3 deletions test/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
def allTests():
path = os.path.abspath(os.path.dirname(__file__))
files = os.listdir(path)
print files
print(files)
test = re.compile("^test.+\.py$", re.IGNORECASE)
files = filter(test.search, files)
filenameToModuleName = lambda f: os.path.splitext(f)[0]
print files
print(files)
moduleNames = map(filenameToModuleName, files)
print moduleNames
print(moduleNames)
modules = map(__import__, moduleNames)
load = unittest.defaultTestLoader.loadTestsFromModule
return unittest.TestSuite(map(load, modules))
Expand Down
12 changes: 6 additions & 6 deletions test/testAID.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ class testAID(unittest.TestCase):

def testInit(self):
a1 = AID([0xA0, 0x00, 0x00, 0x00, 0x62, 0x03, 0x04, 0x00], 0, 8)
self.assertEquals(a1.aid[4], 0x62)
self.assertEqual(a1.aid[4], 0x62)
try:
a2 = AID([0xA0, 0x00, 0x00, 0x00, 0x62, 0x03, 0x04, 0x00], 4, 4)
self.fail()
except ValueError:
pass

a2 = AID([0xA0, 0x00, 0x00, 0x00, 0x62, 0x03, 0x04, 0x00, 0x03, 0x07], 4, 6)
self.assertEquals(a2.aid[0], 0x62)
self.assertEqual(a2.aid[0], 0x62)

try:
a3 = AID([7, 8, 9, 0xA0, 0x00, 0x00], 3, 5)
Expand All @@ -37,18 +37,18 @@ def testEquals(self):

def testEquality(self):
a1 = AID([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], 0, 8)
self.assertEquals(AID([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], 1, 8), a1)
self.assertEqual(AID([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], 1, 8), a1)
self.assertFalse(a1 == AID([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], 1, 7))
self.assertFalse(a1 == AID([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], 0, 8))

def testgetBytes(self):
a1 = AID([0xA0, 0x00, 0x00, 0x00, 0x62, 0x03, 0x04, 0x00], 0, 8)
dest = [0 for i in range(100)]
self.assertEquals(a1.getBytes(dest, 5), 8)
self.assertEquals(dest[5:5+8], a1.aid)
self.assertEqual(a1.getBytes(dest, 5), 8)
self.assertEqual(dest[5:5+8], a1.aid)
dest = []
try:
self.assertEquals(a1.getBytes(dest, 5), 8)
self.assertEqual(a1.getBytes(dest, 5), 8)
self.fail()
except ArrayIndexOutOfBoundsException:
pass
Expand Down
36 changes: 18 additions & 18 deletions test/testAPDU.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,51 @@ class testAPDU(unittest.TestCase):

def testState(self):
apdu = APDU([0x00, 0x20, 0x01, 0x00, 0x04, 0x31, 0x32, 0x33, 0x34, 0x00])
self.assertEquals(APDU.STATE_INITIAL, apdu.getCurrentState())
self.assertEqual(APDU.STATE_INITIAL, apdu.getCurrentState())
apdu.setIncomingAndReceive()
self.assertTrue(APDU.STATE_PARTIAL_INCOMING <= apdu.getCurrentState())
apdu.receiveBytes(ISO7816.OFFSET_CDATA)
self.assertEquals(APDU.STATE_FULL_INCOMING, apdu.getCurrentState())
self.assertEqual(APDU.STATE_FULL_INCOMING, apdu.getCurrentState())
apdu.setOutgoing()
self.assertEquals(APDU.STATE_OUTGOING, apdu.getCurrentState())
self.assertEqual(APDU.STATE_OUTGOING, apdu.getCurrentState())
apdu.setOutgoingLength(10)
self.assertEquals(APDU.STATE_OUTGOING_LENGTH_KNOWN, apdu.getCurrentState())
self.assertEqual(APDU.STATE_OUTGOING_LENGTH_KNOWN, apdu.getCurrentState())
apdu.sendBytes(0, 2)
self.assertEquals(APDU.STATE_PARTIAL_OUTGOING, apdu.getCurrentState())
self.assertEqual(APDU.STATE_PARTIAL_OUTGOING, apdu.getCurrentState())
apdu.sendBytes(0, 8)
self.assertEquals(APDU.STATE_FULL_OUTGOING, apdu.getCurrentState())
self.assertEqual(APDU.STATE_FULL_OUTGOING, apdu.getCurrentState())

def testBuffer(self):
apdu = APDU([0x00, 0x20, 0x01, 0x00, 0x04, 0x31, 0x32, 0x33, 0x34])
buffer = apdu.getBuffer()
self.assertEquals(0x00, buffer[ISO7816.OFFSET_CLA])
self.assertEquals(0x20, buffer[ISO7816.OFFSET_INS])
self.assertEquals(0x01, buffer[ISO7816.OFFSET_P1])
self.assertEquals(0x00, buffer[ISO7816.OFFSET_CLA])
self.assertEqual(0x00, buffer[ISO7816.OFFSET_CLA])
self.assertEqual(0x20, buffer[ISO7816.OFFSET_INS])
self.assertEqual(0x01, buffer[ISO7816.OFFSET_P1])
self.assertEqual(0x00, buffer[ISO7816.OFFSET_CLA])

buffer[0] = 1; buffer[1] = 2; buffer[2] = 3

buff = apdu.getBuffer()
self.assertEquals(1, buff[0])
self.assertEquals(2, buff[1])
self.assertEquals(3, buff[2])
self.assertEqual(1, buff[0])
self.assertEqual(2, buff[1])
self.assertEqual(3, buff[2])

def testAPDUDoc(self):
""" This is an adaptation of the piece of code on the APDU page """
apdu = APDU([0x00, 0x20, 0x01, 0x00, 0x04, 0x31, 0x32, 0x33, 0x34, 0x00])
buffer = apdu.getBuffer()
cla = buffer[ISO7816.OFFSET_CLA]
self.assertEquals(0, cla)
self.assertEqual(0, cla)
ins = buffer[ISO7816.OFFSET_INS]
self.assertEquals(0x20, ins)
self.assertEqual(0x20, ins)

# assume this command has incoming data
# Lc tells us the incoming apdu command length
bytesLeft = buffer[ISO7816.OFFSET_LC]
self.assertEquals(4, bytesLeft)
self.assertEqual(4, bytesLeft)

readCount = apdu.setIncomingAndReceive()
self.assertEquals(4, readCount)
self.assertEqual(4, readCount)
while bytesLeft > 0:
# process bytes in buffer[5] to buffer[readCount+4];
bytesLeft -= readCount
Expand All @@ -67,7 +67,7 @@ def testAPDUDoc(self):
# build response data in apdu.buffer[ 0.. outCount-1 ];
buffer[0] = 1; buffer[1] = 2; buffer[3] = 3
apdu.sendBytes ( 0 , 3 )
self.assertEquals(APDU.STATE_FULL_OUTGOING,
self.assertEqual(APDU.STATE_FULL_OUTGOING,
apdu.getCurrentState())


Expand Down
14 changes: 7 additions & 7 deletions test/testApplet.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def process(self, apdu):
self.assertTrue(app.select())
try:
app.process(APDU([0,0,0,0]))
except ISOException, isoe:
self.assertEquals(0x9000,
except ISOException as isoe:
self.assertEqual(0x9000,
isoe.getReason())


Expand Down Expand Up @@ -73,11 +73,11 @@ def choice2(self, apdu):
app.process(apdu)
buf = apdu._APDU__buffer[:apdu._outgoinglength]
buf.extend([0x90, 0x00])
self.assertEquals(response, buf)
except ISOException, isoe:
self.assertEqual(response, buf)
except ISOException as isoe:
sw = isoe.getReason()
sw1 = sw // 256; sw2 = sw % 256
self.assertEquals(response, [sw1, sw2])
self.assertEqual(response, [sw1, sw2])

def testRSA2048Applet(self):

Expand All @@ -99,11 +99,11 @@ def myregister(self, bArray, bOffset, bLength):
apdu = APDU([0x00, 0xAA, 0x01, 0x00] + [len(tobeencrypted)] + tobeencrypted + [0x00])
app.process(apdu)
buf = apdu._APDU__buffer[:apdu._outgoinglength]
self.assertEquals(len(buf), 1024/8)
self.assertEqual(len(buf), 1024/8)
buf.extend([0x90, 0x00])

apdu = APDU([0x00, 0xAA, 0x02, 0x00] + [len(buf)-2] + buf[:-2] + [0])
app.process(apdu)
buf = apdu._APDU__buffer[:apdu._outgoinglength]
self.assertEquals(tobeencrypted, buf)
self.assertEqual(tobeencrypted, buf)

8 changes: 4 additions & 4 deletions test/testBigNum.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ class BigNumTest(unittest.TestCase):
def testInitBCD(self):
bn = BigNumber(8)
bn.init([-128, 0x00], 0, 2, BigNumber.FORMAT_BCD)
self.assertEquals(8000, bn._value)
self.assertEqual(8000, bn._value)

def testInitHEX(self):
bn = BigNumber(8)
bn.init([0x10, 0x00], 0, 2, BigNumber.FORMAT_HEX)
self.assertEquals(4096, bn._value)
self.assertEqual(4096, bn._value)

def testtoBytesBCD(self):
bn = BigNumber(8)
bn.init([0x12, -128], 0, 2, BigNumber.FORMAT_BCD)
array = [0 for i in range(3)]
bn.toBytes(array, 0, 2, BigNumber.FORMAT_BCD)
self.assertEquals([0x12, -128], array[0:2])
self.assertEqual([0x12, -128], array[0:2])

def testtoBytesHEX(self):
bn = BigNumber(8)
bn.init([-127, 0x34], 0, 2, BigNumber.FORMAT_HEX)
array = [0 for i in range(3)]
bn.toBytes(array, 0, 2, BigNumber.FORMAT_HEX)
self.assertEquals([-127, 0x34], array[0:2])
self.assertEqual([-127, 0x34], array[0:2])
Loading

0 comments on commit 662849a

Please sign in to comment.