-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Removed Unrequired Seeding of Math Rand - #2 Added the functional Option to supply the function for generatingXIDs
- Loading branch information
Showing
5 changed files
with
136 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package dhcp4client | ||
|
||
import ( | ||
cryptorand "crypto/rand" | ||
mathrand "math/rand" | ||
) | ||
|
||
func CryptoGenerateXID(b []byte) { | ||
if _, err := cryptorand.Read(b); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func MathGenerateXID(b []byte) { | ||
if _, err := mathrand.Read(b); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package dhcp4client_test | ||
|
||
import ( | ||
"bytes" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/d2g/dhcp4client" | ||
) | ||
|
||
func Test_GenerateXID(t *testing.T) { | ||
//Set the math seed so we always get the same result. | ||
rand.Seed(1) | ||
|
||
crypto_messageid := make([]byte, 4) | ||
dhcp4client.CryptoGenerateXID(crypto_messageid) | ||
|
||
t.Logf("Crypto Token: %v", crypto_messageid) | ||
|
||
math_messageid := make([]byte, 4) | ||
dhcp4client.MathGenerateXID(math_messageid) | ||
|
||
//Math token shouldn't change as we don't seed it. | ||
if !bytes.Equal(math_messageid, []byte{82, 253, 252, 7}) { | ||
t.Errorf("Math Token was %v, expected %v", math_messageid, []byte{82, 253, 252, 7}) | ||
t.Fail() | ||
} | ||
|
||
} |