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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "Hash.h"
|
|
|
|
|
2016-06-20 21:43:45 +03:00
|
|
|
#include <folly/Conv.h>
|
2016-05-12 23:43:17 +03:00
|
|
|
#include <folly/Format.h>
|
|
|
|
#include <folly/String.h>
|
2016-05-28 02:35:41 +03:00
|
|
|
#include <folly/io/Cursor.h>
|
|
|
|
#include <folly/io/IOBuf.h>
|
2016-06-17 04:30:25 +03:00
|
|
|
#include <folly/ssl/OpenSSLHash.h>
|
2016-05-12 23:43:17 +03:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using folly::ByteRange;
|
2016-06-17 04:30:25 +03:00
|
|
|
using folly::range;
|
2018-01-26 22:17:36 +03:00
|
|
|
using folly::StringPiece;
|
2016-06-17 04:30:25 +03:00
|
|
|
using folly::ssl::OpenSSLHash;
|
2017-11-04 01:58:04 +03:00
|
|
|
using std::string;
|
2016-05-12 23:43:17 +03:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace eden {
|
|
|
|
|
2016-09-19 22:48:13 +03:00
|
|
|
const Hash kZeroHash;
|
|
|
|
|
2018-11-29 03:07:04 +03:00
|
|
|
const Hash kEmptySha1{Hash::Storage{0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b,
|
|
|
|
0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60,
|
|
|
|
0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09}};
|
|
|
|
|
2017-04-27 21:43:34 +03:00
|
|
|
folly::MutableByteRange Hash::mutableBytes() {
|
|
|
|
return folly::MutableByteRange{bytes_.data(), bytes_.size()};
|
|
|
|
}
|
|
|
|
|
2016-05-12 23:43:17 +03:00
|
|
|
std::string Hash::toString() const {
|
|
|
|
std::string result;
|
|
|
|
folly::hexlify(bytes_, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-10-15 23:32:14 +03:00
|
|
|
size_t Hash::getHashCode() const noexcept {
|
2016-10-13 01:51:54 +03:00
|
|
|
static_assert(sizeof(size_t) <= RAW_SIZE, "crazy size_t type");
|
|
|
|
size_t result;
|
|
|
|
memcpy(&result, bytes_.data(), sizeof(size_t));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-12 23:43:17 +03:00
|
|
|
bool Hash::operator==(const Hash& otherHash) const {
|
|
|
|
return bytes_ == otherHash.bytes_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Hash::operator<(const Hash& otherHash) const {
|
|
|
|
return bytes_ < otherHash.bytes_;
|
|
|
|
}
|
|
|
|
|
2018-11-29 03:07:04 +03:00
|
|
|
Hash Hash::sha1(const folly::IOBuf& buf) {
|
2016-05-28 02:35:41 +03:00
|
|
|
Storage hashBytes;
|
2018-11-29 03:07:04 +03:00
|
|
|
OpenSSLHash::sha1(range(hashBytes), buf);
|
|
|
|
return Hash{hashBytes};
|
2016-05-28 02:35:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Hash Hash::sha1(ByteRange data) {
|
|
|
|
Storage hashBytes;
|
2016-06-17 04:30:25 +03:00
|
|
|
OpenSSLHash::sha1(range(hashBytes), data);
|
2018-11-29 03:07:04 +03:00
|
|
|
return Hash{hashBytes};
|
2016-05-28 02:35:41 +03:00
|
|
|
}
|
2016-06-16 00:23:25 +03:00
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const Hash& hash) {
|
|
|
|
os << hash.toString();
|
|
|
|
return os;
|
|
|
|
}
|
2016-06-20 21:43:45 +03:00
|
|
|
|
|
|
|
void toAppend(const Hash& hash, std::string* result) {
|
|
|
|
folly::toAppend(hash.toString(), result);
|
|
|
|
}
|
2017-11-04 01:58:04 +03:00
|
|
|
} // namespace eden
|
|
|
|
} // namespace facebook
|