1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/net/websocket/utils.v

42 lines
1.1 KiB
V
Raw Normal View History

module websocket
import rand
import crypto.sha1
import encoding.base64
import encoding.binary
// htonl64 converts payload length to header bits
2022-04-15 15:35:35 +03:00
fn htonl64(payload_len u64) []u8 {
mut ret := []u8{len: 8}
binary.big_endian_put_u64(mut ret, payload_len)
return ret
}
2023-05-12 09:31:27 +03:00
// create_masking_key returns a new masking key to use when masking websocket messages
2022-04-15 15:35:35 +03:00
fn create_masking_key() []u8 {
return rand.bytes(4) or { [0, 0, 0, 0] }
}
2023-05-12 09:31:27 +03:00
// create_key_challenge_response creates a key challenge response from security key
fn create_key_challenge_response(seckey string) !string {
if seckey.len == 0 {
2023-05-12 09:31:27 +03:00
return error('unexpected seckey length zero')
}
guid := '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
sha1buf := seckey + guid
shabytes := sha1buf.bytes()
hash := sha1.sum(shabytes)
b64 := base64.encode(hash)
unsafe {
hash.free()
shabytes.free()
}
return b64
}
// get_nonce creates a randomized array used in handshake process
fn get_nonce(nonce_size int) string {
return rand.string_from_set('0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz',
nonce_size)
}