text
stringlengths
0
357
{
/* see RFC 4880, 6.2. Forming ASCII Armor, https://tools.ietf.org/html/rfc4880#section-6.2 */
char* base64 = NULL;
char* ret = NULL;
if (key==NULL) {
goto cleanup;
}
if ((base64=dc_key_render_base64(key, 76, "\r\n", 2/*checksum in new line*/))==NULL) { /* RFC: The encoded output stream must be represented in lines of no more than 76 characters each. */
goto cleanup;
}
ret = dc_mprintf("-----BEGIN PGP %s KEY BLOCK-----\r\n%s\r\n%s\r\n-----END PGP %s KEY BLOCK-----\r\n",
key->type==DC_KEY_PUBLIC? "PUBLIC" : "PRIVATE",
add_header_lines? add_header_lines : "",
base64,
key->type==DC_KEY_PUBLIC? "PUBLIC" : "PRIVATE");
cleanup:
free(base64);
return ret;
}
int dc_key_render_asc_to_file(const dc_key_t* key, const char* file, dc_context_t* context /* for logging only */)
{
int success = 0;
char* file_content = NULL;
if (key==NULL || file==NULL || context==NULL) {
goto cleanup;
}
file_content = dc_key_render_asc(key, NULL);
if (file_content==NULL) {
goto cleanup;
}
if (!dc_write_file(context, file, file_content, strlen(file_content))) {
dc_log_error(context, 0, "Cannot write key to %s", file);
goto cleanup;
}
success = 1;
cleanup:
free(file_content);
return success;
}
/* make a fingerprint human-readable */
char* dc_format_fingerprint(const char* fingerprint)
{
int i = 0;
int fingerprint_len = strlen(fingerprint);
dc_strbuilder_t ret;
dc_strbuilder_init(&ret, 0);
while (fingerprint[i]) {
dc_strbuilder_catf(&ret, "%c", fingerprint[i]);
i++;
if (i!=fingerprint_len) {
if (i%20==0) {
dc_strbuilder_cat(&ret, "\n");
}
else if (i%4==0) {
dc_strbuilder_cat(&ret, " ");
}
}
}
return ret.buf;
}
/* bring a human-readable or otherwise formatted fingerprint back to the
40-characters-uppercase-hex format */
char* dc_normalize_fingerprint(const char* in)
{
if (in==NULL) {
return NULL;
}
dc_strbuilder_t out;
dc_strbuilder_init(&out, 0);
const char* p1 = in;
while (*p1) {
if ((*p1 >= '0' && *p1 <= '9') || (*p1 >= 'A' && *p1 <= 'F') || (*p1 >= 'a' && *p1 <= 'f')) {
dc_strbuilder_catf(&out, "%c", toupper(*p1)); /* make uppercase which is needed as we do not search case-insensitive, see comment in dc_sqlite3.c */
}
p1++;
}
return out.buf;
}