nim-websock/src/random.nim
Arijit Das a1ae7d2c70
[WIP] Web socket client implementation. (#2)
* Implement websocket server.

* Implement websocket client.

* Run nimpretty.

* Remove commented code.

* Address comments.

* Address comments on websocket server.

* Use seq[byte] to store data.

* Working bytes conversion.

* Remove result from return

* Refactor the code.

* Minor change.

* Add test.

* Add websocket test and fix closing handshake.

* Add MsgReader to read data in external buffer.
2021-03-10 21:34:14 -06:00

26 lines
848 B
Nim

import bearssl
## Random helpers: similar as in stdlib, but with BrHmacDrbgContext rng
const randMax = 18_446_744_073_709_551_615'u64
proc rand*(rng: var BrHmacDrbgContext, max: Natural): int =
if max == 0: return 0
var x: uint64
while true:
brHmacDrbgGenerate(addr rng, addr x, csize_t(sizeof(x)))
if x < randMax - (randMax mod (uint64(max) + 1'u64)): # against modulo bias
return int(x mod (uint64(max) + 1'u64))
proc genMaskKey*(rng: ref BrHmacDrbgContext): array[4, char] =
## Generates a random key of 4 random chars.
proc r(): char = char(rand(rng[], 255))
return [r(), r(), r(), r()]
proc genWebSecKey*(rng: ref BrHmacDrbgContext): seq[char] =
var key = newSeq[char](16)
proc r(): char = char(rand(rng[], 255))
## Generates a random key of 16 random chars.
for i in 0..15:
key.add(r())
return key