-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) 2009-present The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <crypto/hex_base.h> | ||
|
||
#include <array> | ||
#include <cstring> | ||
#include <string> | ||
|
||
namespace { | ||
|
||
using ByteAsHex = std::array<char, 2>; | ||
|
||
constexpr std::array<ByteAsHex, 256> CreateByteToHexMap() | ||
{ | ||
constexpr char hexmap[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; | ||
|
||
std::array<ByteAsHex, 256> byte_to_hex{}; | ||
for (size_t i = 0; i < byte_to_hex.size(); ++i) { | ||
byte_to_hex[i][0] = hexmap[i >> 4]; | ||
byte_to_hex[i][1] = hexmap[i & 15]; | ||
} | ||
return byte_to_hex; | ||
} | ||
|
||
} // namespace | ||
|
||
std::string HexStr(const Span<const uint8_t> s) | ||
{ | ||
std::string rv(s.size() * 2, '\0'); | ||
static constexpr auto byte_to_hex = CreateByteToHexMap(); | ||
static_assert(sizeof(byte_to_hex) == 512); | ||
|
||
char* it = rv.data(); | ||
for (uint8_t v : s) { | ||
std::memcpy(it, byte_to_hex[v].data(), 2); | ||
it += 2; | ||
} | ||
|
||
assert(it == rv.data() + rv.size()); | ||
return rv; | ||
} | ||
|
||
const signed char p_util_hexdigit[256] = | ||
{ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | ||
0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1, | ||
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | ||
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, | ||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, }; | ||
|
||
signed char HexDigit(char c) | ||
{ | ||
return p_util_hexdigit[(unsigned char)c]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) 2009-present The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef BGL_CRYPTO_HEX_BASE_H | ||
#define BGL_CRYPTO_HEX_BASE_H | ||
|
||
#include <span.h> | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
#include <string> | ||
|
||
/** | ||
* Convert a span of bytes to a lower-case hexadecimal string. | ||
*/ | ||
std::string HexStr(const Span<const uint8_t> s); | ||
inline std::string HexStr(const Span<const char> s) { return HexStr(MakeUCharSpan(s)); } | ||
inline std::string HexStr(const Span<const std::byte> s) { return HexStr(MakeUCharSpan(s)); } | ||
|
||
signed char HexDigit(char c); | ||
|
||
#endif // BGL_CRYPTO_HEX_BASE_H |