Added functions to generate MD5-Sums based on openssl with tests.

This commit is contained in:
Klaas Freitag 2012-07-18 17:00:46 +02:00
parent 5f93c490a5
commit 2b4261f15a
5 changed files with 154 additions and 1 deletions

View file

@ -4,6 +4,7 @@ add_subdirectory(std)
find_package(SQLite3 REQUIRED)
find_package(Iniparser REQUIRED)
find_package(OpenSSL REQUIRED)
set(CSYNC_PUBLIC_INCLUDE_DIRS
${CMAKE_CURRENT_SOURCE_DIR}
@ -12,6 +13,7 @@ set(CSYNC_PUBLIC_INCLUDE_DIRS
)
set(CSYNC_PRIVATE_INCLUDE_DIRS
${OPENSSL_INCLUDE_DIRS}
${INIPARSER_INCLUDE_DIRS}
${SQLITE3_INCLUDE_DIRS}
${CSTDLIB_PUBLIC_INCLUDE_DIRS}
@ -28,7 +30,7 @@ set(CSYNC_LINK_LIBRARIES
${CSTDLIB_LIBRARY}
${CSYNC_REQUIRED_LIBRARIES}
${INIPARSER_LIBRARIES}
${OPENSSL_LIBRARIES}
${SQLITE3_LIBRARIES}
)

View file

@ -32,6 +32,14 @@
#include "csync_util.h"
#include "vio/csync_vio.h"
#if defined(__APPLE__)
# define COMMON_DIGEST_FOR_OPENSSL
# include <CommonCrypto/CommonDigest.h>
# define SHA1 CC_SHA1
#else
# include <openssl/md5.h>
#endif
#define CSYNC_LOG_CATEGORY_NAME "csync.util"
#include "csync_log.h"
@ -326,4 +334,68 @@ uint64_t csync_create_statedb_hash(CSYNC *ctx) {
return hash;
}
static char* digest_to_out(unsigned char* digest)
{
char *out = c_malloc(33);
int n;
if( !digest ) return NULL;
for (n = 0; n < 16; ++n) {
snprintf(&(out[n*2]), 16*2, "%02x", (unsigned int) *(digest+n));
}
return out;
}
#define BUF_SIZE 512
char* csync_file_md5(const char *filename)
{
const char *tmpFileName;
int fd;
MD5_CTX c;
char buf[ BUF_SIZE+1 ];
unsigned char digest[16];
size_t size;
tmpFileName = c_multibyte( filename );
if ( (fd = _topen( tmpFileName, O_RDONLY )) < 0) {
return NULL;
} else {
MD5_Init(&c);
while( (size=read(fd, buf, BUF_SIZE )) > 0) {
buf[size]='\0';
MD5_Update(&c, buf, size);
}
close(fd);
MD5_Final(digest, &c);
}
c_free_multibyte(tmpFileName);
return digest_to_out(digest);
}
char* csync_buffer_md5(const char *str, int length)
{
MD5_CTX c;
unsigned char digest[16];
MD5_Init(&c);
while (length > 0) {
if (length > 512) {
MD5_Update(&c, str, 512);
} else {
MD5_Update(&c, str, length);
}
length -= 512;
str += 512;
}
MD5_Final(digest, &c);
return digest_to_out(digest);
}
/* vim: set ts=8 sw=2 et cindent: */

View file

@ -36,5 +36,13 @@ int csync_unix_extensions(CSYNC *ctx);
/* Normalize the uri to <host>/<path> */
uint64_t csync_create_statedb_hash(CSYNC *ctx);
/* Calculate the md5 sum for a file given by filename.
* Caller has to free the memory. */
char* csync_file_md5(const char *filename);
/* Create an md5 sum from a data pointer with a given length.
* Caller has to free the memory */
char* csync_buffer_md5(const char *str, int length);
#endif /* _CSYNC_UTIL_H */
/* vim: set ft=c.doxygen ts=8 sw=2 et cindent: */

View file

@ -13,5 +13,8 @@ include_directories(
add_executable(ocmod_test ocmod_test.c)
add_executable(md5_test md5_test.c)
target_link_libraries(ocmod_test ${CMOCKA_LIBRARIES} ${NEON_LIBRARIES} ${CSYNC_LIBRARY} )
target_link_libraries(md5_test ${CMOCKA_LIBRARIES} ${CSYNC_LIBRARY} )

View file

@ -0,0 +1,68 @@
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <iniparser.h>
#include <std/c_path.h>
#include <csync_util.h>
#include <cmocka.h>
#include "config_test.h"
static void test_md5_buffer(void **state) {
const char *t1 = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
const char *t2 = "This is a nice md5 test";
const char *t3 = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
(void) state;
assert_string_equal( csync_buffer_md5(t1, strlen(t1)), "dbcb1ed05ecf975f532604f2d2000246");
assert_string_equal( csync_buffer_md5(t2, strlen(t2)), "65236ae09754bca371fb869384040141");
assert_string_equal( csync_buffer_md5(t3, strlen(t3)), "651f37892a9df60ed087bc7a1c660fec");
}
static void test_md5_files(void **state) {
char path[255];
(void) state;
strcpy(path, TESTFILES_DIR);
strcat(path, "test.txt");
assert_string_equal( csync_file_md5(path), "f3971ce599093756e6018513d0835134");
strcpy(path, TESTFILES_DIR);
strcat(path, "red_is_the_rose.jpg");
assert_string_equal( csync_file_md5(path), "baf8eeb2a36af94f033fa0094c50c2d5");
}
int main(void) {
const UnitTest tests[] = {
unit_test(test_md5_buffer),
unit_test(test_md5_files)
};
return run_tests(tests);
}