synk

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

commit e107f99cfc32c18f5c8902f461b70336973de2d5
parent fcee4829b1e08b2e87a49f3c64a15108eceeba3a
Author: Willy <willyatmailoodotorg>
Date:   Mon Aug 22 08:24:00 +0200

New function to generate a stream's hash

Diffstat:
synk.c | 27+++++++++++++++++++++++++++
1 file changed, 27 insertions(+), 0 deletions(-)
diff --git a/synk.c b/synk.c @@ -10,6 +10,7 @@ #include <sys/types.h> #include "arg.h" +#include "sha512.h" #define SERVER_HOST "127.0.0.1" #define SERVER_PORT 9723 @@ -34,6 +35,7 @@ long gettimestamp(const char *path); int handleclient(int cfd, struct in_addr inet); int server(in_addr_t host, in_port_t port); int client(in_addr_t host, in_port_t port, const char *path); +int sha512(FILE *stream, unsigned char *hash); void usage(char *name) @@ -43,6 +45,31 @@ 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); +} + +/* * Returns the UNIX timestamp for the given file, or -1 in case stat(2) * is in error. */