Any plans in future releases to move from vector<unsigned char> to u8string for binary data in DB?

Added by Plug Gulp 26 days ago

Hello Wittys!

In DBO, to store binary data, the field has to be declared as std::vector<unsigned char>. Some of the data I am storing in the DB is related to user authentication and that is in binary form.

The authentication system uses OpenSSL for generating salt and stretching the password. This binary data is converted to std::string as it is natural and faster to convert a byte of array to std::string compared to storing it in std::vector<unsigned char>. But I have to convert std::string to std::vector<unsigned char> everytime when using DBO to actually persist data. It would be useful and faster to use std::u8string when storing binary data as copying from OpenSSL byte array to std::vector<unsigned char> will not be required.

Are there future plans to support std::u8string for storing binary data in DB?

Thanks and kind regards,

~Plug


Replies (7)

RE: Any plans in future releases to move from vector<unsigned char> to u8string for binary data in DB? - Added by Roel Standaert 26 days ago

No, I don't think so. First of all I'm not convinced that it's really so much more natural and faster to convert a byte array to a std::string as opposed to a std::vector<unsigned char>. I don't know how you're inferring that. Using string types to store binary data is kind of a hack, and using std::u8string (made for storing UTF-8 strings) is just bizarre.

RE: Any plans in future releases to move from vector<unsigned char> to u8string for binary data in DB? - Added by Roel Standaert 26 days ago

I do want to add that maybe std::u8string support will be added in the future, but its intention would be to store UTF-8 strings. I'm kind of waiting for C++20 support in compilers and standard library implementations to be more complete before adding some (optional) C++20 features to Wt.

RE: Any plans in future releases to move from vector<unsigned char> to u8string for binary data in DB? - Added by Plug Gulp 25 days ago

@Roel

I'm not convinced that it's really so much more natural and faster to convert a byte array to a std::string as opposed to a std::vector<unsigned char>

unsigned char *byte_array; // Created and filled in by OpenSSL
size_t length_byte_array; // Assigned by OpenSSL

With string you can do something like this:

std::string(byte_array, byte_array + length_byte_array);

With vector you have to do:

std::vector<unsigned char> vec;
for(size_t i = 0; i < length_byte_array; ++i) {
   vec.push_back(byte_array[i]);
}

RE: Any plans in future releases to move from vector<unsigned char> to u8string for binary data in DB? - Added by Plug Gulp 25 days ago

Looks like the initialisation method for string also works with vector. So instead of for loop, the vector can be initialised as such:

std::vector<unsigned char>(byte_array, byte_array + length_byte_array);

So, I don't need to use string to store data from OpenSSL.

RE: Any plans in future releases to move from vector<unsigned char> to u8string for binary data in DB? - Added by Plug Gulp 25 days ago

But now the problem is the base64encode method uses string as parameter. So if I want to encode some binary data into base64 then I have to convert vector to string!
This has nothing with the original question related to salt and password stretching though.

RE: Any plans in future releases to move from vector<unsigned char> to u8string for binary data in DB? - Added by Roel Standaert 25 days ago

Yeah, that's fair, base64Encode/base64Decode taking/returning a std::string maybe isn't right.

(1-7/7)