1
votes

Hey guys I'd like to know how to encode a 32 integer array as similar to the python function below? Below is the python code:

def encodePublicKey(key, key_type):
 return b"\x42" + key + b"\x13\x37" + key_type.encode("US-ASCII")

Below is the C code that I have written ( I do believe there are some errors)

#include<stdio.h>

uint8_t value[33] = 
{
   0xED, 0xD3, 0xF5, 0x5C, 0x1A, 0x63, 0x12, 0x58,
   0xD6, 0x9C, 0xF7, 0xA2, 0xDE, 0xF9, 0xDE, 0x14,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
   0x00,
};

void Encode(uint8_t *value) {

}
int main(){
    uint8_t value[33];
    Encode(&value);
}

I'd like to know how to encode and decode the byte array. Thanks.

1

1 Answers

0
votes
// On error, *buf_ptr is NULL and errno is set.
void encodePublicKey(
   const char* key,
   size_t key_size,
   const char *key_type,
   char **buf_ptr,
   size_t *buf_size_ptr
) {
   size_t key_type_len =  strlen( key_type );
   size_t buf_size = 1 + key_size + 2 + key_type_len;

   char *buf = malloc( buf_size );
   if ( buf == NULL ) {
      *buf_ptr = NULL;
      *buf_size_ptr = 0
      return;
   }

   char *p = buf;
   *p = 0x42;                            ++p;
   memcpy( p, key, key_size );           p += key_size;
   *p = 0x13;                            ++p;
   *p = 0x37;                            ++p;
   memcpy( p, key_type, key_type_len );  p += key_size;

   *buf_ptr = buf;
   *buf_size_ptr = buf_size;
}