From 3cd37344c5ae525ea0484b2906f32e0d2cb22783 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Sat, 31 Aug 2024 06:18:12 -0700 Subject: [PATCH] Allow for custom encoding nonce (#53) To allow for optional deterministic output, allow for a custom nonce to be set. --- dare.go | 8 ++++++-- sio.go | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/dare.go b/dare.go index 8d8fe0c..5aaf4c0 100644 --- a/dare.go +++ b/dare.go @@ -90,7 +90,9 @@ func newAuthEncV10(cfg *Config) (authEncV10, error) { return authEncV10{}, err } var randVal [8]byte - if _, err = io.ReadFull(cfg.Rand, randVal[:]); err != nil { + if cfg.Nonce != nil { + copy(randVal[:], cfg.Nonce[:8]) + } else if _, err = io.ReadFull(cfg.Rand, randVal[:]); err != nil { return authEncV10{}, err } return authEncV10{ @@ -167,7 +169,9 @@ func newAuthEncV20(cfg *Config) (authEncV20, error) { return authEncV20{}, err } var randVal [12]byte - if _, err = io.ReadFull(cfg.Rand, randVal[:]); err != nil { + if cfg.Nonce != nil { + randVal = *cfg.Nonce + } else if _, err = io.ReadFull(cfg.Rand, randVal[:]); err != nil { return authEncV20{}, err } return authEncV20{ diff --git a/sio.go b/sio.go index 4ec21f7..32985d3 100644 --- a/sio.go +++ b/sio.go @@ -122,6 +122,10 @@ type Config struct { // the default value (crypto/rand.Reader) is used. Rand io.Reader + // Nonce will override the nonce if set non-nil. + // V2 will use all 12 bytes, V1 first 8 bytes. + Nonce *[12]byte + // The size of the encrypted payload in bytes. The // default value is 64KB. It should be used to restrict // the size of encrypted packages. The payload size