package documentation

smartcard.util package

__author__ = "https://www.gemalto.com/"

Copyright 2001-2012 gemalto Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com

This file is part of pyscard.

pyscard is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

pyscard is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with pyscard; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

From __init__.py:

Function BinStringToHexList Deprecated. Use `list(binstring.encode("utf-8"))` or similar.
Function HexListToBinString Deprecated. Use `bytes(hexlist).decode("utf-8")` or similar.
Function padd Padds a byte list with a constant byte value (default is x0FF)
Function toASCIIBytes Convert a string to a list of UTF-8 encoded bytes.
Function toASCIIString Convert a list of integers in the range ``[32, 127]`` to a string.
Function toBytes Convert a string of hexadecimal characters to a list of integers.
Function toGSM3_38Bytes Returns a list of bytes from a string using GSM 3.38 conversion table.
Function toHexString Convert a list of integers to a formatted string of hexadecimal.
Constant COMMA Undocumented
Constant HEX Undocumented
Constant PACK Undocumented
Constant UPPERCASE Undocumented
Variable __dic_GSM_3_38__ Undocumented
def BinStringToHexList(binstring: str) -> list[int]:

Deprecated. Use `list(binstring.encode("utf-8"))` or similar.

>>> BinStringToHexList("Number 101")
[78, 117, 109, 98, 101, 114, 32, 49, 48, 49]
def HexListToBinString(hexlist: list[int]) -> str:

Deprecated. Use `bytes(hexlist).decode("utf-8")` or similar.

>>> HexListToBinString([78, 117, 109, 98, 101, 114, 32, 49, 48, 49])
'Number 101'
def padd(bytelist: list[int], length: int, padding: str = 'FF'):

Padds a byte list with a constant byte value (default is x0FF)

Parameters
bytelist:list[int]the byte list to padd
length:intthe total length of the resulting byte list; no padding if length is smaller than the byte list length
padding:strpadding value (default is 0xff)
Returns

the padded bytelist

>>> padd([59, 101, 0, 0, 156, 17, 1, 1, 3], 16)
[59, 101, 0, 0, 156, 17, 1, 1, 3, 255, 255, 255, 255, 255, 255, 255]
>>> padd([59, 101, 0, 0, 156, 17, 1, 1, 3], 12, '80')
[59, 101, 0, 0, 156, 17, 1, 1, 3, 128, 128, 128]
>>> padd([59, 101, 0, 0, 156, 17, 1, 1, 3], 8)
[59, 101, 0, 0, 156, 17, 1, 1, 3]
def toASCIIBytes(stringtoconvert: str) -> list[int]:

Convert a string to a list of UTF-8 encoded bytes.

Parameters
stringtoconvert:strthe string to convert into a byte list
Returns
list[int]

a byte list of the ASCII codes of the string characters

toASCIIBytes() is the reverse of toASCIIString()

>>> toASCIIBytes("Number 101")
[78, 117, 109, 98, 101, 114, 32, 49, 48, 49]
def toASCIIString(bytelist: list[int]) -> str:

Convert a list of integers in the range ``[32, 127]`` to a string.

Integer values outside the range ``[32, 127]`` are replaced with a period.

Parameters
bytelist:list[int]list of ASCII bytes to convert into a string
Returns
str

a string from the ASCII code list

toASCIIString() is the reverse of toASCIIBytes()

>>> toASCIIString([0x4E,0x75,0x6D,0x62,0x65,0x72,0x20,0x31,0x30,0x31])
'Number 101'
>>> toASCIIString([0x01, 0x20, 0x80, 0x7E, 0xF0])
". .~."
def toBytes(bytestring: str) -> list[int]:

Convert a string of hexadecimal characters to a list of integers.

Parameters
bytestring:str

a byte string

>>> toBytes("3B 65 00 00 9C 11 01 01 03")
[59, 101, 0, 0, 156, 17, 1, 1, 3]
>>> toBytes("3B6500009C11010103")
[59, 101, 0, 0, 156, 17, 1, 1, 3]
>>> toBytes("3B6500   009C1101  0103")
[59, 101, 0, 0, 156, 17, 1, 1, 3]
Returns
list[int]Undocumented
def toGSM3_38Bytes(stringtoconvert: str | bytes) -> list[int]:

Returns a list of bytes from a string using GSM 3.38 conversion table.

Parameters
stringtoconvert:str | bytesstring to convert
Returns
list[int]

a list of bytes

>>> toGSM3_38Bytes("@ùPascal")
[0, 6, 80, 97, 115, 99, 97, 108]
def toHexString(data: list[int] | None = None, format: int = 0) -> str:

Convert a list of integers to a formatted string of hexadecimal.

Integers larger than 255 will be truncated to two-byte hexadecimal pairs.

Parameters
data:list[int] | Nonea list of bytes to stringify, e.g. [59, 22, 148, 32, 2, 1, 0, 0, 13]
format:int

a logical OR of

  • COMMA: add a comma between bytes
  • HEX: add the 0x chars before bytes
  • UPPERCASE: use 0X before bytes (need HEX)
  • PACK: remove blanks
>>> vals = [0x3B, 0x65, 0x00, 0x00, 0x9C, 0x11, 0x01, 0x01, 0x03]
>>> toHexString(vals)
'3B 65 00 00 9C 11 01 01 03'
>>> toHexString(vals, COMMA)
'3B, 65, 00, 00, 9C, 11, 01, 01, 03'
>>> toHexString(vals, HEX)
'0x3B 0x65 0x00 0x00 0x9C 0x11 0x01 0x01 0x03'
>>> toHexString(vals, HEX | COMMA)
'0x3B, 0x65, 0x00, 0x00, 0x9C, 0x11, 0x01, 0x01, 0x03'
>>> toHexString(vals, PACK)
'3B6500009C11010103'
>>> toHexString(vals, HEX | UPPERCASE)
'0X3B 0X65 0X00 0X00 0X9C 0X11 0X01 0X01 0X03'
>>> toHexString(vals, HEX | UPPERCASE | COMMA)
'0X3B, 0X65, 0X00, 0X00, 0X9C, 0X11, 0X01, 0X01, 0X03'
Returns
strUndocumented
COMMA: int =

Undocumented

Value
8
HEX: int =

Undocumented

Value
2
PACK: int =

Undocumented

Value
1
UPPERCASE: int =

Undocumented

Value
4
__dic_GSM_3_38__: dict[str, int] =

Undocumented