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

\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{comment}{//Compile with:}}
\DoxyCodeLine{\textcolor{comment}{// gcc -\/o eet-\/data-\/cipher\_decipher eet-\/data-\/cipher\_decipher.c `pkg-\/config -\/-\/cflags -\/-\/libs eet eina`}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <\mbox{\hyperlink{Eina_8h}{Eina.h}}>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <\mbox{\hyperlink{Eet_8h}{Eet.h}}>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <stdio.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <limits.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <sys/types.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <sys/stat.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <unistd.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <string.h>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *buffer = \textcolor{stringliteral}{"Here is a string of data to save !"};}
\DoxyCodeLine{   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *key = \textcolor{stringliteral}{"This is a crypto key"};}
\DoxyCodeLine{   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *key\_bad = \textcolor{stringliteral}{"This is another crypto key"};}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordtype}{char} *file = strdup(\textcolor{stringliteral}{"/tmp/eet\_cipher\_example\_XXXXXX"});}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eet__File__Group_ga8d9779184a9870c1a225f1f40c76e8a7}{Eet\_File}} *ef;}
\DoxyCodeLine{   \textcolor{keywordtype}{char} *test;}
\DoxyCodeLine{   \textcolor{keywordtype}{int} size;}
\DoxyCodeLine{   \textcolor{keywordtype}{int} tmpfd;}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eet__Group_ga9903687bfa20fe9a9e59d4c1d213e89a}{eet\_init}}();}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (-\/1 == (tmpfd = mkstemp(file)) || !!close(tmpfd))}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        fprintf(}
\DoxyCodeLine{                stderr, \textcolor{stringliteral}{"ERROR: could not create temporary file (\%s) : \%s\(\backslash\)n"},}
\DoxyCodeLine{                file, strerror(errno));}
\DoxyCodeLine{        \textcolor{keywordflow}{goto} panic;}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{/* Crypt an eet file. */}}
\DoxyCodeLine{   ef = \mbox{\hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}}(file, \mbox{\hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a8f395f56fba48be007c65be51d799ee9}{EET\_FILE\_MODE\_WRITE}});}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (!ef)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        fprintf(}
\DoxyCodeLine{          stderr, \textcolor{stringliteral}{"ERROR: could not access file (\%s).\(\backslash\)n"}, file);}
\DoxyCodeLine{        \textcolor{keywordflow}{goto} error;}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \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))}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        fprintf(}
\DoxyCodeLine{          stderr, \textcolor{stringliteral}{"ERROR: could not access file (\%s).\(\backslash\)n"}, file);}
\DoxyCodeLine{        \textcolor{keywordflow}{goto} error;}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}}(ef);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{/* Decrypt an eet file. */}}
\DoxyCodeLine{   ef = \mbox{\hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}}(file, \mbox{\hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a416f7e9a76b8131a44bbeedb13060c81}{EET\_FILE\_MODE\_READ}});}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (!ef)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        fprintf(}
\DoxyCodeLine{          stderr, \textcolor{stringliteral}{"ERROR: could not access file (\%s).\(\backslash\)n"}, file);}
\DoxyCodeLine{        \textcolor{keywordflow}{goto} error;}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   test = \mbox{\hyperlink{group__Eet__File__Cipher__Group_gafde124ec4aa3d6ad934e7cc43c24ae3a}{eet\_read\_cipher}}(ef, \textcolor{stringliteral}{"keys/tests"}, \&size, key);}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (!test)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        fprintf(}
\DoxyCodeLine{          stderr, \textcolor{stringliteral}{"ERROR: could decript contents on file \%s, with key \%s.\(\backslash\)n"},}
\DoxyCodeLine{          file, key);}
\DoxyCodeLine{        \textcolor{keywordflow}{goto} error;}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (size != (\textcolor{keywordtype}{int})strlen(buffer) + 1)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        fprintf(}
\DoxyCodeLine{          stderr, \textcolor{stringliteral}{"ERROR: something is wrong with the decripted data\(\backslash\)n"});}
\DoxyCodeLine{        \textcolor{keywordflow}{goto} error;}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (memcmp(test, buffer, strlen(buffer) + 1) != 0)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        fprintf(}
\DoxyCodeLine{          stderr, \textcolor{stringliteral}{"ERROR: something is wrong with the decripted data\(\backslash\)n"});}
\DoxyCodeLine{        \textcolor{keywordflow}{goto} error;}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}}(ef);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{/* Decrypt an eet file, now using our BAD key!! */}}
\DoxyCodeLine{   ef = \mbox{\hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}}(file, \mbox{\hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a416f7e9a76b8131a44bbeedb13060c81}{EET\_FILE\_MODE\_READ}});}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (!ef)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        fprintf(}
\DoxyCodeLine{          stderr, \textcolor{stringliteral}{"ERROR: could not access file (\%s).\(\backslash\)n"}, file);}
\DoxyCodeLine{        \textcolor{keywordflow}{goto} error;}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   test = \mbox{\hyperlink{group__Eet__File__Cipher__Group_gafde124ec4aa3d6ad934e7cc43c24ae3a}{eet\_read\_cipher}}(ef, \textcolor{stringliteral}{"keys/tests"}, \&size, key\_bad);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (size == (\textcolor{keywordtype}{int})strlen(buffer) + 1)}
\DoxyCodeLine{     \textcolor{keywordflow}{if} (memcmp(test, buffer, strlen(buffer) + 1) == 0)}
\DoxyCodeLine{       \{}
\DoxyCodeLine{          fprintf(}
\DoxyCodeLine{            stderr, \textcolor{stringliteral}{"ERROR: something is wrong with the contents of \%s, as"}}
\DoxyCodeLine{                    \textcolor{stringliteral}{" we accessed it with a different key and it decripted our"}}
\DoxyCodeLine{                    \textcolor{stringliteral}{" information right.\(\backslash\)n"}, file);}
\DoxyCodeLine{          \textcolor{keywordflow}{goto} error;}
\DoxyCodeLine{       \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}}(ef);}
\DoxyCodeLine{}
\DoxyCodeLine{error:}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (unlink(file) != 0)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        fprintf(}
\DoxyCodeLine{          stderr, \textcolor{stringliteral}{"ERROR: could not unlink file (\%s).\(\backslash\)n"}, file);}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{panic:}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eet__Group_gacbc7583179399d3b3e7b003e911f866f}{eet\_shutdown}}();}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\end{DoxyCodeInclude}
 