CCF
Loading...
Searching...
No Matches
sha256_hash.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "ccf/ds/json.h"
6#include "ccf/service/map.h"
7
8#include <array>
9#include <span>
10#define FMT_HEADER_ONLY
11#include <fmt/format.h>
12
13namespace ccf::crypto
14{
16 {
17 public:
18 static constexpr size_t SIZE = 256 / 8;
19 using Representation = std::array<uint8_t, SIZE>;
21
22 Sha256Hash() = default;
23
24 void set(const Representation& r)
25 {
26 h = r;
27 }
28
29 Sha256Hash(const uint8_t* data, size_t size);
30 explicit Sha256Hash(const std::vector<uint8_t>& vec);
31 explicit Sha256Hash(const std::string& str);
32 Sha256Hash(const Sha256Hash& left, const Sha256Hash& right);
34 const Sha256Hash& first,
35 const Sha256Hash& second,
36 const Sha256Hash& third);
37 Sha256Hash(const Sha256Hash& hash) = default;
38 Sha256Hash& operator=(const Sha256Hash& hash) = default;
39 Sha256Hash(Sha256Hash&& hash) noexcept : h(hash.h) {}
41 {
42 h = hash.h;
43 return *this;
44 }
45
46 friend std::ostream& operator<<(
47 std::ostream& os, const ccf::crypto::Sha256Hash& h);
48
49 [[nodiscard]] std::string hex_str() const;
50
51 static Sha256Hash from_hex_string(const std::string& str);
52 static Sha256Hash from_span(const std::span<const uint8_t, SIZE>& sp);
54 };
55
56 void to_json(nlohmann::json& j, const Sha256Hash& hash);
57
58 void from_json(const nlohmann::json& j, Sha256Hash& hash);
59
60 std::string schema_name(const Sha256Hash* hash);
61
62 void fill_json_schema(nlohmann::json& schema, const Sha256Hash* hash);
63
64 bool operator==(const Sha256Hash& lhs, const Sha256Hash& rhs);
65
66 bool operator!=(const Sha256Hash& lhs, const Sha256Hash& rhs);
67}
68
69FMT_BEGIN_NAMESPACE
70template <>
71struct formatter<ccf::crypto::Sha256Hash>
72{
73 template <typename ParseContext>
74 constexpr auto parse(ParseContext& ctx)
75 {
76 return ctx.begin();
77 }
78
79 template <typename FormatContext>
80 auto format(const ccf::crypto::Sha256Hash& p, FormatContext& ctx) const
81 {
82 return format_to(ctx.out(), "<sha256 {:02x}>", fmt::join(p.h, ""));
83 }
84};
85FMT_END_NAMESPACE
86
88{
89 template <>
91 {
93 {
94 auto hex_str = h.hex_str();
95 return {hex_str.begin(), hex_str.end()};
96 }
97
99 {
100 auto data_str = std::string{data.begin(), data.end()};
102 }
103 };
104}
Definition sha256_hash.h:16
Representation h
Definition sha256_hash.h:20
static Sha256Hash from_representation(const Representation &r)
Definition sha256_hash.cpp:76
static Sha256Hash from_span(const std::span< const uint8_t, SIZE > &sp)
Definition sha256_hash.cpp:69
Sha256Hash(const Sha256Hash &hash)=default
Sha256Hash(Sha256Hash &&hash) noexcept
Definition sha256_hash.h:39
std::array< uint8_t, SIZE > Representation
Definition sha256_hash.h:19
Sha256Hash & operator=(Sha256Hash &&hash) noexcept
Definition sha256_hash.h:40
std::string hex_str() const
Definition sha256_hash.cpp:57
void set(const Representation &r)
Definition sha256_hash.h:24
friend std::ostream & operator<<(std::ostream &os, const ccf::crypto::Sha256Hash &h)
Definition sha256_hash.cpp:51
static constexpr size_t SIZE
Definition sha256_hash.h:18
static Sha256Hash from_hex_string(const std::string &str)
Definition sha256_hash.cpp:62
Sha256Hash & operator=(const Sha256Hash &hash)=default
Definition base64.h:10
bool operator!=(const Sha256Hash &lhs, const Sha256Hash &rhs)
Definition sha256_hash.cpp:134
void fill_json_schema(nlohmann::json &schema, const Pem *pem)
Definition pem.h:107
std::string schema_name(const Pem *pem)
Definition pem.h:99
void to_json(nlohmann::json &j, const Pem &p)
Definition pem.h:77
bool operator==(const Sha256Hash &lhs, const Sha256Hash &rhs)
Definition sha256_hash.cpp:122
void from_json(const nlohmann::json &j, Pem &p)
Definition pem.h:82
Definition sha256_hash.h:88
ccf::ByteVector SerialisedEntry
Definition serialised_entry.h:8
Definition app_interface.h:14
static ccf::crypto::Sha256Hash from_serialised(const SerialisedEntry &data)
Definition sha256_hash.h:98
static SerialisedEntry to_serialised(const ccf::crypto::Sha256Hash &h)
Definition sha256_hash.h:92
Definition blit_serialiser.h:14
auto format(const ccf::crypto::Sha256Hash &p, FormatContext &ctx) const
Definition sha256_hash.h:80
constexpr auto parse(ParseContext &ctx)
Definition sha256_hash.h:74