2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
6 | Copyright (c) 1998-2010 Zend Technologies Ltd. (http://www.zend.com) |
7 +----------------------------------------------------------------------+
8 | This source file is subject to version 2.00 of the Zend license, |
9 | that is bundled with this package in the file LICENSE, and is |
10 | available through the world-wide-web at the following url: |
11 | http://www.zend.com/license/2_00.txt. |
12 | If you did not receive a copy of the Zend license and are unable to |
13 | obtain it through the world-wide-web, please send a note to |
14 | license@zend.com so we can mail you a copy immediately. |
15 +----------------------------------------------------------------------+
18 #ifndef incl_HPHP_BASE_MD5_H_
19 #define incl_HPHP_BASE_MD5_H_
21 #include "hphp/runtime/base/zend-string.h"
22 #include "hphp/util/util.h"
26 * Most of php's infrastructure for md5sums treats them as strings. They
27 * are 128-bits, though, and fit economically in a fixed-size struct.
36 explicit MD5(const char* str
) {
37 // We expect our input to be null-terminated output from PHP::md5().
38 assert(strlen(str
) == 32);
39 const int kQWordAsciiLen
= 16;
40 char buf
[kQWordAsciiLen
+ 1];
41 buf
[kQWordAsciiLen
] = 0;
42 memcpy(buf
, str
, kQWordAsciiLen
);
43 assert(strlen(buf
) == 16);
44 q
[0] = strtoull(buf
, nullptr, 16);
46 memcpy(buf
, str
+ kQWordAsciiLen
, 16);
47 assert(strlen(buf
) == 16);
48 q
[1] = strtoull(buf
, nullptr, 16);
51 std::string
toString() const {
55 return std::string(string_bin2hex(md5nbo
, len
));
58 // blob is assumed to be in network byte order.
59 explicit MD5(const void* blob
) {
60 q
[0] = ntohq(((const uint64_t*)blob
)[0]);
61 q
[1] = ntohq(((const uint64_t*)blob
)[1]);
64 // Copy out in network byte order.
65 void nbo(void* blob
) const {
66 ((uint64_t*)blob
)[0] = htonq(q
[0]);
67 ((uint64_t*)blob
)[1] = htonq(q
[1]);
70 bool isValid() const {
72 * We arbitrarily choose kDefaultMD5 to be an "impossible" md5 value.
73 * If someone manages to construct a PHP compilation unit that
74 * collides, well, we owe them a pizza dinner or something.
79 bool operator==(const MD5
& r
) const {
80 return q
[0] == r
.q
[0] && q
[1] == r
.q
[1];
82 bool operator<(const MD5
& r
) const {
83 return q
[0] < r
.q
[0] || (q
[0] == r
.q
[0] && q
[1] < r
.q
[1]);
85 bool operator>(const MD5
& r
) const {
86 return q
[0] > r
.q
[0] || (q
[0] == r
.q
[0] && q
[1] > r
.q
[1]);
88 bool operator!=(const MD5
& r
) const {
89 return !operator==(r
);
92 uint64_t hash() const {
93 // hash_int64_pair does way more work than necessary; all the bits here
94 // are fantastically good.