mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-11 16:40:17 +00:00
Make Mongoose's SHA1 algo pluggable
PUBLISHED_FROM=bdaeed6628ffb544e31d7fd59475f3a2e78c2967
This commit is contained in:
1 parent
ced823cd95
commit
4d65cb1c0f
1 file changed
+22
-9
+22
-9
@@ -4791,19 +4791,32 @@ static void mg_websocket_handler(struct mg_connection *nc, int ev,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef MG_EXT_SHA1
|
||||||
|
static void mg_hash_sha1_v(size_t num_msgs, const uint8_t *msgs[],
|
||||||
|
const size_t *msg_lens, uint8_t *digest) {
|
||||||
|
size_t i;
|
||||||
|
cs_sha1_ctx sha_ctx;
|
||||||
|
cs_sha1_init(&sha_ctx);
|
||||||
|
for (i = 0; i < num_msgs; i++) {
|
||||||
|
cs_sha1_update(&sha_ctx, msgs[i], msg_lens[i]);
|
||||||
|
}
|
||||||
|
cs_sha1_final(digest, &sha_ctx);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
extern void mg_hash_sha1_v(size_t num_msgs, const uint8_t *msgs[],
|
||||||
|
const size_t *msg_lens, uint8_t *digest);
|
||||||
|
#endif
|
||||||
|
|
||||||
static void mg_ws_handshake(struct mg_connection *nc,
|
static void mg_ws_handshake(struct mg_connection *nc,
|
||||||
const struct mg_str *key) {
|
const struct mg_str *key) {
|
||||||
static const char *magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
static const char *magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
||||||
char buf[MG_VPRINTF_BUFFER_SIZE], sha[20], b64_sha[sizeof(sha) * 2];
|
const uint8_t *msgs[2] = {(const uint8_t *) key->p, (const uint8_t *) magic};
|
||||||
cs_sha1_ctx sha_ctx;
|
const size_t msg_lens[2] = {key->len, 36};
|
||||||
|
unsigned char sha[20];
|
||||||
|
char b64_sha[30];
|
||||||
|
|
||||||
snprintf(buf, sizeof(buf), "%.*s%s", (int) key->len, key->p, magic);
|
mg_hash_sha1_v(2, msgs, msg_lens, sha);
|
||||||
|
mg_base64_encode(sha, sizeof(sha), b64_sha);
|
||||||
cs_sha1_init(&sha_ctx);
|
|
||||||
cs_sha1_update(&sha_ctx, (unsigned char *) buf, strlen(buf));
|
|
||||||
cs_sha1_final((unsigned char *) sha, &sha_ctx);
|
|
||||||
|
|
||||||
mg_base64_encode((unsigned char *) sha, sizeof(sha), b64_sha);
|
|
||||||
mg_printf(nc, "%s%s%s",
|
mg_printf(nc, "%s%s%s",
|
||||||
"HTTP/1.1 101 Switching Protocols\r\n"
|
"HTTP/1.1 101 Switching Protocols\r\n"
|
||||||
"Upgrade: websocket\r\n"
|
"Upgrade: websocket\r\n"
|
||||||
|
|||||||
Reference in new issue
Block a user