REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
dcp.h
Go to the documentation of this file.
1#ifndef UTILS_TEENSY_DCP_H
2#define UTILS_TEENSY_DCP_H
3
4#include <cstdint>
5#include <cstddef>
6#include <string>
7#include <array>
8#include <stdlib.h>
9
10namespace utils {
11 using sha256_t = std::array<uint8_t, 32>;
12 using sha1_t = std::array<uint8_t, 20>;
13
19 void hash_sha256(const uint8_t* msg, size_t msg_len, uint8_t* out_hash);
20
26 void hash_sha1(const uint8_t* msg, size_t msg_len, uint8_t* out_hash);
27
28
29 inline sha256_t hash_sha256(const uint8_t* msg, size_t msg_len) {
30 sha256_t ret; hash_sha256(msg, msg_len, ret.data()); return ret;
31 }
32
33 inline std::string sha256_to_string(const utils::sha256_t& hash) {
34 char out64[64+1] = {0}; // 0-terminated
35 for(int pin=0; pin<32; pin++) {
36 int pout = 2*pin;
37 std::sprintf(out64+pout, "%02x", hash[pin]);
38 }
39 return std::string(out64);
40 }
41
42 // TODO: Remove redundancy in all over this file. Maybe optin all for the
43 // classes below.
44
45 inline std::string sha1_to_string(const utils::sha1_t& hash) {
46 char out64[40+1] = {0}; // 0-terminated
47 for(int pin=0; pin<20; pin++) {
48 int pout = 2*pin;
49 std::sprintf(out64+pout, "%02x", hash[pin]);
50 }
51 return std::string(out64);
52 }
53
54 inline sha256_t parse_sha256(const std::string& hash) {
55 sha256_t ret; char stroct[3]; stroct[2] = '\0';
56 for(int i=0; i<32; i++) {
57 stroct[0] = hash[2*i]; stroct[1] = hash[2*i+1];
58 ret[i] = strtoul(stroct, NULL, 16);
59 }
60 return ret;
61 }
62
63 // convenience
64 struct sha256 {
66 sha256(const uint8_t* msg, size_t msg_len) { hash_sha256(msg, msg_len, checksum.data()); }
67 std::string to_string() const { return sha256_to_string(checksum); }
68 std::string short_string() const { return to_string().substr(0,7); }
69 };
70
71 struct sha1 {
73 sha1(const uint8_t* msg, size_t msg_len) { hash_sha1(msg, msg_len, checksum.data()); }
74 std::string to_string() const { return sha1_to_string(checksum); }
75 std::string short_string() const { return to_string().substr(0,7); }
76 };
77
78 // compares two hashes, where any kind of abbreviation is allowed, in particular the famous
79 // "git short hashes" which are only the first 7 digits. To make this useful, the shorter
80 // has should not be too short.
81 inline bool sha256_test_short(std::string a, std::string b) {
82 auto& longer = a.length() > b.length() ? a : b,
83 shorter = a.length() > b.length() ? b : a;
84 return shorter == longer.substr(0, shorter.length());
85 }
86
87 // note: Could expose further functions, such as prhash(out_hash, 32) for a
88 // simple string representation
89
90} // end of namespace
91#endif /* UTILS_TEENSY_DCP_H */
Definition carrier.h:12
std::array< uint8_t, 32 > sha256_t
Definition dcp.h:11
FLASHMEM void hash(const uint8_t *msg, size_t msg_len, uint8_t *out_hash, dcp_hash_algo_t algo)
Definition dcp.cpp:611
sha256_t parse_sha256(const std::string &hash)
Definition dcp.h:54
FLASHMEM void hash_sha1(const uint8_t *msg, size_t msg_len, uint8_t *out_hash)
Computes the SHA1 sum of an arbitrary message (large memory segment), hardware-accelerated on the Tee...
Definition dcp.cpp:635
FLASHMEM void hash_sha256(const uint8_t *msg, size_t msg_len, uint8_t *out_hash)
Computes the SHA256 sum of an arbitrary message (large memory segment).
Definition dcp.cpp:631
bool sha256_test_short(std::string a, std::string b)
Definition dcp.h:81
std::array< uint8_t, 20 > sha1_t
Definition dcp.h:12
std::string sha1_to_string(const utils::sha1_t &hash)
Definition dcp.h:45
std::string sha256_to_string(const utils::sha256_t &hash)
Definition dcp.h:33
sha1(const uint8_t *msg, size_t msg_len)
Definition dcp.h:73
utils::sha1_t checksum
Definition dcp.h:72
std::string short_string() const
Definition dcp.h:75
std::string to_string() const
Definition dcp.h:74
utils::sha256_t checksum
Definition dcp.h:65
std::string short_string() const
Definition dcp.h:68
sha256(const uint8_t *msg, size_t msg_len)
Definition dcp.h:66
std::string to_string() const
Definition dcp.h:67