#include "sha.h"
#include <Eina.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>

void
sha1_stat(const struct stat *st, unsigned char dst[20])
{
   char buf[128];

#ifdef STAT_NSEC
# if (defined __USE_MISC && defined st_mtime)
#  define STAT_NSEC_MTIME(st) (unsigned long long)((st)->st_mtim.tv_nsec)
#  define STAT_NSEC_CTIME(st) (unsigned long long)((st)->st_ctim.tv_nsec)
# else
#  define STAT_NSEC_MTIME(st) (unsigned long long)((st)->st_mtimensec)
#  define STAT_NSEC_CTIME(st) (unsigned long long)((st)->st_ctimensec)
# endif
#else
# define STAT_NSEC_MTIME(st) (unsigned long long)(0)
# define STAT_NSEC_CTIME(st) (unsigned long long)(0)
#endif

   snprintf(buf, sizeof(buf),
            "%llu %llu %llu %llu %llu %llu %llu %llu",
            (unsigned long long)(st->st_mode),
            (unsigned long long)(st->st_uid),
            (unsigned long long)(st->st_gid),
            (unsigned long long)(st->st_size),
            (unsigned long long)(st->st_mtime),
            (unsigned long long)(st->st_ctime),
            STAT_NSEC_MTIME(st),
            STAT_NSEC_CTIME(st));
   eina_sha1((unsigned char *)buf, strlen(buf), dst);
}

void
sha1_str(unsigned char sha[20], char shastr[41])
{
   const char *chmap = "0123456789abcdef";
   int i;

   for (i = 0; i < 20; i++)
     {
        shastr[(i * 2)    ] = chmap[(sha[i] >> 4) & 0xf];
        shastr[(i * 2) + 1] = chmap[ sha[i]       & 0xf];
     }
   shastr[i * 2] = 0;
}
