Skip to content

Commit e6a000f

Browse files
committed
update hex encoding to use sodium
1 parent 39a6741 commit e6a000f

File tree

3 files changed

+12
-21
lines changed

3 files changed

+12
-21
lines changed

index.hxx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <iomanip>
1111
#include <exception>
1212

13-
#include "deps/datcxx/sodium-base64/index.hxx"
13+
#include "deps/datcxx/sodium-encodedecode/index.hxx"
1414

1515
///
1616
/// namespace Hyper
@@ -166,7 +166,7 @@ namespace Hyper {
166166
/// method toString(const std::string& encoding, size_t start = 0, size_t end = 0)
167167
/// comment Get the value of of the buffer as a string.
168168
///
169-
/// param encoding type.
169+
/// param encoding type (currently supports "base64" or "hex").
170170
///
171171
/// return std::string
172172
///
@@ -179,20 +179,17 @@ namespace Hyper {
179179

180180
std::stringstream ss;
181181

182-
//
183-
// TODO add more encodings
184-
//
185-
if (encoding == "hex") {
186-
ss << std::hex;
187-
}
188-
189182
std::string substring(
190183
this->value.begin() + start,
191184
this->value.begin() + end
192185
);
193186

194187
if (encoding == "base64") {
195-
return Hyper::Sodium::base64Encode(substring);
188+
return Hyper::Sodium::Base64::encode(substring);
189+
}
190+
191+
if (encoding == "hex") {
192+
return Hyper::Sodium::Hex::encode(substring);
196193
}
197194

198195
ss << substring;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
"index.hxx"
2222
],
2323
"dependencies": {
24-
"[email protected]:datcxx/sodium-base64": "066fb26f"
24+
"[email protected]:datcxx/sodium-encodedecode": "345051bd"
2525
}
26-
}
26+
}

test/index.cxx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,15 @@ int main() {
183183

184184
t.test("toString(\"hex\", 0, 0)", [](auto t) {
185185
Buffer<uint8_t> a("abc");
186-
std::stringstream ss;
187-
ss << std::hex << "abc";
188-
std::string expected = ss.str();
186+
std::string expected = "616263";
189187

190-
t->equal(a.toString("hex"), expected, "strings are equal");
188+
t->equal(a.toString("hex"), expected, "hex strings are equal");
191189
t->end();
192190
});
193191

194192
t.test("toString(\"hex\", 3, 9)", [](auto t) {
195193
Buffer<uint8_t> a("abcdef0123456789");
196-
std::stringstream ss;
197-
ss << std::hex << "def012";
198-
std::string expected = ss.str();
194+
std::string expected = "646566303132";
199195

200196
t->equal(a.toString("hex", 3, 9), expected, "strings are equal");
201197
t->end();
@@ -204,7 +200,6 @@ int main() {
204200
t.test("toString(\"base64\")", [](auto t) {
205201
Buffer<uint8_t> buf("Hello, world!");
206202
std::string expected = "SGVsbG8sIHdvcmxkIQ==";
207-
expected.push_back('\0');
208203

209204
t->equal(buf.toString("base64"), expected, "strings are equal");
210205
t->end();
@@ -213,7 +208,6 @@ int main() {
213208
t.test("toString(\"base64\", 1, 5)", [](auto t) {
214209
Buffer<uint8_t> buf("Hello, world!");
215210
std::string expected = "ZWxsbw==";
216-
expected.push_back('\0');
217211

218212
t->equal(buf.toString("base64", 1, 5), expected, "strings are equal");
219213
t->notEqual(buf.toString("base64", 1, 4), expected, "strings are not equal");

0 commit comments

Comments
 (0)