Eet data cipher/decipher example

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", tmpf);
goto error;
}
if (!eet_write_cipher(ef, "keys/tests", buffer, strlen(buffer) + 1, 0, key))
{
fprintf(
stderr, "ERROR: could not access file (%s).\n", tmpf);
goto error;
}
eet_close(ef);
EAPI int eet_write_cipher(Eet_File *ef, const char *name, const void *data, int size, int compress, const char *cipher_key)
Writes a specified entry to an eet file handle using a cipher.
Definition: eet_lib.c:2342
EAPI Eet_Error eet_close(Eet_File *ef)
Closes an eet file handle and flush pending writes.
Definition: eet_lib.c:1899
EAPI Eet_File * eet_open(const char *file, Eet_File_Mode mode)
Opens an eet file on disk, and returns a handle to it.
Definition: eet_lib.c:1499
@ EET_FILE_MODE_WRITE
File is write-only.
Definition: Eet.h:480

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", tmpf);
goto error;
}
test = eet_read_cipher(ef, "keys/tests", &size, key);
if (!test)
{
fprintf(
stderr, "ERROR: could decript contents on file %s, with key %s.\n",
tmpf, 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;
}
eet_close(ef);
EAPI void * eet_read_cipher(Eet_File *ef, const char *name, int *size_ret, const char *cipher_key)
Reads a specified entry from an eet file and return data using a cipher.
Definition: eet_lib.c:1905
@ EET_FILE_MODE_READ
File is read-only.
Definition: Eet.h:479
/* Decrypt an eet file, now using our BAD key!! */
if (!ef)
{
fprintf(
stderr, "ERROR: could not access file (%s).\n", tmpf);
goto error;
}
test = eet_read_cipher(ef, "keys/tests", &size, key_bad);
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", tmpf);
goto error;
}
eet_close(ef);

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.

1//Compile with:
2// gcc -o eet-data-cipher_decipher eet-data-cipher_decipher.c `pkg-config --cflags --libs eet eina`
3
4#include <Eina.h>
5#include <Eet.h>
6#include <stdio.h>
7#include <limits.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <unistd.h>
11#include <string.h>
12
13int
14main(void)
15{
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";
19
20 Eet_File *ef;
21 char *test;
22 int size;
23 int tmpfd;
24 Eina_Tmpstr *tmpf = NULL;
25
26 eet_init();
27
28 if (-1 == (tmpfd = eina_file_mkstemp("eet_cipher_example_XXXXXX", &tmpf)) || !!close(tmpfd))
29 {
30 fprintf(
31 stderr, "ERROR: could not create temporary file (%s) : %s\n",
32 tmpf, strerror(errno));
33 goto panic;
34 }
35
36 /* Crypt an eet file. */
37 ef = eet_open(tmpf, EET_FILE_MODE_WRITE);
38 if (!ef)
39 {
40 fprintf(
41 stderr, "ERROR: could not access file (%s).\n", tmpf);
42 goto error;
43 }
44
45 if (!eet_write_cipher(ef, "keys/tests", buffer, strlen(buffer) + 1, 0, key))
46 {
47 fprintf(
48 stderr, "ERROR: could not access file (%s).\n", tmpf);
49 goto error;
50 }
51
52 eet_close(ef);
53
54 /* Decrypt an eet file. */
55 ef = eet_open(tmpf, EET_FILE_MODE_READ);
56 if (!ef)
57 {
58 fprintf(
59 stderr, "ERROR: could not access file (%s).\n", tmpf);
60 goto error;
61 }
62
63 test = eet_read_cipher(ef, "keys/tests", &size, key);
64 if (!test)
65 {
66 fprintf(
67 stderr, "ERROR: could decript contents on file %s, with key %s.\n",
68 tmpf, key);
69 goto error;
70 }
71
72 if (size != (int)strlen(buffer) + 1)
73 {
74 fprintf(
75 stderr, "ERROR: something is wrong with the decripted data\n");
76 goto error;
77 }
78
79 if (memcmp(test, buffer, strlen(buffer) + 1) != 0)
80 {
81 fprintf(
82 stderr, "ERROR: something is wrong with the decripted data\n");
83 goto error;
84 }
85
86 eet_close(ef);
87
88 /* Decrypt an eet file, now using our BAD key!! */
89 ef = eet_open(tmpf, EET_FILE_MODE_READ);
90 if (!ef)
91 {
92 fprintf(
93 stderr, "ERROR: could not access file (%s).\n", tmpf);
94 goto error;
95 }
96
97 test = eet_read_cipher(ef, "keys/tests", &size, key_bad);
98
99 if (size == (int)strlen(buffer) + 1)
100 if (memcmp(test, buffer, strlen(buffer) + 1) == 0)
101 {
102 fprintf(
103 stderr, "ERROR: something is wrong with the contents of %s, as"
104 " we accessed it with a different key and it decripted our"
105 " information right.\n", tmpf);
106 goto error;
107 }
108
109 eet_close(ef);
110
111error:
112 if (unlink(tmpf) != 0)
113 {
114 fprintf(
115 stderr, "ERROR: could not unlink file (%s)%d.\n", tmpf, errno);
116 }
117 eina_tmpstr_del(tmpf);
118
119panic:
120 eet_shutdown();
121}
122
The file that provides the eet functions.
Eina Utility library.
struct _Eet_File Eet_File
Opaque handle that defines an Eet file (or memory).
Definition: Eet.h:527
EAPI int eet_init(void)
Initializes the EET library.
Definition: eet_lib.c:540
EAPI int eet_shutdown(void)
Shuts down the EET library.
Definition: eet_lib.c:594
EINA_API int eina_file_mkstemp(const char *templatename, Eina_Tmpstr **path)
Generates and creates a uniquely named temporary file from a template name.
Definition: eina_file_posix.c:1514
EINA_API void eina_tmpstr_del(Eina_Tmpstr *tmpstr)
Deletes the temporary string if it is one, or ignore it if it is not.
Definition: eina_tmpstr.c:125
const char Eina_Tmpstr
Interchangeable with "const char *" but still a good visual hint for the purpose.
Definition: eina_tmpstr.h:121