\hypertarget{eet-data-cipher_decipher_8c-example}{}\section{eet-\/data-\/cipher\+\_\+decipher.\+c}

\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{// gcc -o eet-data-cipher\_decipher eet-data-cipher\_decipher.c `pkg-config --cflags --libs eet eina`}

\textcolor{preprocessor}{#include <\mbox{\hyperlink{Eina_8h}{Eina.h}}>}
\textcolor{preprocessor}{#include <\mbox{\hyperlink{Eet_8h}{Eet.h}}>}
\textcolor{preprocessor}{#include <stdio.h>}
\textcolor{preprocessor}{#include <limits.h>}
\textcolor{preprocessor}{#include <sys/types.h>}
\textcolor{preprocessor}{#include <sys/stat.h>}
\textcolor{preprocessor}{#include <unistd.h>}
\textcolor{preprocessor}{#include <string.h>}

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{void})
\{
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *buffer = \textcolor{stringliteral}{"Here is a string of data to save !"};
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *key = \textcolor{stringliteral}{"This is a crypto key"};
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *key\_bad = \textcolor{stringliteral}{"This is another crypto key"};

   \textcolor{keywordtype}{char} *file = strdup(\textcolor{stringliteral}{"/tmp/eet\_cipher\_example\_XXXXXX"});
   \mbox{\hyperlink{group__Eet__File__Group_ga8d9779184a9870c1a225f1f40c76e8a7}{Eet\_File}} *ef;
   \textcolor{keywordtype}{char} *test;
   \textcolor{keywordtype}{int} size;
   \textcolor{keywordtype}{int} tmpfd;

   \mbox{\hyperlink{group__Eet__Group_ga9903687bfa20fe9a9e59d4c1d213e89a}{eet\_init}}();

   \textcolor{keywordflow}{if} (-1 == (tmpfd = \mbox{\hyperlink{group__Evil__Stdlib__Group_gab62e7adedbeaf115d479d057e1c7b795}{mkstemp}}(file)) || !!close(tmpfd))
     \{
        fprintf(
                stderr, \textcolor{stringliteral}{"ERROR: could not create temporary file (%s) : %s\(\backslash\)n"},
                file, strerror(errno));
        \textcolor{keywordflow}{goto} panic;
     \}

   \textcolor{comment}{/* Crypt an eet file. */}
   ef = \mbox{\hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}}(file, \mbox{\hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a8f395f56fba48be007c65be51d799ee9}{EET\_FILE\_MODE\_WRITE}});
   \textcolor{keywordflow}{if} (!ef)
     \{
        fprintf(
          stderr, \textcolor{stringliteral}{"ERROR: could not access file (%s).\(\backslash\)n"}, file);
        \textcolor{keywordflow}{goto} error;
     \}

   \textcolor{keywordflow}{if} (!\mbox{\hyperlink{group__Eet__File__Cipher__Group_ga0c08a5881d6fd3474a8cbe03a445aff3}{eet\_write\_cipher}}(ef, \textcolor{stringliteral}{"keys/tests"}, buffer, strlen(buffer) + 1, 0, key))
     \{
        fprintf(
          stderr, \textcolor{stringliteral}{"ERROR: could not access file (%s).\(\backslash\)n"}, file);
        \textcolor{keywordflow}{goto} error;
     \}

   \mbox{\hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}}(ef);

   \textcolor{comment}{/* Decrypt an eet file. */}
   ef = \mbox{\hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}}(file, \mbox{\hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a416f7e9a76b8131a44bbeedb13060c81}{EET\_FILE\_MODE\_READ}});
   \textcolor{keywordflow}{if} (!ef)
     \{
        fprintf(
          stderr, \textcolor{stringliteral}{"ERROR: could not access file (%s).\(\backslash\)n"}, file);
        \textcolor{keywordflow}{goto} error;
     \}

   test = \mbox{\hyperlink{group__Eet__File__Cipher__Group_gafde124ec4aa3d6ad934e7cc43c24ae3a}{eet\_read\_cipher}}(ef, \textcolor{stringliteral}{"keys/tests"}, &size, key);
   \textcolor{keywordflow}{if} (!test)
     \{
        fprintf(
          stderr, \textcolor{stringliteral}{"ERROR: could decript contents on file %s, with key %s.\(\backslash\)n"},
          file, key);
        \textcolor{keywordflow}{goto} error;
     \}

   \textcolor{keywordflow}{if} (size != (\textcolor{keywordtype}{int})strlen(buffer) + 1)
     \{
        fprintf(
          stderr, \textcolor{stringliteral}{"ERROR: something is wrong with the decripted data\(\backslash\)n"});
        \textcolor{keywordflow}{goto} error;
     \}

   \textcolor{keywordflow}{if} (memcmp(test, buffer, strlen(buffer) + 1) != 0)
     \{
        fprintf(
          stderr, \textcolor{stringliteral}{"ERROR: something is wrong with the decripted data\(\backslash\)n"});
        \textcolor{keywordflow}{goto} error;
     \}

   \mbox{\hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}}(ef);

   \textcolor{comment}{/* Decrypt an eet file, now using our BAD key!! */}
   ef = \mbox{\hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}}(file, \mbox{\hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a416f7e9a76b8131a44bbeedb13060c81}{EET\_FILE\_MODE\_READ}});
   \textcolor{keywordflow}{if} (!ef)
     \{
        fprintf(
          stderr, \textcolor{stringliteral}{"ERROR: could not access file (%s).\(\backslash\)n"}, file);
        \textcolor{keywordflow}{goto} error;
     \}

   test = \mbox{\hyperlink{group__Eet__File__Cipher__Group_gafde124ec4aa3d6ad934e7cc43c24ae3a}{eet\_read\_cipher}}(ef, \textcolor{stringliteral}{"keys/tests"}, &size, key\_bad);

   \textcolor{keywordflow}{if} (size == (\textcolor{keywordtype}{int})strlen(buffer) + 1)
     \textcolor{keywordflow}{if} (memcmp(test, buffer, strlen(buffer) + 1) == 0)
       \{
          fprintf(
            stderr, \textcolor{stringliteral}{"ERROR: something is wrong with the contents of %s, as"}
                    \textcolor{stringliteral}{" we accessed it with a different key and it decripted our"}
                    \textcolor{stringliteral}{" information right.\(\backslash\)n"}, file);
          \textcolor{keywordflow}{goto} error;
       \}

   \mbox{\hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}}(ef);

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

panic:
   \mbox{\hyperlink{group__Eet__Group_gacbc7583179399d3b3e7b003e911f866f}{eet\_shutdown}}();
\}

\end{DoxyCodeInclude}
 