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 inline void set(Representation&& r)
25 {
26 h = std::move(r);
27 }
28
29 Sha256Hash(const uint8_t* data, size_t size);
30 Sha256Hash(const std::vector<uint8_t>& vec);
31 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
38 friend std::ostream& operator<<(
39 std::ostream& os, const ccf::crypto::Sha256Hash& h);
40
41 std::string hex_str() const;
42
43 static Sha256Hash from_hex_string(const std::string& str);
44 static Sha256Hash from_span(const std::span<const uint8_t, SIZE>& sp);
46 };
47
48 void to_json(nlohmann::json& j, const Sha256Hash& hash);
49
50 void from_json(const nlohmann::json& j, Sha256Hash& hash);
51
52 std::string schema_name(const Sha256Hash*);
53
54 void fill_json_schema(nlohmann::json& schema, const Sha256Hash*);
55
56 bool operator==(const Sha256Hash& lhs, const Sha256Hash& rhs);
57
58 bool operator!=(const Sha256Hash& lhs, const Sha256Hash& rhs);
59}
60
61FMT_BEGIN_NAMESPACE
62template <>
63struct formatter<ccf::crypto::Sha256Hash>
64{
65 template <typename ParseContext>
66 constexpr auto parse(ParseContext& ctx)
67 {
68 return ctx.begin();
69 }
70
71 template <typename FormatContext>
72 auto format(const ccf::crypto::Sha256Hash& p, FormatContext& ctx) const
73 {
74 return format_to(ctx.out(), "<sha256 {:02x}>", fmt::join(p.h, ""));
75 }
76};
77FMT_END_NAMESPACE
78
80{
81 template <>
83 {
85 {
86 auto hex_str = h.hex_str();
87 return SerialisedEntry(hex_str.begin(), hex_str.end());
88 }
89
91 {
92 auto data_str = std::string{data.begin(), data.end()};
94 return ret.from_hex_string(data_str);
95 }
96 };
97}
Definition sha256_hash.h:16
void set(Representation &&r)
Definition sha256_hash.h:24
Representation h
Definition sha256_hash.h:20
static Sha256Hash from_representation(const Representation &r)
Definition sha256_hash.cpp:80
static Sha256Hash from_span(const std::span< const uint8_t, SIZE > &sp)
Definition sha256_hash.cpp:73
std::array< uint8_t, SIZE > Representation
Definition sha256_hash.h:19
std::string hex_str() const
Definition sha256_hash.cpp:61
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:66
Definition base64.h:10
bool operator!=(const Sha256Hash &lhs, const Sha256Hash &rhs)
Definition sha256_hash.cpp:138
void fill_json_schema(nlohmann::json &schema, const Pem *)
Definition pem.h:107
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:126
std::string schema_name(const Pem *)
Definition pem.h:99
void from_json(const nlohmann::json &j, Pem &p)
Definition pem.h:82
Definition sha256_hash.h:80
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:90
static SerialisedEntry to_serialised(const ccf::crypto::Sha256Hash &h)
Definition sha256_hash.h:84
Definition blit_serialiser.h:14
auto format(const ccf::crypto::Sha256Hash &p, FormatContext &ctx) const
Definition sha256_hash.h:72
constexpr auto parse(ParseContext &ctx)
Definition sha256_hash.h:66