-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtweetnacl-keypair.c
33 lines (26 loc) · 1015 Bytes
/
tweetnacl-keypair.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <string.h>
#include "tools.h"
#include "tweetnacl.h"
void output_key(char filename[], unsigned char key[], int key_size) {
// http://stackoverflow.com/a/8004250
if (strcmp(filename, "-") != 0) {
FILE *out = create_file(filename);
fwrite(key, key_size, 1, out);
fclose(out);
} else {
fwrite(bytes_to_hex(key, key_size), key_size * 2, 1, stdout);
fputs("\n", stdout);
}
}
int main(int argc, char *argv[]) {
if (argc != 3) error(2, "Usage: tweetnacl-keypair key.pub key.sec");
unsigned char public_key[crypto_box_PUBLICKEYBYTES];
unsigned char secret_key[crypto_box_SECRETKEYBYTES];
crypto_box_keypair(public_key, secret_key);
if (file_exists(argv[1])) errorf(1, "File <%s> exists", argv[1]);
if (file_exists(argv[2])) errorf(1, "File <%s> exists", argv[2]);
output_key(argv[1], public_key, crypto_box_PUBLICKEYBYTES);
output_key(argv[2], secret_key, crypto_box_SECRETKEYBYTES);
return 0;
}