2016-05-12 23:43:17 +03:00
|
|
|
/*
|
2017-01-21 09:02:33 +03:00
|
|
|
* Copyright (c) 2016-present, Facebook, Inc.
|
2016-05-12 23:43:17 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2016-06-16 00:23:25 +03:00
|
|
|
#include <boost/operators.hpp>
|
2016-05-12 23:43:17 +03:00
|
|
|
#include <folly/Range.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <array>
|
2016-06-16 00:23:25 +03:00
|
|
|
#include <iosfwd>
|
2016-05-12 23:43:17 +03:00
|
|
|
|
2016-05-28 02:35:41 +03:00
|
|
|
namespace folly {
|
|
|
|
class IOBuf;
|
|
|
|
}
|
|
|
|
|
2016-05-12 23:43:17 +03:00
|
|
|
namespace facebook {
|
|
|
|
namespace eden {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Immutable 160-bit hash.
|
|
|
|
*/
|
|
|
|
class Hash : boost::totally_ordered<Hash> {
|
|
|
|
public:
|
|
|
|
enum { RAW_SIZE = 20 };
|
2016-05-28 02:35:41 +03:00
|
|
|
using Storage = std::array<uint8_t, RAW_SIZE>;
|
2016-05-12 23:43:17 +03:00
|
|
|
|
2016-06-09 02:16:19 +03:00
|
|
|
/**
|
|
|
|
* Create a 0-initialized hash
|
|
|
|
*/
|
|
|
|
Hash();
|
|
|
|
|
2016-05-28 02:35:41 +03:00
|
|
|
explicit Hash(Storage bytes);
|
2016-05-12 23:43:17 +03:00
|
|
|
|
|
|
|
explicit Hash(folly::ByteRange bytes);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param hex is a string of 40 hexadecimal characters.
|
|
|
|
*/
|
|
|
|
explicit Hash(folly::StringPiece hex);
|
|
|
|
|
2016-05-28 02:35:41 +03:00
|
|
|
/**
|
|
|
|
* Compute the SHA1 hash of an IOBuf chain.
|
|
|
|
*/
|
|
|
|
static Hash sha1(const folly::IOBuf* buf);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the SHA1 hash of a ByteRange
|
|
|
|
*/
|
|
|
|
static Hash sha1(folly::ByteRange data);
|
|
|
|
|
2016-05-28 02:35:36 +03:00
|
|
|
folly::ByteRange getBytes() const;
|
2017-04-27 21:43:34 +03:00
|
|
|
folly::MutableByteRange mutableBytes();
|
2016-05-12 23:43:17 +03:00
|
|
|
|
|
|
|
/** @return 40-character [lowercase] hex representation of this hash. */
|
|
|
|
std::string toString() const;
|
|
|
|
|
2018-02-21 02:13:21 +03:00
|
|
|
size_t getHashCode() const;
|
2016-10-13 01:51:54 +03:00
|
|
|
|
2016-05-12 23:43:17 +03:00
|
|
|
bool operator==(const Hash&) const;
|
|
|
|
bool operator<(const Hash&) const;
|
|
|
|
|
|
|
|
private:
|
2016-06-09 02:16:19 +03:00
|
|
|
Storage bytes_;
|
2016-05-12 23:43:17 +03:00
|
|
|
};
|
2016-06-16 00:23:25 +03:00
|
|
|
|
2016-09-19 22:48:13 +03:00
|
|
|
/** A hash object initialized to all zeroes */
|
|
|
|
extern const Hash kZeroHash;
|
|
|
|
|
2016-06-16 00:23:25 +03:00
|
|
|
/**
|
|
|
|
* Output stream operator for Hash.
|
|
|
|
*
|
|
|
|
* This makes it possible to easily use Hash in glog statements.
|
|
|
|
*/
|
|
|
|
std::ostream& operator<<(std::ostream& os, const Hash& hash);
|
2016-06-20 21:43:45 +03:00
|
|
|
|
|
|
|
/* Define toAppend() so folly::to<string>(Hash) will work */
|
|
|
|
void toAppend(const Hash& hash, std::string* result);
|
2017-11-04 01:58:04 +03:00
|
|
|
} // namespace eden
|
|
|
|
} // namespace facebook
|
2016-10-13 01:51:54 +03:00
|
|
|
|
|
|
|
namespace std {
|
|
|
|
template <>
|
|
|
|
struct hash<facebook::eden::Hash> {
|
2018-02-21 02:13:21 +03:00
|
|
|
size_t operator()(const facebook::eden::Hash& hash) const {
|
2016-10-13 01:51:54 +03:00
|
|
|
return hash.getHashCode();
|
|
|
|
}
|
|
|
|
};
|
2017-11-04 01:58:04 +03:00
|
|
|
} // namespace std
|