hex2bin.h library reference
The ProtectToolkit-C Software Development Kit (SDK) offers a number of extended API libraries with functionality that is extended to that of the standard PKCS#11 function set.
The following additional features do not form part of the standard PKCS#11 functionality, but are provided by Thales as part of the SafeNet ProtectToolkit-C API within the hex2bin.h library.
hex2bin
Used to convert ASCII HEX strings to binary data.
The function ignores white space in 'hex' and converts pairs of HEX characters into their equivalent binary representation.
Synopsis
int hex2bin(
void * bin,
const char * hex,
unsigned maxLen );
Parameter | Description |
---|---|
bin Output | A buffer to receive the binary data |
hex Input | A string of ASCII HEX characters to be converted |
maxLen Input | The maximum number of characters that 'bin' can hold |
Example
Input -
hex = "41424300"
maxLen = 4
Output -
bin[4] = "ABC"
bin2hex
Converts binary data into an ASCII HEX. This function is the inverse of hex2bin.
Synopsis
int bin2hex(
char * hex,
const void * bin,
unsigned maxLen );
Parameter | Description |
---|---|
bin Input | A buffer of binary data |
hex Output | A buffer to receive the string of ASCII HEX characters |
maxLen Input | The number of characters that 'bin' contains that should be converted (this is not the length of the output buffer 'hex') |
Example
Input -
bin = "abc"
maxLen = 3
Output -
hex[7] = "616263"
bin2hexM
As for bin2hex, converts binary data into an ASCII HEX and then inserts a '\n' after every 'lineLen' characters for display formatting.
Synopsis
int bin2hexM(
char * hex,
const void * bin,
unsigned maxLen,
unsigned int lineLen);
Parameter | Description |
---|---|
bin Input | A buffer of binary data |
hex Output | A buffer to receive the string of ASCII HEX characters |
maxLen Input | The number of characters that 'bin' contains that should be converted (this is not the length of the output buffer 'hex') |
lineLen | Number of characters before a new line (\n) is added |
memdump
This function prints the contents of the memory as binary data to stdout for debugging purposes.
Synopsis
void memdump(
const char * txt,
const unsigned char * buf,
unsigned int len);
Parameter | Description |
---|---|
txt Input | Title string (may be NULL) |
buf Input | Binary data that is to be hex dumped |
len Input | Length of 'buf' |
SetOddParity
Converts a buffer of binary data to 'odd' parity.
For each byte in 'string' this function will flip the least significant bit, if necessary, to make the number of one bits in the entire byte an odd number (odd parity).
Synopsis
void SetOddParity(
unsigned char * string,
unsigned int count);
Parameter | Description |
---|---|
string | Input/output, binary data to convert |
count | Length of 'string' |
isOddParity
This function checks the parity of the supplied data and returns 1 if buffer contains bytes that are all of odd parity.
Synopsis
int isOddParity(
const unsigned char * string,
unsigned int count);
Parameter | Description |
---|---|
string | Input, binary data to check |
count | Input, length of 'string' |