eet-data-cipher_decipher.c

- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Examples

eet-data-cipher_decipher.c
//Compile with:
// gcc -o eet-data-cipher_decipher eet-data-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>
#include <string.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";
Eet_File *ef;
char *test;
int size;
int tmpfd;
Eina_Tmpstr *tmpf = NULL;
eet_init();
if (-1 == (tmpfd = eina_file_mkstemp("eet_cipher_example_XXXXXX", &tmpf)) || !!close(tmpfd))
{
fprintf(
stderr, "ERROR: could not create temporary file (%s) : %s\n",
tmpf, strerror(errno));
goto panic;
}
/* Crypt an eet file. */
ef = eet_open(tmpf, EET_FILE_MODE_WRITE);
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);
/* Decrypt an eet file. */
ef = eet_open(tmpf, EET_FILE_MODE_READ);
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);
/* Decrypt an eet file, now using our BAD key!! */
ef = eet_open(tmpf, EET_FILE_MODE_READ);
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);
error:
if (unlink(tmpf) != 0)
{
fprintf(
stderr, "ERROR: could not unlink file (%s)%d.\n", tmpf, errno);
}
eina_tmpstr_del(tmpf);
panic:
eet_shutdown();
}
Eet.h
The file that provides the eet functions.
Eina.h
Eina Utility library.
eet_write_cipher
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
eet_read_cipher
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_close
EAPI Eet_Error eet_close(Eet_File *ef)
Closes an eet file handle and flush pending writes.
Definition: eet_lib.c:1899
eet_open
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
struct _Eet_File Eet_File
Opaque handle that defines an Eet file (or memory).
Definition: Eet.h:527
EET_FILE_MODE_READ
@ EET_FILE_MODE_READ
File is read-only.
Definition: Eet.h:479
EET_FILE_MODE_WRITE
@ EET_FILE_MODE_WRITE
File is write-only.
Definition: Eet.h:480
eet_init
EAPI int eet_init(void)
Initializes the EET library.
Definition: eet_lib.c:540
eet_shutdown
EAPI int eet_shutdown(void)
Shuts down the EET library.
Definition: eet_lib.c:594
eina_file_mkstemp
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_tmpstr_del
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
Eina_Tmpstr
const char Eina_Tmpstr
Interchangeable with "const char *" but still a good visual hint for the purpose.
Definition: eina_tmpstr.h:121