~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to util/md5.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "md5.h"
 
21
 
 
22
namespace mongo {
 
23
 
 
24
    typedef unsigned char md5digest[16];
 
25
 
 
26
    inline void md5(const void *buf, int nbytes, md5digest digest) {
 
27
        md5_state_t st;
 
28
        md5_init(&st);
 
29
        md5_append(&st, (const md5_byte_t *) buf, nbytes);
 
30
        md5_finish(&st, digest);
 
31
    }
 
32
 
 
33
    inline void md5(const char *str, md5digest digest) {
 
34
        md5(str, strlen(str), digest);
 
35
    }
 
36
    
 
37
    inline std::string digestToString( md5digest digest ){
 
38
        static const char * letters = "0123456789abcdef";
 
39
        stringstream ss;
 
40
        for ( int i=0; i<16; i++){
 
41
            unsigned char c = digest[i];
 
42
            ss << letters[ ( c >> 4 ) & 0xf ] << letters[ c & 0xf ];
 
43
        }
 
44
        return ss.str();
 
45
    }
 
46
 
 
47
    inline std::string md5simpledigest( string s ){
 
48
        md5digest d;
 
49
        md5( s.c_str() , d );
 
50
        return digestToString( d );
 
51
    }
 
52
 
 
53
} // namespace mongo