mirror of
https://gitlab.kit.edu/kit/scc/sys/mail/exim-encrypt-dlfunc.git
synced 2025-12-06 12:03:55 +01:00
…
This commit is contained in:
49
src/genkey.c
Normal file
49
src/genkey.c
Normal file
@ -0,0 +1,49 @@
|
||||
#include <sodium.h>
|
||||
|
||||
void dumpkey(FILE* f, unsigned char * name, unsigned char * key, unsigned int keylen) {
|
||||
fprintf(f, "const unsigned char %s[] = { ", name);
|
||||
for(int i=0; i < keylen; i++) {
|
||||
fprintf(f, "0x%02X", key[i]);
|
||||
if (i < keylen-1) {
|
||||
fprintf(f, ", ");
|
||||
}
|
||||
}
|
||||
fprintf(f, " };\n");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
if (sodium_init() < 0) {
|
||||
fputs("Unable to initialize libsodium", stderr);
|
||||
exit(128);
|
||||
}
|
||||
|
||||
unsigned char key[crypto_secretbox_KEYBYTES];
|
||||
crypto_secretbox_keygen(key);
|
||||
|
||||
FILE *keyfile = fopen("secretkey.h", "w+");
|
||||
if (keyfile == NULL) {
|
||||
fputs("Unable to open secretkey.h", stderr);
|
||||
exit(129);
|
||||
}
|
||||
|
||||
fputs("#ifndef EXIM4ENCRYPTSECRETKEY_H\n#define EXIM4ENCRYPTSECRETKEY_H\n\n", keyfile);
|
||||
dumpkey(keyfile, "key", key, crypto_secretbox_KEYBYTES);
|
||||
fprintf(keyfile, "unsigned int keylen = %u;\n", crypto_secretbox_KEYBYTES);
|
||||
fputs("#endif // EXIM4ENCRYPTSECRETKEY_H\n", keyfile);
|
||||
|
||||
fclose(keyfile);
|
||||
|
||||
FILE *keyfilebin = fopen("secretkey.bin", "w+");
|
||||
if (keyfilebin == NULL) {
|
||||
fputs("Unable to open secretkey.bin", stderr);
|
||||
exit(129);
|
||||
}
|
||||
|
||||
fwrite(key, sizeof(key[0]), crypto_secretbox_KEYBYTES, keyfilebin);
|
||||
|
||||
fclose(keyfilebin);
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user