aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eet/src/examples/eet-data-cipher_decipher.c
blob: 2ef965c7458d0453a69789d55d9b1df01faa1fdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * build: gcc -o eet_data_file_cipher_decipher eet-data-file_cipher_decipher.c `pkg-config --cflags --libs eet eina`
 */

#include <Eina.h>
#include <Eet.h>
#include <stdio.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int
main(void)
{
   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";

   char *file = strdup("/tmp/eet_cipher_example_XXXXX");
   Eet_File *ef;
   char *test;
   int size;

   eet_init();

   if (!(file = tmpnam(file)))
     {
        fprintf(
          stderr, "ERROR: could not create temporary file (%s).\n", file);
        goto panic;
     }

   /* Crypt an eet file. */
   ef = eet_open(file, EET_FILE_MODE_WRITE);
   if (!ef)
     {
        fprintf(
          stderr, "ERROR: could not access file (%s).\n", file);
        goto error;
     }

   if (!eet_write_cipher(ef, "keys/tests", buffer, strlen(buffer) + 1, 0, key))
     {
        fprintf(
          stderr, "ERROR: could not access file (%s).\n", file);
        goto error;
     }

   eet_close(ef);

   /* Decrypt an eet file. */
   ef = eet_open(file, EET_FILE_MODE_READ);
   if (!ef)
     {
        fprintf(
          stderr, "ERROR: could not access file (%s).\n", file);
        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",
          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;
     }

   eet_close(ef);

   /* Decrypt an eet file, now using our BAD key!! */
   ef = eet_open(file, EET_FILE_MODE_READ);
   if (!ef)
     {
        fprintf(
          stderr, "ERROR: could not access file (%s).\n", file);
        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", file);
          goto error;
       }

   eet_close(ef);

error:
   if (unlink(file) != 0)
     {
        fprintf(
          stderr, "ERROR: could not unlink file (%s).\n", file);
     }

panic:
   eet_shutdown();
}