In this example, we exemplify the usage of eet_write_cipher() and eet_read_cipher().
For it to work, make sure to have your Eet installation with a ciphering backend enabled.
We start by defining the information to record in an Eet file (buffer
), the key to cipher that (key
) and a dummy wrong key to try to access that information, later (key_bad
).
const char *buffer = "Here is a string of data to save !";
const char *key = "This is a crypto key";
const char *key_bad = "This is another crypto key";
After opening our file, we simply use the first cited function to write our string ciphered:
if (!ef)
{
fprintf(
stderr, "ERROR: could not access file (%s).\n", file);
goto error;
}
{
fprintf(
stderr, "ERROR: could not access file (%s).\n", file);
goto error;
}
Then, after closing it on purpose, we open it again, to retrieve the encrypted information back, in a readable format:
if (!ef)
{
fprintf(
stderr, "ERROR: could not access file (%s).\n", file);
goto error;
}
if (!test)
{
fprintf(
stderr, "ERROR: could decript contents on file %s, with key %s.\n",
file, key);
goto error;
}
if (size != (int)strlen(buffer) + 1)
{
fprintf(
stderr, "ERROR: something is wrong with the decripted data\n");
goto error;
}
if (memcmp(test, buffer, strlen(buffer) + 1) != 0)
{
fprintf(
stderr, "ERROR: something is wrong with the decripted data\n");
goto error;
}
if (!ef)
{
fprintf(
stderr, "ERROR: could not access file (%s).\n", file);
goto error;
}
if (size == (int)strlen(buffer) + 1)
if (memcmp(test, buffer, strlen(buffer) + 1) == 0)
{
fprintf(
stderr, "ERROR: something is wrong with the contents of %s, as"
" we accessed it with a different key and it decripted our"
" information right.\n", file);
goto error;
}
Note that we do it twice, being the last time with the wrong key. In this last case, if the information is read back and matches the original buffer
, something wrong is going on (we made it to fail on purpose). The former access is OK, and must work.
What we do in sequence is just to delete the file. The complete code of the example follows.
16 const char *buffer =
"Here is a string of data to save !";
17 const char *key =
"This is a crypto key";
18 const char *key_bad =
"This is another crypto key";
20 char *file = strdup(
"/tmp/eet_cipher_example_XXXXX");
27 if (!(file = tmpnam(file)))
30 stderr,
"ERROR: could not create temporary file (%s).\n", file);
39 stderr,
"ERROR: could not access file (%s).\n", file);
46 stderr,
"ERROR: could not access file (%s).\n", file);
57 stderr,
"ERROR: could not access file (%s).\n", file);
65 stderr,
"ERROR: could decript contents on file %s, with key %s.\n",
70 if (size != (
int)strlen(buffer) + 1)
73 stderr,
"ERROR: something is wrong with the decripted data\n");
77 if (memcmp(test, buffer, strlen(buffer) + 1) != 0)
80 stderr,
"ERROR: something is wrong with the decripted data\n");
91 stderr,
"ERROR: could not access file (%s).\n", file);
97 if (size == (
int)strlen(buffer) + 1)
98 if (memcmp(test, buffer, strlen(buffer) + 1) == 0)
101 stderr,
"ERROR: something is wrong with the contents of %s, as"
102 " we accessed it with a different key and it decripted our"
103 " information right.\n", file);
110 if (unlink(file) != 0)
113 stderr,
"ERROR: could not unlink file (%s).\n", file);
EAPI Eet_File * eet_open(const char *file, Eet_File_Mode mode)
Open an eet file on disk, and returns a handle to it.
Definition: eet_lib.c:1419
Definition: Eet_private.h:74
EAPI void * eet_read_cipher(Eet_File *ef, const char *name, int *size_ret, const char *cipher_key)
Read a specified entry from an eet file and return data using a cipher.
Definition: eet_lib.c:1665
File is read-only.
Definition: Eet.h:513
EAPI int eet_shutdown(void)
Shut down the EET library.
Definition: eet_lib.c:634
EAPI int eet_write_cipher(Eet_File *ef, const char *name, const void *data, int size, int compress, const char *cipher_key)
Write a specified entry to an eet file handle using a cipher.
Definition: eet_lib.c:2264
The file that provides the eet functions.
File is write-only.
Definition: Eet.h:514
EAPI Eet_Error eet_close(Eet_File *ef)
Close an eet file handle and flush pending writes.
Definition: eet_lib.c:1659
EAPI int eet_init(void)
Initialize the EET library.
Definition: eet_lib.c:553