rearrange md5 code for sharing. update shared smallut
This commit is contained in:
parent
2fb61f2583
commit
1d62b50c7e
@ -17,9 +17,6 @@
|
|||||||
* will fill a supplied 16-byte array with the digest.
|
* will fill a supplied 16-byte array with the digest.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "autoconfig.h"
|
|
||||||
//#include <compat.h>
|
|
||||||
|
|
||||||
#include "md5.h"
|
#include "md5.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -47,6 +44,9 @@ static uint8_t PADDING[MD5_BLOCK_LENGTH] = {
|
|||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void MD5Pad(MD5_CTX *);
|
||||||
|
static void MD5Transform(uint32_t [4], const uint8_t [MD5_BLOCK_LENGTH]);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
|
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
|
||||||
* initialization constants.
|
* initialization constants.
|
||||||
@ -103,7 +103,7 @@ MD5Update(MD5_CTX *ctx, const unsigned char *input, size_t len)
|
|||||||
* Pad pad to 64-byte boundary with the bit pattern
|
* Pad pad to 64-byte boundary with the bit pattern
|
||||||
* 1 0* (64-bit count of bits processed, MSB-first)
|
* 1 0* (64-bit count of bits processed, MSB-first)
|
||||||
*/
|
*/
|
||||||
void
|
static void
|
||||||
MD5Pad(MD5_CTX *ctx)
|
MD5Pad(MD5_CTX *ctx)
|
||||||
{
|
{
|
||||||
uint8_t count[8];
|
uint8_t count[8];
|
||||||
@ -155,7 +155,7 @@ MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx)
|
|||||||
* reflect the addition of 16 longwords of new data. MD5Update blocks
|
* reflect the addition of 16 longwords of new data. MD5Update blocks
|
||||||
* the data and converts bytes into longwords for this routine.
|
* the data and converts bytes into longwords for this routine.
|
||||||
*/
|
*/
|
||||||
void
|
static void
|
||||||
MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
|
MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
|
||||||
{
|
{
|
||||||
uint32_t a, b, c, d, in[MD5_BLOCK_LENGTH / 4];
|
uint32_t a, b, c, d, in[MD5_BLOCK_LENGTH / 4];
|
||||||
@ -250,3 +250,52 @@ MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
|
|||||||
state[2] += c;
|
state[2] += c;
|
||||||
state[3] += d;
|
state[3] += d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// C++ utilities
|
||||||
|
using std::string;
|
||||||
|
|
||||||
|
void MD5Final(string &digest, MD5_CTX *context)
|
||||||
|
{
|
||||||
|
unsigned char d[16];
|
||||||
|
MD5Final (d, context);
|
||||||
|
digest.assign((const char *)d, 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
string& MD5String(const string& data, string& digest)
|
||||||
|
{
|
||||||
|
MD5_CTX ctx;
|
||||||
|
MD5Init(&ctx);
|
||||||
|
MD5Update(&ctx, (const unsigned char*)data.c_str(), data.length());
|
||||||
|
MD5Final(digest, &ctx);
|
||||||
|
return digest;
|
||||||
|
}
|
||||||
|
|
||||||
|
string& MD5HexPrint(const string& digest, string &out)
|
||||||
|
{
|
||||||
|
out.erase();
|
||||||
|
out.reserve(33);
|
||||||
|
static const char hex[]="0123456789abcdef";
|
||||||
|
const unsigned char *hash = (const unsigned char *)digest.c_str();
|
||||||
|
for (int i = 0; i < 16; i++) {
|
||||||
|
out.append(1, hex[hash[i] >> 4]);
|
||||||
|
out.append(1, hex[hash[i] & 0x0f]);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
string& MD5HexScan(const string& xdigest, string& digest)
|
||||||
|
{
|
||||||
|
digest.erase();
|
||||||
|
if (xdigest.length() != 32) {
|
||||||
|
return digest;
|
||||||
|
}
|
||||||
|
for (unsigned int i = 0; i < 16; i++) {
|
||||||
|
unsigned int val;
|
||||||
|
if (sscanf(xdigest.c_str() + 2*i, "%2x", &val) != 1) {
|
||||||
|
digest.erase();
|
||||||
|
return digest;
|
||||||
|
}
|
||||||
|
digest.append(1, (unsigned char)val);
|
||||||
|
}
|
||||||
|
return digest;
|
||||||
|
}
|
||||||
|
|||||||
@ -30,8 +30,13 @@ typedef struct MD5Context {
|
|||||||
|
|
||||||
void MD5Init(MD5_CTX *);
|
void MD5Init(MD5_CTX *);
|
||||||
void MD5Update(MD5_CTX *, const uint8_t *, size_t);
|
void MD5Update(MD5_CTX *, const uint8_t *, size_t);
|
||||||
void MD5Pad(MD5_CTX *);
|
|
||||||
void MD5Final(uint8_t [MD5_DIGEST_LENGTH], MD5_CTX *);
|
void MD5Final(uint8_t [MD5_DIGEST_LENGTH], MD5_CTX *);
|
||||||
void MD5Transform(uint32_t [4], const uint8_t [MD5_BLOCK_LENGTH]);
|
|
||||||
|
/** md5 c++ utility wrappers */
|
||||||
|
#include <string>
|
||||||
|
extern void MD5Final(std::string& digest, MD5_CTX *);
|
||||||
|
extern std::string& MD5String(const std::string& data, std::string& digest);
|
||||||
|
extern std::string& MD5HexPrint(const std::string& digest, std::string& xdigest);
|
||||||
|
extern std::string& MD5HexScan(const std::string& xdigest, std::string& digest);
|
||||||
|
|
||||||
#endif /* _MD5_H_ */
|
#endif /* _MD5_H_ */
|
||||||
|
|||||||
@ -14,8 +14,6 @@
|
|||||||
* Free Software Foundation, Inc.,
|
* Free Software Foundation, Inc.,
|
||||||
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef TEST_MD5
|
|
||||||
#include "autoconfig.h"
|
#include "autoconfig.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -23,54 +21,10 @@
|
|||||||
|
|
||||||
#include "md5ut.h"
|
#include "md5ut.h"
|
||||||
|
|
||||||
/* Convenience utilities */
|
|
||||||
|
|
||||||
void MD5Final(string &digest, MD5_CTX *context)
|
|
||||||
{
|
|
||||||
unsigned char d[16];
|
|
||||||
MD5Final (d, context);
|
|
||||||
digest.assign((const char *)d, 16);
|
|
||||||
}
|
|
||||||
|
|
||||||
string& MD5String(const string& data, string& digest)
|
|
||||||
{
|
|
||||||
MD5_CTX ctx;
|
|
||||||
MD5Init(&ctx);
|
|
||||||
MD5Update(&ctx, (const unsigned char*)data.c_str(), data.length());
|
|
||||||
MD5Final(digest, &ctx);
|
|
||||||
return digest;
|
|
||||||
}
|
|
||||||
|
|
||||||
string& MD5HexPrint(const string& digest, string &out)
|
|
||||||
{
|
|
||||||
out.erase();
|
|
||||||
out.reserve(33);
|
|
||||||
static const char hex[]="0123456789abcdef";
|
|
||||||
const unsigned char *hash = (const unsigned char *)digest.c_str();
|
|
||||||
for (int i = 0; i < 16; i++) {
|
|
||||||
out.append(1, hex[hash[i] >> 4]);
|
|
||||||
out.append(1, hex[hash[i] & 0x0f]);
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
string& MD5HexScan(const string& xdigest, string& digest)
|
|
||||||
{
|
|
||||||
digest.erase();
|
|
||||||
if (xdigest.length() != 32) {
|
|
||||||
return digest;
|
|
||||||
}
|
|
||||||
for (unsigned int i = 0; i < 16; i++) {
|
|
||||||
unsigned int val;
|
|
||||||
if (sscanf(xdigest.c_str() + 2*i, "%2x", &val) != 1) {
|
|
||||||
digest.erase();
|
|
||||||
return digest;
|
|
||||||
}
|
|
||||||
digest.append(1, (unsigned char)val);
|
|
||||||
}
|
|
||||||
return digest;
|
|
||||||
}
|
|
||||||
|
|
||||||
#include "readfile.h"
|
#include "readfile.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
class FileScanMd5 : public FileScanDo {
|
class FileScanMd5 : public FileScanDo {
|
||||||
public:
|
public:
|
||||||
FileScanMd5(string& d) : digest(d) {}
|
FileScanMd5(string& d) : digest(d) {}
|
||||||
@ -87,6 +41,7 @@ public:
|
|||||||
string &digest;
|
string &digest;
|
||||||
MD5_CTX ctx;
|
MD5_CTX ctx;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool MD5File(const string& filename, string &digest, string *reason)
|
bool MD5File(const string& filename, string &digest, string *reason)
|
||||||
{
|
{
|
||||||
FileScanMd5 md5er(digest);
|
FileScanMd5 md5er(digest);
|
||||||
@ -96,58 +51,3 @@ bool MD5File(const string& filename, string &digest, string *reason)
|
|||||||
MD5Final(md5er.digest, &md5er.ctx);
|
MD5Final(md5er.digest, &md5er.ctx);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else // TEST_MD5
|
|
||||||
|
|
||||||
// Test driver
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <iostream>
|
|
||||||
#include "md5ut.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
static const char *thisprog;
|
|
||||||
static char usage [] =
|
|
||||||
"trmd5 filename\n\n"
|
|
||||||
;
|
|
||||||
static void
|
|
||||||
Usage(void)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, const char **argv)
|
|
||||||
{
|
|
||||||
thisprog = argv[0];
|
|
||||||
argc--; argv++;
|
|
||||||
|
|
||||||
if (argc != 1)
|
|
||||||
Usage();
|
|
||||||
string filename = *argv++;argc--;
|
|
||||||
|
|
||||||
string reason, digest;
|
|
||||||
if (!MD5File(filename, digest, &reason)) {
|
|
||||||
cerr << reason << endl;
|
|
||||||
exit(1);
|
|
||||||
} else {
|
|
||||||
string hex;
|
|
||||||
cout << "MD5 (" << filename << ") = " << MD5HexPrint(digest, hex) << endl;
|
|
||||||
|
|
||||||
string digest1;
|
|
||||||
MD5HexScan(hex, digest1);
|
|
||||||
if (digest1.compare(digest)) {
|
|
||||||
cout << "MD5HexScan Failure" << endl;
|
|
||||||
cout << MD5HexPrint(digest, hex) << " " << digest.length() << " -> "
|
|
||||||
<< MD5HexPrint(digest1, hex) << " " << digest1.length() << endl;
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|||||||
@ -17,17 +17,11 @@
|
|||||||
#ifndef _MD5UT_H_
|
#ifndef _MD5UT_H_
|
||||||
#define _MD5UT_H_
|
#define _MD5UT_H_
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
#include "md5.h"
|
#include "md5.h"
|
||||||
|
|
||||||
/** md5 utility wrappers */
|
/** md5 utility: compute file md5 */
|
||||||
#include <string>
|
|
||||||
using std::string;
|
extern bool MD5File(const std::string& filename, std::string& digest,
|
||||||
extern void MD5Final(string& digest, MD5_CTX *);
|
std::string *reason);
|
||||||
extern bool MD5File(const string& filename, string& digest, string *reason);
|
|
||||||
extern string& MD5String(const string& data, string& digest);
|
|
||||||
extern string& MD5HexPrint(const string& digest, string& xdigest);
|
|
||||||
extern string& MD5HexScan(const string& xdigest, string& digest);
|
|
||||||
|
|
||||||
#endif /* _MD5UT_H_ */
|
#endif /* _MD5UT_H_ */
|
||||||
|
|||||||
@ -477,13 +477,13 @@ void ltrimstring(string& s, const char *ws)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove some chars and replace them with spaces
|
// Remove some chars and replace them with spaces
|
||||||
string neutchars(const string& str, const string& chars)
|
string neutchars(const string& str, const string& chars, char rep)
|
||||||
{
|
{
|
||||||
string out;
|
string out;
|
||||||
neutchars(str, out, chars);
|
neutchars(str, out, chars, rep);
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
void neutchars(const string& str, string& out, const string& chars)
|
void neutchars(const string& str, string& out, const string& chars, char rep)
|
||||||
{
|
{
|
||||||
string::size_type startPos, pos;
|
string::size_type startPos, pos;
|
||||||
|
|
||||||
@ -498,7 +498,7 @@ void neutchars(const string& str, string& out, const string& chars)
|
|||||||
if (pos == string::npos) {
|
if (pos == string::npos) {
|
||||||
out += str.substr(startPos);
|
out += str.substr(startPos);
|
||||||
} else {
|
} else {
|
||||||
out += str.substr(startPos, pos - startPos) + " ";
|
out += str.substr(startPos, pos - startPos) + rep;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1237,9 +1237,10 @@ class SimpleRegexp::Internal {
|
|||||||
public:
|
public:
|
||||||
Internal(const string& exp, int flags, int nm)
|
Internal(const string& exp, int flags, int nm)
|
||||||
: expr(exp,
|
: expr(exp,
|
||||||
basic_regex<char>::flag_type(regex_constants::extended |
|
basic_regex<char>::flag_type(
|
||||||
((flags&SRE_ICASE) ? int(regex_constants::icase) : 0) |
|
regex_constants::extended |
|
||||||
((flags&SRE_NOSUB) ? int(regex_constants::nosubs) : 0)
|
((flags&SRE_ICASE) ? int(regex_constants::icase) : 0) |
|
||||||
|
((flags&SRE_NOSUB) ? int(regex_constants::nosubs) : 0)
|
||||||
)), ok(true), nmatch(nm) {
|
)), ok(true), nmatch(nm) {
|
||||||
}
|
}
|
||||||
std::regex expr;
|
std::regex expr;
|
||||||
|
|||||||
@ -152,9 +152,10 @@ extern std::string escapeHtml(const std::string& in);
|
|||||||
extern std::string makeCString(const std::string& in);
|
extern std::string makeCString(const std::string& in);
|
||||||
|
|
||||||
/** Replace some chars with spaces (ie: newline chars). */
|
/** Replace some chars with spaces (ie: newline chars). */
|
||||||
extern std::string neutchars(const std::string& str, const std::string& chars);
|
extern std::string neutchars(const std::string& str, const std::string& chars,
|
||||||
|
char rep = ' ');
|
||||||
extern void neutchars(const std::string& str, std::string& out,
|
extern void neutchars(const std::string& str, std::string& out,
|
||||||
const std::string& chars);
|
const std::string& chars, char rep = ' ');
|
||||||
|
|
||||||
/** Turn string into something that won't be expanded by a shell. In practise
|
/** Turn string into something that won't be expanded by a shell. In practise
|
||||||
* quote with double-quotes and escape $`\ */
|
* quote with double-quotes and escape $`\ */
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user