aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/cpp/util/md5.h
blob: fbab1881c65024a7c6b01987d2f18c16b5937a91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2014 Google Inc. All rights reserved.
//
// 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.
// Provides a fast MD5 implementation.
//
// This implementation saves us from linking huge OpenSSL library.

#ifndef DEVTOOLS_BLAZE_MAIN_UTIL_MD5_H_
#define DEVTOOLS_BLAZE_MAIN_UTIL_MD5_H_

#include <string>

#include "util/port.h"

namespace blaze_util {

using std::string;

// The <code>Context</code> class performs the actual MD5
// computation. It works incrementally and can be fed a single byte at
// a time if desired.
class Md5Digest {
 public:
  Md5Digest();

  Md5Digest(const Md5Digest& original);

  // the MD5 digest is always 128 bits = 16 bytes
  static const int kDigestLength = 16;

  // Resets the context so that it can be used to calculate another
  // MD5 digest. The context is in the same state as if it had just
  // been constructed. It is unnecessary to call <code>Reset</code> on
  // a newly created context.
  void Reset();

  // Add <code>length</code> bytes of <code>buf</code> to the MD5
  // digest.
  void Update(const void *buf, unsigned int length);

  // Retrieve the computed MD5 digest as a 16 byte array.
  void Finish(unsigned char* digest);

  // Produces a hexadecimal string representation of this digest in the form:
  // [0-9a-f]{32}
  string String() const;

 private:
  void Transform(const unsigned char* buffer, unsigned int len);

 private:
  unsigned int state[4];          // state (ABCD)
  unsigned int count[2];          // number of bits, modulo 2^64 (lsb first)
  unsigned char ctx_buffer[128];  // input buffer
  unsigned int ctx_buffer_len;
};

}  // namespace blaze_util


#endif  // DEVTOOLS_BLAZE_MAIN_UTIL_MD5_H_