Cnv class¶
- class cryptosyspki.Cnv¶
Character conversion routines.
- class EndianNess¶
Byte order for integer/byte array conversions.
- BIG_ENDIAN = 0¶
Big-endian order (default)
- LITTLE_ENDIAN = 1¶
Little-endian order
- static frombase58(s)¶
Decode a base58-encoded string into a byte array.
Uses the "Bitcoin" scheme of base58 encoding where the leading character '1' is reserved for representing an entire leading zero byte.
- Parameters:
s (str) -- Base58-encoded data
- Returns:
Binary data in byte array.
- Return type:
bytes
- static frombase64(s)¶
Decode a base64-encoded string into a byte array.
- Parameters:
s (str) -- Base64-encoded data
- Returns:
Binary data in byte array.
- Return type:
bytes
- Remarks:
Whitespace characters are ignored, but other non-base64 characters will cause an error.
- static fromhex(s)¶
Decode a hexadecimal-encoded string into a byte array.
- Parameters:
s (str) -- Hex-encoded string
- Returns:
Binary data in byte array.
- Return type:
bytes
Note
Whitespace and ASCII punctuation characters in the input are ignored, but other non-hex characters, e.g.
[G-Zg-z]
, will cause an error.Examples
>>> Cnv.fromhex("61:62:63") 'abc'
- static num_from_bytes(data, endn=EndianNess.BIG_ENDIAN)¶
Convert the leftmost four bytes of an array to a 32-bit integer.
An array shorter than 4 bytes will be padded on the right with zeros.
- Parameters:
data (bytes) -- Byte array to be converted
endn (EndianNess) -- Byte order
- Returns:
Integer value.
- Return type:
int
Examples
>>> hex(Cnv.num_from_bytes(Cnv.fromhex("DEADBEEF"))) '0xdeadbeefL' >>> hex(Cnv.num_from_bytes(Cnv.fromhex("DEADBEEF"), Cnv.EndianNess.LITTLE_ENDIAN)) '0xefbeaddeL'
- static num_to_bytes(num, endn=EndianNess.BIG_ENDIAN)¶
Convert a 32-bit integer to an array of 4 bytes.
- Parameters:
num (int) -- Integer to be converted
endn (EndianNess) -- Byte order
- Returns:
Byte array containing representation of integer in given order.
- Return type:
bytes
- static reverse_bytes(data)¶
Reverse the order of a byte array.
- Parameters:
data (bytes) -- Input data to be reversed
- Returns:
Byte array in reverse order.
- Return type:
bytes
Examples
>>> Cnv.tohex(Cnv.reverse_bytes(Cnv.fromhex("DEADBEEF01"))) '01EFBEADDE'
- static shortpathname(pathName)¶
Retrieve the Windows short path form of the specified path.
- Parameters:
pathName (str) -- Path name.
- Returns:
Windows short path name of file or empty string if file does not exist.
- Return type:
str
- Remarks:
Windows only. The file path must exist. The short path name is guaranteed to be ASCII and can be used as a filename argument in any function in this Toolkit.
Example
>>> Cnv.shortpathname("work/你好.txt") 'work/FC0F~1.TXT'
- static tobase58(data)¶
Encode binary data as a base58 string.
Uses the "Bitcoin" scheme of base58 encoding where the leading character '1' is reserved for representing an entire leading zero byte.
- Parameters:
data (bytes) -- binary data
- Returns:
Base58-encoded string.
- Return type:
str
Example
>>> Cnv.tobase58(Cnv.fromhex("00010966776006953D5567439E5E39F86A0D273BEED61967F6")) '16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM'
- static tobase64(data)¶
Encode binary data as a base64 string.
- Parameters:
data (bytes) -- binary data to be encoded.
- Returns:
Base64-encoded string.
- Return type:
str
Example
>>> Cnv.tobase64(Cnv.fromhex('fedcba9876543210')) '/ty6mHZUMhA='
- static tohex(data)¶
Encode binary data as a hexadecimal string.
- Parameters:
data (bytes) -- binary data to be encoded.
- Returns:
Hex-encoded string. Letters [A-F] are in uppercase. Use
s.lower()
for lowercase.- Return type:
str
Examples
>>> Cnv.tohex(b"abcé") '616263E9' >>> Cnv.tohex(bytearray([0xde, 0xad, 0xbe, 0xef])).lower() 'deadbeef'
- static utf8_check(data)¶
Check if a byte array or string contains valid UTF-8 characters. Returns integer code.
- Parameters:
data (bytes) -- input byte array to check
- Returns:
Integer code indicating nature of encoded characters:
0
-- Not valid UTF-81
-- Valid UTF-8, all characters are 7-bit ASCII2
-- Valid UTF-8, contains at least one multi-byte character equivalent to 8-bit ANSI3
-- Valid UTF-8, contains at least one multi-byte character that cannot be represented in a single-byte character set
- Return type:
int
- static utf8_check_file(filename)¶
Check if a file contains valid UTF-8 characters. Returns integer code.
- Parameters:
filename (str) -- name of file to check
- Returns:
Integer code indicating nature of encoded characters:
0
-- Not valid UTF-81
-- Valid UTF-8, all characters are 7-bit ASCII2
-- Valid UTF-8, contains at least one multi-byte character equivalent to 8-bit ANSI3
-- Valid UTF-8, contains at least one multi-byte character that cannot be represented in a single-byte character set
- Return type:
int
- static utf8_check_to_string(n)¶
Return a string describing an integer code returned by
Cnv.utf8_check()
andCnv.utf8_check_file()
.Examples
>>> Cnv.utf8_check_to_string(Cnv.utf8_check("abc".encode())) 'Valid UTF-8, all characters are 7-bit ASCII'