COOPENOMICS  v1
Кооперативная Экономика
text_digest.hpp
См. документацию.
1#pragma once
2
3#include <eosio/eosio.hpp>
4#include <eosio/crypto.hpp>
5#include <string>
6
16namespace TextDigest {
17
33 inline constexpr bool PHASE2 = true;
34
36 inline bool is_digest(const std::string &value) {
37 if (value.empty()) return true;
38 if (value.size() != 64) return false;
39 for (char c : value) {
40 const bool hex = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
41 if (!hex) return false;
42 }
43 return true;
44 }
45
47 inline void check_digest(const std::string &value, const char *field) {
48 if (!PHASE2) return;
49 eosio::check(is_digest(value),
50 std::string("Поле ") + field + " принимает sha256 текста (64 шестнадцатеричных символа в нижнем регистре) или пустую строку");
51 }
52
54 inline std::string of(const std::string &text) {
55 if (text.empty()) return text;
56 const auto bytes = eosio::sha256(text.data(), text.size()).extract_as_byte_array();
57 static const char *hex = "0123456789abcdef";
58 std::string out;
59 out.reserve(64);
60 for (const auto byte : bytes) {
61 out.push_back(hex[(byte >> 4) & 0x0f]);
62 out.push_back(hex[byte & 0x0f]);
63 }
64 return out;
65 }
66
67} // namespace TextDigest
Тексты в цепи хранятся хешем.
Definition: text_digest.hpp:16
bool is_digest(const std::string &value)
Значение — хеш текста в принятой записи или пустая строка.
Definition: text_digest.hpp:36
void check_digest(const std::string &value, const char *field)
Отказ, если в поле пришёл текст, а не хеш.
Definition: text_digest.hpp:47
constexpr bool PHASE2
Definition: text_digest.hpp:33
std::string of(const std::string &text)
sha256 текста в той же записи, что считает контроллер; пустой текст остаётся пустым.
Definition: text_digest.hpp:54