Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fphammerle/6965e9279f0d49de4ddb530b0ff3ce52 to your computer and use it in GitHub Desktop.
Save fphammerle/6965e9279f0d49de4ddb530b0ff3ce52 to your computer and use it in GitHub Desktop.
// gcc -lgcrypt ...
#include <assert.h>
#include <gcrypt.h>
#include <stdio.h>
#include <stdlib.h>
const size_t KEYGRIP_LENGTH = 20;
unsigned char* read_binary(const char* path, size_t* bytes_read) {
FILE* f = fopen(path, "rb");
fseek(f, 0, SEEK_END);
size_t size = ftell(f);
fseek(f, 0, SEEK_SET);
unsigned char* data = malloc(sizeof(unsigned char) * size);
assert(data != NULL);
*bytes_read = fread(data, sizeof(char), size, f);
assert(*bytes_read == sizeof(char)*size);
fclose(f);
return data;
}
gcry_sexp_t load_sexp_from_file(const char* path) {
size_t size;
char* data = read_binary(path, &size);
assert(data != NULL && size > 0);
gcry_sexp_t key;
gcry_error_t err = gcry_sexp_new(&key, data, size, 0);
if(err) {
fprintf(stderr, "failed load sexp: %s (%d)\n", gcry_strerror(err), gcry_err_code(err));
return NULL;
} else {
return key;
}
}
void dump_keygrip(gcry_sexp_t key) {
unsigned char* keygrip = gcry_pk_get_keygrip(key, NULL);
assert(keygrip != NULL);
for(int i=0; i<KEYGRIP_LENGTH; i++) {
printf("%X", keygrip[i]);
}
printf("\n");
free(keygrip);
}
int main(int argc, const char* argv[]) {
if(!gcry_check_version(GCRYPT_VERSION)) {
fprintf(stderr, "gcrypt version mismatch");
return 1;
}
gcry_sexp_t key = load_sexp_from_file(argv[1]);
assert(key != NULL);
// gcry_sexp_dump(key);
dump_keygrip(key);
gcry_sexp_release(key);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment