-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17028 from geoffw0/cryptodoc
C++: Improve query doc advice for using encryption
- Loading branch information
Showing
5 changed files
with
49 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,31 @@ | ||
void writeCredentials() { | ||
char *password = "cleartext password"; | ||
FILE* file = fopen("credentials.txt", "w"); | ||
|
||
#include <sodium.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
void writeCredentialsBad(FILE *file, const char *cleartextCredentials) { | ||
// BAD: write password to disk in cleartext | ||
fputs(password, file); | ||
|
||
// GOOD: encrypt password first | ||
char *encrypted = encrypt(password); | ||
fputs(encrypted, file); | ||
fputs(cleartextCredentials, file); | ||
} | ||
|
||
int writeCredentialsGood(FILE *file, const char *cleartextCredentials, const unsigned char *key, const unsigned char *nonce) { | ||
size_t credentialsLen = strlen(cleartextCredentials); | ||
size_t ciphertext_len = crypto_secretbox_MACBYTES + credentialsLen; | ||
unsigned char *ciphertext = malloc(ciphertext_len); | ||
if (!ciphertext) { | ||
logError(); | ||
return -1; | ||
} | ||
|
||
// encrypt the password first | ||
if (crypto_secretbox_easy(ciphertext, (const unsigned char *)cleartextCredentials, credentialsLen, nonce, key) != 0) { | ||
free(ciphertext); | ||
logError(); | ||
return -1; | ||
} | ||
|
||
// GOOD: write encrypted password to disk | ||
fwrite(ciphertext, 1, ciphertext_len, file); | ||
|
||
free(ciphertext); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters