diff options
author | Antonin Kral <a.kral@bobek.cz> | 2010-01-31 08:32:52 +0100 |
---|---|---|
committer | Antonin Kral <a.kral@bobek.cz> | 2010-01-31 08:32:52 +0100 |
commit | 4eefaf421bfeddf040d96a3dafb12e09673423d7 (patch) | |
tree | cb2e5ccc7f98158894f977ff131949da36673591 /util/md5.hpp | |
download | mongodb-4eefaf421bfeddf040d96a3dafb12e09673423d7.tar.gz |
Imported Upstream version 1.3.1
Diffstat (limited to 'util/md5.hpp')
-rw-r--r-- | util/md5.hpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/util/md5.hpp b/util/md5.hpp new file mode 100644 index 0000000..d955910 --- /dev/null +++ b/util/md5.hpp @@ -0,0 +1,53 @@ +// md5.hpp + +/* Copyright 2009 10gen 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. + */ + +#pragma once + +#include "md5.h" + +namespace mongo { + + typedef unsigned char md5digest[16]; + + inline void md5(const void *buf, int nbytes, md5digest digest) { + md5_state_t st; + md5_init(&st); + md5_append(&st, (const md5_byte_t *) buf, nbytes); + md5_finish(&st, digest); + } + + inline void md5(const char *str, md5digest digest) { + md5(str, strlen(str), digest); + } + + inline std::string digestToString( md5digest digest ){ + static const char * letters = "0123456789abcdef"; + stringstream ss; + for ( int i=0; i<16; i++){ + unsigned char c = digest[i]; + ss << letters[ ( c >> 4 ) & 0xf ] << letters[ c & 0xf ]; + } + return ss.str(); + } + + inline std::string md5simpledigest( string s ){ + md5digest d; + md5( s.c_str() , d ); + return digestToString( d ); + } + +} // namespace mongo |