From bc7e9740d881f766063d653dba7434939a389563 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sun, 2 Aug 2020 00:11:50 +0000 Subject: [PATCH] make key type deterministic with seed --- mockid/api/common.go | 9 +++++++-- mockid/mockid_test.go | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/mockid/api/common.go b/mockid/api/common.go index 8d6b82e..b2fca1e 100644 --- a/mockid/api/common.go +++ b/mockid/api/common.go @@ -41,13 +41,18 @@ func getOpts(r *http.Request) (*options, error) { seed, _ = binary.ReadVarint(bytes.NewReader(b[0:8])) } + var n int if 0 != seed { - rndReader = rand.New(rand.NewSource(seed)) + rnd := rand.New(rand.NewSource(seed)) + rndReader = rnd + n = rnd.Intn(2) + } else { + n = rand.Intn(2) } kty, _ := tok["kty"].(string) if "" == kty { - if 0 == rand.Intn(2) { + if 0 == n { kty = "RSA" } else { kty = "EC" diff --git a/mockid/mockid_test.go b/mockid/mockid_test.go index 9e45588..db6dc69 100644 --- a/mockid/mockid_test.go +++ b/mockid/mockid_test.go @@ -121,7 +121,7 @@ func TestGenWithSeed(t *testing.T) { res, err := client.Do(&http.Request{ Method: "POST", URL: urlstr, - Body: ioutil.NopCloser(bytes.NewReader([]byte(`{"seed":"test"}`))), + Body: ioutil.NopCloser(bytes.NewReader([]byte(`{"seed":"c"}`))), }) if nil != err { //t.Fatal(err) @@ -141,7 +141,7 @@ func TestGenWithSeed(t *testing.T) { res, err = client.Do(&http.Request{ Method: "POST", URL: urlstr, - Body: ioutil.NopCloser(bytes.NewReader([]byte(`{"seed":"test"}`))), + Body: ioutil.NopCloser(bytes.NewReader([]byte(`{"seed":"c"}`))), }) if nil != err { //t.Fatal(err)