Demos: let prompt_input() add extra \n itself
This commit is contained in:
parent
9b209f0078
commit
a313e0be66
@ -45,9 +45,7 @@ auth(void)
|
||||
puts("Example: crypto_auth\n");
|
||||
|
||||
prompt_input("a key", (char*)key, sizeof key, 0);
|
||||
|
||||
message_len = prompt_input("a message", (char*)message, sizeof message, 1);
|
||||
putchar('\n');
|
||||
|
||||
printf("Generating %s authentication...\n", crypto_auth_primitive());
|
||||
crypto_auth(mac, message, message_len, key);
|
||||
|
@ -61,9 +61,7 @@ generichash(void)
|
||||
puts("Example: crypto_generichash\n");
|
||||
|
||||
key_len = prompt_input("a key", (char*)key, sizeof key, 1);
|
||||
|
||||
message_len = prompt_input("a message", (char*)message, sizeof message, 1);
|
||||
putchar('\n');
|
||||
|
||||
printf("Hashing message with %s\n", crypto_generichash_primitive());
|
||||
if (crypto_generichash(hash, sizeof hash, message, message_len,
|
||||
|
@ -27,7 +27,6 @@ generichash_stream(void)
|
||||
puts("Example: crypto_generichashstream\n");
|
||||
|
||||
prompt_input("a key", (char*)key, sizeof key, 1);
|
||||
putchar('\n');
|
||||
|
||||
printf("Hashing message with %s\n", crypto_generichash_primitive());
|
||||
|
||||
|
@ -43,9 +43,7 @@ shorthash(void)
|
||||
puts("Example: crypto_shorthash\n");
|
||||
|
||||
prompt_input("a key", (char*)key, sizeof key, 0);
|
||||
|
||||
message_len = prompt_input("a message", (char*)message, sizeof message, 1);
|
||||
putchar('\n');
|
||||
|
||||
printf("Hashing the message with %s\n", crypto_shorthash_primitive());
|
||||
crypto_shorthash(hash, message, message_len, key);
|
||||
|
57
demos/sign.c
57
demos/sign.c
@ -22,59 +22,60 @@
|
||||
static int
|
||||
sign(void)
|
||||
{
|
||||
unsigned char pk[crypto_sign_PUBLICKEYBYTES]; /* Bob public */
|
||||
unsigned char sk[crypto_sign_SECRETKEYBYTES]; /* Bob secret */
|
||||
unsigned char m[MAX_INPUT_SIZE]; /* message */
|
||||
unsigned char sm[MAX_INPUT_SIZE + crypto_sign_BYTES]; /* signed message */
|
||||
unsigned long long int mlen; /* message length */
|
||||
unsigned long long int smlen; /* signed length */
|
||||
int r;
|
||||
unsigned char pk[crypto_sign_PUBLICKEYBYTES]; /* Bob's public key */
|
||||
unsigned char sk[crypto_sign_SECRETKEYBYTES]; /* Bob's secret key */
|
||||
unsigned char message[MAX_INPUT_SIZE];
|
||||
unsigned char message_signed[crypto_sign_BYTES + MAX_INPUT_SIZE];
|
||||
unsigned long long message_len;
|
||||
unsigned long long message_signed_len;
|
||||
int ret;
|
||||
|
||||
puts("Example: crypto_sign\n");
|
||||
|
||||
puts("Generating keypair...");
|
||||
crypto_sign_keypair(pk, sk); /* generate Bob's keys */
|
||||
|
||||
fputs("Public: ", stdout);
|
||||
fputs("Public key: ", stdout);
|
||||
print_hex(pk, sizeof pk);
|
||||
putchar('\n');
|
||||
fputs("Secret: ", stdout);
|
||||
fputs("Secret key: ", stdout);
|
||||
print_hex(sk, sizeof sk);
|
||||
putchar('\n');
|
||||
puts("The secret key, as returned by crypto_sign_keypair(), actually includes "
|
||||
"a copy of the public key, in order to avoid a scalar multiplication "
|
||||
"when signing messages.");
|
||||
|
||||
mlen = prompt_input("a message", (char*)m, sizeof m, 1);
|
||||
putchar('\n');
|
||||
|
||||
puts("Notice the message has no prepended padding");
|
||||
print_hex(m, mlen);
|
||||
putchar('\n');
|
||||
message_len = prompt_input("a message", (char*)message, sizeof message, 1);
|
||||
|
||||
printf("Signing message with %s...\n", crypto_sign_primitive());
|
||||
crypto_sign(sm, &smlen, m, mlen, sk);
|
||||
crypto_sign(message_signed, &message_signed_len, message, message_len, sk);
|
||||
|
||||
puts("Notice the signed message has prepended signature");
|
||||
print_hex(sm, smlen);
|
||||
printf("Signed message:");
|
||||
print_hex(message_signed, message_signed_len);
|
||||
putchar('\n');
|
||||
printf("A %u bytes signature was prepended to the message\n",
|
||||
crypto_sign_BYTES);
|
||||
|
||||
fputs("Signature: ", stdout);
|
||||
print_hex(sm, crypto_sign_BYTES);
|
||||
print_hex(message_signed, crypto_sign_BYTES);
|
||||
putchar('\n');
|
||||
fputs("Message: ", stdout);
|
||||
fwrite(sm + crypto_sign_BYTES, 1U, smlen - crypto_sign_BYTES, stdout);
|
||||
fwrite(message_signed + crypto_sign_BYTES, 1U,
|
||||
message_signed_len - crypto_sign_BYTES, stdout);
|
||||
putchar('\n');
|
||||
|
||||
puts("Validating message...");
|
||||
r = crypto_sign_open(m, &mlen, sm, smlen, pk);
|
||||
|
||||
print_verification(r);
|
||||
if (r == 0)
|
||||
printf("Message: %s\n", m);
|
||||
ret = crypto_sign_open(message, &message_len, message_signed,
|
||||
message_signed_len, pk);
|
||||
print_verification(ret);
|
||||
if (ret == 0)
|
||||
printf("Message: %s\n", message);
|
||||
|
||||
sodium_memzero(sk, sizeof sk); /* wipe sensitive data */
|
||||
sodium_memzero(m, sizeof m);
|
||||
sodium_memzero(sm, sizeof sm);
|
||||
sodium_memzero(message, sizeof message);
|
||||
sodium_memzero(message_signed, sizeof message_signed);
|
||||
|
||||
return r;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -52,9 +52,9 @@ prompt_input(const char *prompt, char *input, const size_t max_input_len,
|
||||
size_t actual_input_len;
|
||||
|
||||
if (variable_length != 0) {
|
||||
printf("Enter %s (%zu bytes max) > ", prompt, max_input_len);
|
||||
printf("\nEnter %s (%zu bytes max) > ", prompt, max_input_len);
|
||||
} else {
|
||||
printf("Enter %s (%zu bytes) > ", prompt, max_input_len);
|
||||
printf("\nEnter %s (%zu bytes) > ", prompt, max_input_len);
|
||||
}
|
||||
fflush(stdout);
|
||||
fgets(input_tmp, sizeof input_tmp, stdin);
|
||||
@ -67,14 +67,14 @@ prompt_input(const char *prompt, char *input, const size_t max_input_len,
|
||||
}
|
||||
|
||||
if (actual_input_len > max_input_len) {
|
||||
printf("Warning: truncating input to %zu bytes\n", max_input_len);
|
||||
printf("Warning: truncating input to %zu bytes\n\n", max_input_len);
|
||||
actual_input_len = max_input_len;
|
||||
} else if (actual_input_len < max_input_len && variable_length == 0) {
|
||||
printf("Warning: %zu bytes expected, %zu bytes given: padding with zeros\n",
|
||||
printf("Warning: %zu bytes expected, %zu bytes given: padding with zeros\n\n",
|
||||
max_input_len, actual_input_len);
|
||||
memset(input, 0, max_input_len);
|
||||
} else {
|
||||
printf("Length: %zu bytes\n", actual_input_len);
|
||||
printf("Length: %zu bytes\n\n", actual_input_len);
|
||||
}
|
||||
|
||||
memcpy(input, input_tmp, actual_input_len);
|
||||
|
Loading…
Reference in New Issue
Block a user