From 7fa45e04f5aa5d99c557b1ea02b842fdb2c00b09 Mon Sep 17 00:00:00 2001 From: myoungwon oh Date: Wed, 22 Aug 2018 19:19:07 +0900 Subject: [PATCH] common/buffer.cc: add sha1 fingerprint Signed-off-by: Myoungwon Oh --- src/common/buffer.cc | 26 ++++++++++++++++++++++++++ src/include/buffer.h | 3 +++ src/include/types.h | 23 +++++++++++++++++++++++ src/osd/osd_types.cc | 1 + 4 files changed, 53 insertions(+) diff --git a/src/common/buffer.cc b/src/common/buffer.cc index ebd3aae511ee..60359223afb0 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -2464,6 +2464,32 @@ void buffer::list::invalidate_crc() } } +#include "common/ceph_crypto.h" +using ceph::crypto::SHA1; + +boost::optional buffer::list::sha1() +{ + ptr nb; + unsigned pos = 0; + unsigned char fingerprint[CEPH_CRYPTO_SHA1_DIGESTSIZE]; + if (_len == 0) { + return boost::none; + } + nb = buffer::create(_len); + for (std::list::iterator it = _buffers.begin(); + it != _buffers.end(); + ++it) { + nb.copy_in(pos, it->length(), it->c_str(), false); + pos += it->length(); + } + int size = length(); + SHA1 sha1_gen; + sha1_gen.Update((const unsigned char *)nb.c_str(), size); + sha1_gen.Final(fingerprint); + sha1_digest_info_t fp_t(fingerprint); + return fp_t; +} + /** * Binary write all contents to a C++ stream */ diff --git a/src/include/buffer.h b/src/include/buffer.h index 15a062e21be9..9af7e7869a7f 100644 --- a/src/include/buffer.h +++ b/src/include/buffer.h @@ -61,6 +61,7 @@ #endif #include "inline_memory.h" +#include #if __GNUC__ >= 4 #define CEPH_BUFFER_API __attribute__ ((visibility ("default"))) @@ -81,6 +82,7 @@ class packet; } #endif // HAVE_SEASTAR class deleter; +struct sha1_digest_info_t; namespace ceph { @@ -954,6 +956,7 @@ namespace buffer CEPH_BUFFER_API { } uint32_t crc32c(uint32_t crc) const; void invalidate_crc(); + boost::optional sha1(); // These functions return a bufferlist with a pointer to a single // static buffer. They /must/ not outlive the memory they diff --git a/src/include/types.h b/src/include/types.h index 1140e3f45a50..4bf8e0182c30 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -541,5 +541,28 @@ WRITE_CLASS_ENCODER(errorcode32_t) WRITE_EQ_OPERATORS_1(errorcode32_t, code) WRITE_CMP_OPERATORS_1(errorcode32_t, code) +struct sha1_digest_info_t { +#define SHA1_DIGEST_SIZE 20 + unsigned char v[SHA1_DIGEST_SIZE] = {0}; + + string to_str() const { + char str[SHA1_DIGEST_SIZE*2+1] = {0}; + str[0] = '\0'; + for (size_t i = 0; i < 20; i++) { + ::sprintf(&str[i*2], "%02x", static_cast(v[i])); + } + return string(str); + } + sha1_digest_info_t(unsigned char *_v) { + memcpy(v, _v, 20); + }; + sha1_digest_info_t() {} +}; + +inline ostream& operator<<(ostream& out, const sha1_digest_info_t& b) +{ + string str = b.to_str(); + return out << str; +} #endif diff --git a/src/osd/osd_types.cc b/src/osd/osd_types.cc index 1228137baa3a..26e3a2442508 100644 --- a/src/osd/osd_types.cc +++ b/src/osd/osd_types.cc @@ -1962,6 +1962,7 @@ ostream& operator<<(ostream& out, const pg_pool_t& p) } if (p.get_fingerprint_type() != pg_pool_t::TYPE_FINGERPRINT_NONE) { out << " fingerprint_algorighm " << p.get_fingerprint_name(); + } return out; } -- 2.47.3