@@ -137,18 +137,21 @@ std::string sha1_base64(std::string_view source) {
137
137
ret != GNUTLS_E_SUCCESS) {
138
138
throw websocket::exception (fmt::format (" gnutls_hash_fast: {}" , gnutls_strerror (ret)));
139
139
}
140
- gnutls_datum_t hash_data{
141
- .data = hash,
142
- .size = sizeof (hash),
140
+ return encode_base64 (std::string_view (reinterpret_cast <const char *>(hash), sizeof (hash)));
141
+ }
142
+
143
+ std::string encode_base64 (std::string_view source) {
144
+ gnutls_datum_t src_data{
145
+ .data = reinterpret_cast <uint8_t *>(const_cast <char *>(source.data ())),
146
+ .size = static_cast <unsigned >(source.size ())
143
147
};
144
- gnutls_datum_t base64_encoded;
145
- if (int ret = gnutls_base64_encode2 (&hash_data, &base64_encoded);
146
- ret != GNUTLS_E_SUCCESS) {
148
+ gnutls_datum_t encoded_data;
149
+ if (int ret = gnutls_base64_encode2 (&src_data, &encoded_data); ret != GNUTLS_E_SUCCESS) {
147
150
throw websocket::exception (fmt::format (" gnutls_base64_encode2: {}" , gnutls_strerror (ret)));
148
151
}
149
- auto free_base64_encoded = defer ([&] () noexcept { gnutls_free (base64_encoded .data ); });
152
+ auto free_encoded_data = defer ([&] () noexcept { gnutls_free (encoded_data .data ); });
150
153
// base64_encoded.data is "unsigned char *"
151
- return std::string (reinterpret_cast <const char *>(base64_encoded .data ), base64_encoded .size );
154
+ return std::string (reinterpret_cast <const char *>(encoded_data .data ), encoded_data .size );
152
155
}
153
156
154
157
}
0 commit comments