synk

synchronize files between hosts
Log | Files | Refs | README | LICENSE

commit 061a8d1893ef222177f12dcd1d0f9ec84ca572c0
parent 8c80e23c0813a078879f506afca68141a9c060a7
Author: Willy <willyatmailoodotorg>
Date:   Wed Aug 24 08:45:27 +0200

Move sha512 functions to sha512.c

Diffstat:
sha512.c | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sha512.h | 4++++
synk.c | 61-------------------------------------------------------------
3 files changed, 64 insertions(+), 61 deletions(-)
diff --git a/sha512.c b/sha512.c @@ -245,6 +245,66 @@ int sha512_done(sha512_state * md, unsigned char *out) return 0; } +/* + * Return 0 is two sha512 hashes match together, 1 otherwise. + * Hashes MUST be 64 byte long, and not NULL. + */ +int +sha512_compare(unsigned char *h1, unsigned char *h2) +{ + int i; + for (i=0; i<64; i++) { + if (h1[i] != h2[i]) + return 1; + } + + return 0; +} + +/* + * Format a sha512 hash (64 bits long) as an hexadecimal string (128 char) + * A pointer to this char is statically allocated, so multiple calls to this + * function will overwrite the converted value. + */ +char * +sha512_format(unsigned char *hash) +{ + int i; + static char fmt[128] = ""; + + for (i=0; i<64; i++) { + snprintf(fmt+i, 2, "%02x\n", hash[i]); + } + + return fmt; +} + +/* + * Generate sha512 hash from stream, and store it in hash, which must + * be able to store 64 bytes. + */ +int +sha512(FILE *stream, unsigned char *hash) +{ + sha512_state md; + size_t len = 0; + unsigned char buf[128]; + + if (sha512_init(&md) != 0) { + perror("sha512_init"); + return 1; + } + + while ((len = fread(buf, 128, 1, stream)) > 0) { + if (sha512_process(&md, buf, len) != 0) { + return 1; + } + } + + return sha512_done(&md, hash); +} + + /* $Source: /cvs/libtom/libtomcrypt/src/hashes/sha2/sha512.c,v $ */ /* $Revision: 1.10 $ */ /* $Date: 2007/05/12 14:25:28 $ */ diff --git a/sha512.h b/sha512.h @@ -1,3 +1,4 @@ +#include <stdio.h> #include <stddef.h> #include <stdint.h> @@ -11,3 +12,6 @@ typedef struct sha512_state { int sha512_init(sha512_state * md); int sha512_process(sha512_state * md, const unsigned char *in, unsigned long inlen); int sha512_done(sha512_state * md, unsigned char *hash); +int sha512_compare(unsigned char *h1, unsigned char *h2); +int sha512(FILE *stream, unsigned char *hash); +char *sha512_format(unsigned char *hash); diff --git a/synk.c b/synk.c @@ -38,9 +38,6 @@ enum { const char *rsync_cmd[] = { "rsync", "-azEq", "--delete", NULL }; void usage(char *name); -int sha512(FILE *stream, unsigned char *hash); -int sha512_compare(unsigned char *h1, unsigned char *h2); -char *sha512_format(unsigned char *hash); long gettimestamp(const char *path); int handleclient(int cfd, struct in_addr inet); int server(in_addr_t host, in_port_t port); @@ -54,64 +51,6 @@ usage(char *name) } /* - * Generate sha512 hash from stream, and store it in hash, which must - * be able to store 64 bytes. - */ -int -sha512(FILE *stream, unsigned char *hash) -{ - sha512_state md; - size_t len = 0; - unsigned char buf[128]; - - if (sha512_init(&md) != 0) { - perror("sha512_init"); - return 1; - } - - while ((len = fread(buf, 128, 1, stream)) > 0) { - if (sha512_process(&md, buf, len) != 0) { - return 1; - } - } - - return sha512_done(&md, hash); -} - -/* - * Return 0 is two sha512 hashes match together, 1 otherwise. - * Hashes MUST be 64 byte long, and not NULL. - */ -int -sha512_compare(unsigned char *h1, unsigned char *h2) -{ - int i; - for (i=0; i<64; i++) { - if (h1[i] != h2[i]) - return 1; - } - - return 0; -} - -/* - * Format a sha512 hash (64 bits long) as an hexadecimal string (128 char) - * A pointer to this char is statically allocated, so multiple calls to this - * function will overwrite the converted value. - */ -char * -sha512_format(unsigned char *hash) -{ - int i; - static char fmt[128] = ""; - - for (i=0; i<64; i++) { - snprintf(fmt+i, 2, "%02x\n", hash[i]); - } - - return fmt; -} -/* * Returns the UNIX timestamp for the given file, or -1 in case stat(2) * is in error. */