mirror of
https://github.com/facebook/sapling.git
synced 2024-10-10 08:47:12 +03:00
c769088f16
Summary: Add some static helper functions to create Hash objects by running a SHA1 hash on input data. Reviewed By: wez, bolinfest Differential Revision: D3354594 fbshipit-source-id: 6d6bfb835175e7a25c1e6e2539438bee5887a863
64 lines
1.3 KiB
C++
64 lines
1.3 KiB
C++
/*
|
|
* Copyright (c) 2016, Facebook, Inc.
|
|
* 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
|
|
|
|
#include <folly/Range.h>
|
|
#include <stdint.h>
|
|
#include <array>
|
|
#include <boost/operators.hpp>
|
|
|
|
namespace folly {
|
|
class IOBuf;
|
|
}
|
|
|
|
namespace facebook {
|
|
namespace eden {
|
|
|
|
/**
|
|
* Immutable 160-bit hash.
|
|
*/
|
|
class Hash : boost::totally_ordered<Hash> {
|
|
public:
|
|
enum { RAW_SIZE = 20 };
|
|
using Storage = std::array<uint8_t, RAW_SIZE>;
|
|
|
|
explicit Hash(Storage bytes);
|
|
|
|
explicit Hash(folly::ByteRange bytes);
|
|
|
|
/**
|
|
* @param hex is a string of 40 hexadecimal characters.
|
|
*/
|
|
explicit Hash(folly::StringPiece hex);
|
|
|
|
/**
|
|
* 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);
|
|
|
|
folly::ByteRange getBytes() const;
|
|
|
|
/** @return 40-character [lowercase] hex representation of this hash. */
|
|
std::string toString() const;
|
|
|
|
bool operator==(const Hash&) const;
|
|
bool operator<(const Hash&) const;
|
|
|
|
private:
|
|
const Storage bytes_;
|
|
};
|
|
}
|
|
}
|