MongoDB  2.7.0
md5.hpp
1 // md5.hpp
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include "mongo/util/md5.h"
21 
22 #include <sstream>
23 #include <string>
24 #include <string.h>
25 
26 namespace mongo {
27 
28  typedef unsigned char md5digest[16];
29 
30  inline void md5(const void *buf, int nbytes, md5digest digest) {
31  md5_state_t st;
32  md5_init(&st);
33  md5_append(&st, (const md5_byte_t *) buf, nbytes);
34  md5_finish(&st, digest);
35  }
36 
37  inline void md5(const char *str, md5digest digest) {
38  md5(str, strlen(str), digest);
39  }
40 
41  inline std::string digestToString( md5digest digest ){
42  static const char * letters = "0123456789abcdef";
43  std::stringstream ss;
44  for ( int i=0; i<16; i++){
45  unsigned char c = digest[i];
46  ss << letters[ ( c >> 4 ) & 0xf ] << letters[ c & 0xf ];
47  }
48  return ss.str();
49  }
50 
51  inline std::string md5simpledigest( const void* buf, int nbytes){
52  md5digest d;
53  md5( buf, nbytes , d );
54  return digestToString( d );
55  }
56 
57  inline std::string md5simpledigest( const std::string& s ){
58  return md5simpledigest(s.data(), s.size());
59  }
60 
61 
62 } // namespace mongo
Definition: md5.h:70