aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eet/src/examples/eet-data-cipher_decipher.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/eet/src/examples/eet-data-cipher_decipher.c')
-rw-r--r--libraries/eet/src/examples/eet-data-cipher_decipher.c119
1 files changed, 119 insertions, 0 deletions
diff --git a/libraries/eet/src/examples/eet-data-cipher_decipher.c b/libraries/eet/src/examples/eet-data-cipher_decipher.c
new file mode 100644
index 0000000..2ef965c
--- /dev/null
+++ b/libraries/eet/src/examples/eet-data-cipher_decipher.c
@@ -0,0 +1,119 @@
1/*
2 * build: gcc -o eet_data_file_cipher_decipher eet-data-file_cipher_decipher.c `pkg-config --cflags --libs eet eina`
3 */
4
5#include <Eina.h>
6#include <Eet.h>
7#include <stdio.h>
8#include <limits.h>
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <unistd.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 char *file = strdup("/tmp/eet_cipher_example_XXXXX");
21 Eet_File *ef;
22 char *test;
23 int size;
24
25 eet_init();
26
27 if (!(file = tmpnam(file)))
28 {
29 fprintf(
30 stderr, "ERROR: could not create temporary file (%s).\n", file);
31 goto panic;
32 }
33
34 /* Crypt an eet file. */
35 ef = eet_open(file, EET_FILE_MODE_WRITE);
36 if (!ef)
37 {
38 fprintf(
39 stderr, "ERROR: could not access file (%s).\n", file);
40 goto error;
41 }
42
43 if (!eet_write_cipher(ef, "keys/tests", buffer, strlen(buffer) + 1, 0, key))
44 {
45 fprintf(
46 stderr, "ERROR: could not access file (%s).\n", file);
47 goto error;
48 }
49
50 eet_close(ef);
51
52 /* Decrypt an eet file. */
53 ef = eet_open(file, EET_FILE_MODE_READ);
54 if (!ef)
55 {
56 fprintf(
57 stderr, "ERROR: could not access file (%s).\n", file);
58 goto error;
59 }
60
61 test = eet_read_cipher(ef, "keys/tests", &size, key);
62 if (!test)
63 {
64 fprintf(
65 stderr, "ERROR: could decript contents on file %s, with key %s.\n",
66 file, key);
67 goto error;
68 }
69
70 if (size != (int)strlen(buffer) + 1)
71 {
72 fprintf(
73 stderr, "ERROR: something is wrong with the decripted data\n");
74 goto error;
75 }
76
77 if (memcmp(test, buffer, strlen(buffer) + 1) != 0)
78 {
79 fprintf(
80 stderr, "ERROR: something is wrong with the decripted data\n");
81 goto error;
82 }
83
84 eet_close(ef);
85
86 /* Decrypt an eet file, now using our BAD key!! */
87 ef = eet_open(file, EET_FILE_MODE_READ);
88 if (!ef)
89 {
90 fprintf(
91 stderr, "ERROR: could not access file (%s).\n", file);
92 goto error;
93 }
94
95 test = eet_read_cipher(ef, "keys/tests", &size, key_bad);
96
97 if (size == (int)strlen(buffer) + 1)
98 if (memcmp(test, buffer, strlen(buffer) + 1) == 0)
99 {
100 fprintf(
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);
104 goto error;
105 }
106
107 eet_close(ef);
108
109error:
110 if (unlink(file) != 0)
111 {
112 fprintf(
113 stderr, "ERROR: could not unlink file (%s).\n", file);
114 }
115
116panic:
117 eet_shutdown();
118}
119