Skip to content

Commit

Permalink
Allow for custom encoding nonce
Browse files Browse the repository at this point in the history
To allow for optional deterministic output, allow for a custom nonce to be set.
  • Loading branch information
klauspost committed Aug 30, 2024
1 parent 2b8420b commit 8a97bcb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions dare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
4 changes: 4 additions & 0 deletions sio.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8a97bcb

Please sign in to comment.