Skip to content

Commit

Permalink
Make the MD5 library C compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
FooBarWidget committed May 7, 2013
1 parent ff3ec91 commit 4934f3c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
35 changes: 19 additions & 16 deletions ext/common/Utils/MD5.cpp
Expand Up @@ -52,11 +52,12 @@
*/

#include "MD5.h"
#include <string.h>
#include <boost/detail/endian.hpp>
#include "StrIntUtils.h"

namespace Passenger {
#include "../../boost/detail/endian.hpp" /* File is C compatible. */
#ifdef __cplusplus
#include <string.h>
#include "StrIntUtils.h"
namespace Passenger {
#endif

#if defined(BOOST_BIG_ENDIAN)
# define ARCH_IS_BIG_ENDIAN 1
Expand Down Expand Up @@ -390,17 +391,19 @@ md5_finish(md5_state_t *pms, md5_byte_t digest[16])
digest[i] = (md5_byte_t)(pms->abcd[i >> 2] >> ((i & 3) << 3));
}

std::string
md5_hex(const StaticString &input)
{
md5_state_t pms;
md5_byte_t digest[16];
#ifdef __cplusplus
std::string
md5_hex(const StaticString &input)
{
md5_state_t pms;
md5_byte_t digest[16];

md5_init(&pms);
md5_append(&pms, (const md5_byte_t *) input.data(), input.size());
md5_finish(&pms, digest);
md5_init(&pms);
md5_append(&pms, (const md5_byte_t *) input.data(), input.size());
md5_finish(&pms, digest);

return toHex(StaticString((const char *) digest, 16));
}
return toHex(StaticString((const char *) digest, 16));
}

} // namespace Passenger
} // namespace Passenger
#endif
21 changes: 13 additions & 8 deletions ext/common/Utils/MD5.h
Expand Up @@ -50,9 +50,14 @@
#ifndef _PASSENGER_MD5_H_
#define _PASSENGER_MD5_H_

#include <string>
#include <boost/cstdint.hpp>
#include "../StaticString.h"
#ifdef __cplusplus
#include "../../boost/cstdint.hpp"
#include <string>
#include <StaticString.h>
namespace Passenger {
#else
#include <stdint.h>
#endif

/*
* This package supports both compile-time and run-time determination of CPU
Expand All @@ -64,8 +69,6 @@
* efficiently on either one than if ARCH_IS_BIG_ENDIAN is defined.
*/

namespace Passenger {

typedef uint8_t md5_byte_t; /* 8-bit byte */
typedef uint32_t md5_word_t; /* 32-bit word */

Expand All @@ -90,9 +93,11 @@ void md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes);
/* Finish the message and return the digest. */
void md5_finish(md5_state_t *pms, md5_byte_t digest[MD5_SIZE]);

/* Convenience method for directly converting data into a hexadecimal MD5 string. */
std::string md5_hex(const StaticString &input);
#ifdef __cplusplus
/* Convenience method for directly converting data into a hexadecimal MD5 string. */
std::string md5_hex(const StaticString &input);

} // namespace Passenger
} // namespace Passenger
#endif

#endif /* _PASSENGER_MD5_H_ */

0 comments on commit 4934f3c

Please sign in to comment.