text
stringlengths
0
357
dc_key_empty(key);
stmt = dc_sqlite3_prepare(sql,
"SELECT private_key FROM keypairs WHERE addr=? AND is_default=1;");
sqlite3_bind_text (stmt, 1, self_addr, -1, SQLITE_STATIC);
if (sqlite3_step(stmt)!=SQLITE_ROW) {
goto cleanup;
}
dc_key_set_from_stmt(key, stmt, 0, DC_KEY_PRIVATE);
success = 1;
cleanup:
sqlite3_finalize(stmt);
return success;
}
/*******************************************************************************
* Render keys
******************************************************************************/
static long crc_octets(const unsigned char *octets, size_t len)
{
#define CRC24_INIT 0xB704CEL
#define CRC24_POLY 0x1864CFBL
long crc = CRC24_INIT;
while (len--) {
crc ^= (*octets++) << 16;
for (int i = 0; i < 8; i++) {
crc <<= 1;
if (crc & 0x1000000)
crc ^= CRC24_POLY;
}
}
return crc & 0xFFFFFFL;
}
char* dc_render_base64(const void* buf, size_t buf_bytes, int break_every, const char* break_chars,
int add_checksum /*0=no checksum, 1=add without break, 2=add with break_chars*/)
{
char* ret = NULL;
if (buf==NULL || buf_bytes<=0) {
goto cleanup;
}
if ((ret = encode_base64((const char*)buf, buf_bytes))==NULL) {
goto cleanup;
}
#if 0
if (add_checksum==1/*appended checksum*/) {
long checksum = crc_octets(buf, buf_bytes);
uint8_t c[3];
c[0] = (uint8_t)((checksum >> 16)&0xFF);
c[1] = (uint8_t)((checksum >> 8)&0xFF);
c[2] = (uint8_t)((checksum)&0xFF);
char* c64 = encode_base64((const char*)c, 3);
char* temp = ret;
ret = dc_mprintf("%s=%s", temp, c64);
free(temp);
free(c64);
}
#endif
if (break_every>0) {
char* temp = ret;
ret = dc_insert_breaks(temp, break_every, break_chars);
free(temp);
}
if (add_checksum==2/*checksum with break character*/) {
long checksum = crc_octets(buf, buf_bytes);
uint8_t c[3];
c[0] = (uint8_t)((checksum >> 16)&0xFF);
c[1] = (uint8_t)((checksum >> 8)&0xFF);
c[2] = (uint8_t)((checksum)&0xFF);
char* c64 = encode_base64((const char*)c, 3);
char* temp = ret;
ret = dc_mprintf("%s%s=%s", temp, break_chars, c64);
free(temp);
free(c64);
}
cleanup:
return ret;
}
char* dc_key_render_base64(const dc_key_t* key, int break_every, const char* break_chars, int add_checksum)
{
if (key==NULL) {
return NULL;
}
return dc_render_base64(key->binary, key->bytes, break_every, break_chars, add_checksum);
}
char* dc_key_render_asc(const dc_key_t* key, const char* add_header_lines /*must be terminated by \r\n*/)