cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
ws_utils.c File Reference

Small SHA-1 and Base64 helpers used by the WebSocket handshake path. More...

#include "ws_utils.h"
#include <cwist/core/mem/alloc.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for ws_utils.c:

Go to the source code of this file.

Macros

#define ROL(value, bits)   (((value) << (bits)) | ((value) >> (32 - (bits))))
 
#define blk0(i)    (block->l[i] = (ROL(block->l[i], 24) & 0xFF00FF00) | (ROL(block->l[i], 8) & 0x00FF00FF))
 
#define blk(i)
 
#define R0(v, w, x, y, z, i)
 
#define R1(v, w, x, y, z, i)
 
#define R2(v, w, x, y, z, i)
 
#define R3(v, w, x, y, z, i)
 
#define R4(v, w, x, y, z, i)
 

Functions

void sha1 (const uint8_t *data, size_t len, uint8_t *hash)
 Compute a SHA-1 digest for the provided input bytes.
 
char * base64_encode (const uint8_t *data, size_t input_length, size_t *output_length)
 Base64-encode binary data for WebSocket handshake header generation.
 

Detailed Description

Small SHA-1 and Base64 helpers used by the WebSocket handshake path.

Definition in file ws_utils.c.

Macro Definition Documentation

◆ blk

#define blk (   i)
Value:
(block->l[i & 15] = ROL(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] ^ \
block->l[(i + 2) & 15] ^ block->l[i & 15], \
1))
size_t cap
Definition otel.c:56
#define ROL(value, bits)
Definition ws_utils.c:11

Definition at line 21 of file ws_utils.c.

◆ blk0

#define blk0 (   i)     (block->l[i] = (ROL(block->l[i], 24) & 0xFF00FF00) | (ROL(block->l[i], 8) & 0x00FF00FF))

Definition at line 17 of file ws_utils.c.

◆ R0

#define R0 (   v,
  w,
  x,
  y,
  z,
  i 
)
Value:
z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + ROL(v, 5); \
w = ROL(w, 30);
#define blk0(i)
Definition ws_utils.c:17

Definition at line 27 of file ws_utils.c.

◆ R1

#define R1 (   v,
  w,
  x,
  y,
  z,
  i 
)
Value:
z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + ROL(v, 5); \
w = ROL(w, 30);
#define blk(i)
Definition ws_utils.c:21

Definition at line 30 of file ws_utils.c.

◆ R2

#define R2 (   v,
  w,
  x,
  y,
  z,
  i 
)
Value:
z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + ROL(v, 5); \
w = ROL(w, 30);

Definition at line 33 of file ws_utils.c.

◆ R3

#define R3 (   v,
  w,
  x,
  y,
  z,
  i 
)
Value:
z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + ROL(v, 5); \
w = ROL(w, 30);

Definition at line 36 of file ws_utils.c.

◆ R4

#define R4 (   v,
  w,
  x,
  y,
  z,
  i 
)
Value:
z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + ROL(v, 5); \
w = ROL(w, 30);

Definition at line 39 of file ws_utils.c.

◆ ROL

#define ROL (   value,
  bits 
)    (((value) << (bits)) | ((value) >> (32 - (bits))))

Definition at line 11 of file ws_utils.c.

Function Documentation

◆ base64_encode()

char * base64_encode ( const uint8_t *  data,
size_t  input_length,
size_t *  output_length 
)

Base64-encode binary data for WebSocket handshake header generation.

Base64-encode a byte buffer for handshake header generation.

Parameters
dataInput bytes to encode.
input_lengthNumber of input bytes.
output_lengthOutput parameter that receives the encoded length.
Returns
Heap-allocated Base64 string, or NULL when allocation fails.

Definition at line 258 of file ws_utils.c.

References cap, and cwist_alloc().

Referenced by cwist_websocket_upgrade_response().

◆ sha1()

void sha1 ( const uint8_t *  data,
size_t  len,
uint8_t *  hash 
)

Compute a SHA-1 digest for the provided input bytes.

Compute the SHA-1 digest for a byte range.

Parameters
dataInput bytes to hash.
lenNumber of bytes in data.
hashOutput buffer that receives the 20-byte digest.

Definition at line 235 of file ws_utils.c.

References cap.

Referenced by cwist_websocket_upgrade_response().