Skip to content
Snippets Groups Projects
Select Git revision
  • e3cf8ac9d02a5d0f26d6e9371a9e9f79c3c324cf
  • release default protected
  • 11-22-implement-kv-interface-defined-in-collectiveversionedkvgo
  • hotfix/TestHostPool_UpdateNdf_AddFilter
  • XX-4719/announcementChannels
  • xx-4717/logLevel
  • jonah/noob-channel
  • master protected
  • XX-4707/tagDiskJson
  • xx-4698/notification-retry
  • hotfix/notifylockup
  • syncNodes
  • hotfix/localCB
  • XX-4677/NewChanManagerMobile
  • XX-4689/DmSync
  • duplicatePrefix
  • XX-4601/HavenInvites
  • finalizedUICallbacks
  • XX-4673/AdminKeySync
  • debugNotifID
  • anne/test
  • v4.7.5
  • v4.7.4
  • v4.7.3
  • v4.7.2
  • v4.7.1
  • v4.6.3
  • v4.6.1
  • v4.5.0
  • v4.4.4
  • v4.3.11
  • v4.3.8
  • v4.3.7
  • v4.3.6
  • v4.3.5
  • v4.2.0
  • v4.3.0
  • v4.3.4
  • v4.3.3
  • v4.3.2
  • v4.3.1
41 results

random.go

  • Jono Wenger's avatar
    Jono Wenger authored and Benjamin Wenger committed
    4ebf5d3d
    History
    random.go 2.51 KiB
    ////////////////////////////////////////////////////////////////////////////////
    // Copyright © 2020 xx network SEZC                                           //
    //                                                                            //
    // Use of this source code is governed by a license that can be found in the  //
    // LICENSE file                                                               //
    ////////////////////////////////////////////////////////////////////////////////
    
    package dummy
    
    import (
    	"encoding/binary"
    	"github.com/pkg/errors"
    	"gitlab.com/elixxir/primitives/format"
    	"gitlab.com/xx_network/crypto/csprng"
    	"time"
    ) // Error messages.
    
    const (
    	payloadSizeRngErr = "failed to generate random payload size: %+v"
    )
    
    // intRng returns, as an int, a non-negative, non-zero random number in [1, n)
    // from the csprng.Source.
    func intRng(n int, rng csprng.Source) (int, error) {
    	v, err := csprng.Generate(8, rng)
    	if err != nil {
    		return 0, err
    	}
    
    	return int(binary.LittleEndian.Uint64(v)%uint64(n-1)) + 1, nil
    }
    
    // durationRng returns a duration that is the base duration plus or minus a
    // random duration of max randomRange.
    func durationRng(base, randomRange time.Duration, rng csprng.Source) (
    	time.Duration, error) {
    	delta, err := intRng(int(2*randomRange), rng)
    	if err != nil {
    		return 0, err
    	}
    
    	return base + randomRange - time.Duration(delta), nil
    }
    
    // newRandomPayload generates a random payload of a random length.
    func newRandomPayload(maxPayloadSize int, rng csprng.Source) ([]byte, error) {
    	// Generate random payload size
    	randomPayloadSize, err := intRng(maxPayloadSize, rng)
    	if err != nil {
    		return nil, errors.Errorf(payloadSizeRngErr, err)
    	}
    
    	randomMsg, err := csprng.Generate(randomPayloadSize, rng)
    	if err != nil {
    		return nil, err
    	}
    
    	return randomMsg, nil
    }
    
    // newRandomFingerprint generates a random format.Fingerprint.
    func newRandomFingerprint(rng csprng.Source) (format.Fingerprint, error) {
    	fingerprintBytes, err := csprng.Generate(format.KeyFPLen, rng)
    	if err != nil {
    		return format.Fingerprint{}, err
    	}
    
    	// Create new fingerprint from bytes
    	fingerprint := format.NewFingerprint(fingerprintBytes)
    
    	// Set the first bit to be 0 to comply with the cMix group
    	fingerprint[0] &= 0x7F
    
    	return fingerprint, nil
    }
    
    // newRandomMAC generates a random MAC.
    func newRandomMAC(rng csprng.Source) ([]byte, error) {
    	mac, err := csprng.Generate(format.MacLen, rng)
    	if err != nil {
    		return nil, err
    	}
    
    	// Set the first bit to be 0 to comply with the cMix group
    	mac[0] &= 0x7F
    
    	return mac, nil
    }