feat: add proc genUfrag to generate a random username string

This commit is contained in:
Ludovic Chenut 2024-06-10 15:57:40 +02:00
parent 8b9f34959b
commit 9f90721d12
No known key found for this signature in database
GPG Key ID: D9A59B1907F1D50C
1 changed files with 8 additions and 0 deletions

View File

@ -37,6 +37,14 @@ logScope:
export transport, results export transport, results
const charset = toSeq("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/+".items)
proc genUfrag*(rng: ref HmacDrbgContext, size: int): seq[byte] =
# https://github.com/libp2p/specs/blob/master/webrtc/webrtc-direct.md?plain=1#L73-L77
result = newSeq[byte](size)
for resultIndex in 0..<size:
let charsetIndex = rng[].generate(uint) mod charset.len()
result[resultIndex] = charset[charsetIndex].ord().uint8
const const
WebRtcTransportTrackerName* = "libp2p.webrtctransport" WebRtcTransportTrackerName* = "libp2p.webrtctransport"
MaxMessageSize = 16384 # 16KiB; from the WebRtc-direct spec MaxMessageSize = 16384 # 16KiB; from the WebRtc-direct spec