Skip to content

Commit

Permalink
Remove newRng (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
Menduist committed Nov 21, 2022
1 parent acbe30e commit cf8b8ce
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 12 deletions.
2 changes: 1 addition & 1 deletion tests/extensions/testextflow.nim
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ suite "Decode frame extensions flow":
var
address: TransportAddress
server: StreamServer
maskKey = genMaskKey(newRng())
maskKey = genMaskKey(HmacDrbgContext.new())
transport: StreamTransport
reader: AsyncStreamReader
frame: Frame
Expand Down
4 changes: 2 additions & 2 deletions tests/testwebsockets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ suite "Test Payload":
address = initTAddress("127.0.0.1:8888"),
frameSize = maxFrameSize)

let maskKey = genMaskKey(newRng())
let maskKey = genMaskKey(HmacDrbgContext.new())
await session.stream.writer.write(
(await Frame(
fin: false,
Expand Down Expand Up @@ -866,7 +866,7 @@ suite "Test Payload":
pong = true
)

let maskKey = genMaskKey(newRng())
let maskKey = genMaskKey(HmacDrbgContext.new())
await session.stream.writer.write(
(await Frame(
fin: false,
Expand Down
8 changes: 1 addition & 7 deletions websock/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ export rand
## Random helpers: similar as in stdlib, but with HmacDrbgContext rng
const randMax = 18_446_744_073_709_551_615'u64

type
Rng* = ref HmacDrbgContext

proc newRng*(): Rng =
# You should only create one instance of the RNG per application / library
# Ref is used so that it can be shared between components
HmacDrbgContext.new()
type Rng* = ref HmacDrbgContext

proc rand*(rng: Rng, max: Natural): int =
if max == 0: return 0
Expand Down
4 changes: 2 additions & 2 deletions websock/websock.nim
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ proc connect*(
rng: Rng = nil): Future[WSSession] {.async.} =

let
rng = if isNil(rng): newRng() else: rng
rng = if isNil(rng): HmacDrbgContext.new() else: rng
key = Base64Pad.encode(genWebSecKey(rng))
hostname = if hostName.len > 0: hostName else: $host

Expand Down Expand Up @@ -364,7 +364,7 @@ proc new*(
return WSServer(
protocols: @protos,
masked: false,
rng: if isNil(rng): newRng() else: rng,
rng: if isNil(rng): HmacDrbgContext.new() else: rng,
frameSize: frameSize,
factories: @factories,
onPing: onPing,
Expand Down

0 comments on commit cf8b8ce

Please sign in to comment.