Please note that the main reason for including iostream late is to show that the only required header for the code above is string.
I hope I didn't miss anything, but I attempted to keep the properties of the old function. Previously you also required enough space to keep the full buffer, so this should be largely the same.
Obviously you could also create two lookup tables, one lowercase, one uppercase and alternate between them base on a bool argument, for example (if you prefer that flexibility). Personally I am used to viewing hex in all uppercase.
If you feel like the above (with or without the discussed alterations) is worth including in uthenticode, please respond in a comment and I'll send a PR.
I am attaching a small benchmark merely for the performance aspect. But I think the above method is both simpler to write (consider the iomanip stuff in the original code) and to read and faster as well.
$ hyperfine ./old_tohex; hyperfine ./new_tohex
Benchmark 1: ./old_tohex
Time (mean ± σ): 6.061 s ± 0.137 s [User: 4.812 s, System: 1.248 s]
Range (min … max): 5.885 s … 6.276 s 10 runs
Benchmark 1: ./new_tohex
Time (mean ± σ): 1.672 s ± 0.009 s [User: 0.672 s, System: 1.000 s]
Range (min … max): 1.658 s … 1.690 s 10 runs
hyperfine can be installed with cargo install hyperfine if you have Rust installed. Otherwise use whatever other tool you normally use for the purpose.
We need no warmup, because we read from /dev/urandom and throw it away into /dev/null after converting with either of the tohex() variants.
As far as I am concerned the optimized variant comes under the Unlicense, i.e. it is placed into the public domain plus the disclaimer of the MIT license. So if you don't insist on a PR, simply copy it over at your discretion if you find it worthy of inclusion.
Hi, I think you can do without a
std::stringstream
for a simple function liketohex()
and still have a robust (RAII etc.) version.Here's an alternative implementation in the context of a small test program:
Please note that the main reason for including
iostream
late is to show that the only required header for the code above isstring
.I hope I didn't miss anything, but I attempted to keep the properties of the old function. Previously you also required enough space to keep the full buffer, so this should be largely the same.
Obviously you could also create two lookup tables, one lowercase, one uppercase and alternate between them base on a
bool
argument, for example (if you prefer that flexibility). Personally I am used to viewing hex in all uppercase.If you feel like the above (with or without the discussed alterations) is worth including in uthenticode, please respond in a comment and I'll send a PR.
I am attaching a small benchmark merely for the performance aspect. But I think the above method is both simpler to write (consider the
iomanip
stuff in the original code) and to read and faster as well.Benchmark: tohex_bench.zip or clone from here
Output:
hyperfine
can be installed withcargo install hyperfine
if you have Rust installed. Otherwise use whatever other tool you normally use for the purpose.We need no warmup, because we read from /dev/urandom and throw it away into /dev/null after converting with either of the
tohex()
variants.As far as I am concerned the optimized variant comes under the Unlicense, i.e. it is placed into the public domain plus the disclaimer of the MIT license. So if you don't insist on a PR, simply copy it over at your discretion if you find it worthy of inclusion.