print_hex() is always followed by putc('\n'), so let it do it directly

This commit is contained in:
Frank Denis
2015-05-27 21:04:32 +02:00
parent 944857bbf5
commit 5a57615f44
8 changed files with 2 additions and 29 deletions
-1
View File
@@ -52,7 +52,6 @@ auth(void)
fputs("Authentication tag: ", stdout);
print_hex(mac, sizeof mac);
putchar('\n');
puts("Verifying authentication tag...");
ret = crypto_auth_verify(mac, message, message_len, key);
-9
View File
@@ -72,31 +72,25 @@ box(void)
puts("Bob");
fputs("Public key: ", stdout);
print_hex(bob_pk, sizeof bob_pk);
putchar('\n');
fputs("Secret key: ", stdout);
print_hex(bob_sk, sizeof bob_sk);
putchar('\n');
puts("Alice");
fputs("Public key: ", stdout);
print_hex(alice_pk, sizeof alice_pk);
putchar('\n');
fputs("Secret key: ", stdout);
print_hex(alice_sk, sizeof alice_sk);
putchar('\n');
/* nonce must be unique per (key, message) - it can be public and deterministic */
puts("Generating nonce...");
randombytes_buf(nonce, sizeof nonce);
fputs("Nonce: ", stdout);
print_hex(nonce, sizeof nonce);
putchar('\n');
/* read input */
message_len = prompt_input("a message", (char*)message, sizeof message, 1);
print_hex(message, message_len);
putchar('\n');
/* encrypt and authenticate the message */
printf("Encrypting and authenticating with %s\n\n", crypto_box_primitive());
@@ -110,17 +104,14 @@ box(void)
puts("Notice the prepended 16 byte authentication token\n");
fputs("Nonce: ", stdout);
print_hex(nonce, nonce_len);
putchar('\n');
fputs("Ciphertext: ", stdout);
print_hex(ciphertext, ciphertext_len);
putchar('\n');
/* decrypt the message */
puts("Alice verifies and decrypts the ciphertext...");
ret = crypto_box_open_easy(message, ciphertext, ciphertext_len, nonce, bob_pk,
alice_sk);
print_hex(message, message_len);
putchar('\n');
print_verification(ret);
if (ret == 0) {
-10
View File
@@ -72,31 +72,25 @@ box_detached(void)
puts("Bob");
fputs("Public key: ", stdout);
print_hex(bob_pk, sizeof bob_pk);
putchar('\n');
fputs("Secret key: ", stdout);
print_hex(bob_sk, sizeof bob_sk);
putchar('\n');
puts("Alice");
fputs("Public key: ", stdout);
print_hex(alice_pk, sizeof alice_pk);
putchar('\n');
fputs("Secret key: ", stdout);
print_hex(alice_sk, sizeof alice_sk);
putchar('\n');
/* nonce must be unique per (key, message) - it can be public and deterministic */
puts("Generating nonce...");
randombytes_buf(nonce, sizeof nonce);
fputs("Nonce: ", stdout);
print_hex(nonce, sizeof nonce);
putchar('\n');
/* read input */
message_len = prompt_input("a message", (char*)message, sizeof message, 1);
print_hex(message, message_len);
putchar('\n');
/* encrypt and authenticate the message */
printf("Encrypting and authenticating with %s\n\n", crypto_box_primitive());
@@ -107,20 +101,16 @@ box_detached(void)
puts("Bob sends the nonce, the MAC and the ciphertext...\n");
fputs("Nonce: ", stdout);
print_hex(nonce, sizeof nonce);
putchar('\n');
fputs("MAC: ", stdout);
print_hex(mac, sizeof mac);
putchar('\n');
fputs("Ciphertext: ", stdout);
print_hex(ciphertext, message_len);
putchar('\n');
/* decrypt the message */
puts("Alice verifies the MAC and decrypts the ciphertext...");
ret = crypto_box_open_detached(message, ciphertext, mac, message_len, nonce,
bob_pk, alice_sk);
print_hex(message, message_len);
putchar('\n');
print_verification(ret);
if (ret == 0) {
-1
View File
@@ -71,7 +71,6 @@ generichash(void)
fputs("Hash: ", stdout);
print_hex(hash, sizeof hash);
}
putchar('\n');
}
int
-1
View File
@@ -49,7 +49,6 @@ generichash_stream(void)
fputs("Hash: ", stdout);
print_hex(hash, sizeof hash);
putchar('\n');
}
int
-1
View File
@@ -49,7 +49,6 @@ shorthash(void)
crypto_shorthash(hash, message, message_len, key);
fputs("Hash: ", stdout);
print_hex(hash, sizeof hash);
putchar('\n');
}
int
-4
View File
@@ -37,10 +37,8 @@ sign(void)
fputs("Public key: ", stdout);
print_hex(pk, sizeof pk);
putchar('\n');
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.");
@@ -52,13 +50,11 @@ sign(void)
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(message_signed, crypto_sign_BYTES);
putchar('\n');
fputs("Message: ", stdout);
fwrite(message_signed + crypto_sign_BYTES, 1U,
message_signed_len - crypto_sign_BYTES, stdout);
+2 -2
View File
@@ -12,7 +12,7 @@
/*
* print_hex() is a wrapper around sodium_bin2hex() which allocates
* temporary memory then immediately prints the result.
* temporary memory then immediately prints the result followed by \n
*/
void
print_hex(const void *bin, const size_t bin_len)
@@ -31,7 +31,7 @@ print_hex(const void *bin, const size_t bin_len)
if (sodium_bin2hex(hex, hex_size, bin, bin_len) == NULL) {
abort();
}
fputs(hex, stdout);
puts(hex);
free(hex);
}