forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBase32.h
More file actions
54 lines (46 loc) · 1.74 KB
/
Copy pathBase32.h
File metadata and controls
54 lines (46 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#pragma once
#include "Data.h"
#include <TrezorCrypto/base32.h>
#include <cassert>
namespace TW::Base32 {
/// Decode Base32 string, return bytes as Data
/// alphabet: Optional alphabet, if missing, default ALPHABET_RFC4648
inline bool decode(const std::string& encoded_in, Data& decoded_out, const char* alphabet_in = nullptr) {
size_t inLen = encoded_in.size();
// obtain output length first
size_t outLen = base32_decoded_length(inLen);
uint8_t buf[outLen];
if (alphabet_in == nullptr) {
alphabet_in = BASE32_ALPHABET_RFC4648;
}
// perform the base32 decode
uint8_t* retval = base32_decode(encoded_in.data(), inLen, buf, outLen, alphabet_in);
if (retval == nullptr) {
return false;
}
decoded_out.assign(buf, buf + outLen);
return true;
}
/// Encode bytes in Data to Base32 string
/// alphabet: Optional alphabet, if missing, default ALPHABET_RFC4648
inline std::string encode(const Data& val, const char* alphabet = nullptr) {
size_t inLen = val.size();
// obtain output length first, reserve for terminator
size_t outLen = base32_encoded_length(inLen) + 1;
char buf[outLen];
if (alphabet == nullptr) {
alphabet = BASE32_ALPHABET_RFC4648;
}
// perform the base32 encode
char* retval = base32_encode(val.data(), inLen, buf, outLen, alphabet);
assert(retval != nullptr);
// make sure there is a terminator ath the end
buf[outLen - 1] = '\0';
return std::string(buf);
}
} // namespace TW::Base32