From 07b2fd08e7c43d2b24a6cd5a3525cc10dc7dd802 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 20 Sep 2024 10:08:32 +0200 Subject: [PATCH] Fix linter errors, update required Go version --- .github/workflows/test.yaml | 4 ++-- .travis.yml | 11 ----------- dh.go | 4 +++- generator.go | 13 +++++++------ generator_test.go | 7 ++++--- go.mod | 2 +- validate.go | 28 +++++++++++++++------------- 7 files changed, 32 insertions(+), 37 deletions(-) delete mode 100644 .travis.yml diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 797d115..ce93ff7 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -9,8 +9,8 @@ jobs: strategy: matrix: version: - - '1.19-alpine' - - '1.20-alpine' + - '1.22-alpine' + - '1.23-alpine' - alpine defaults: diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index cc94f50..0000000 --- a/.travis.yml +++ /dev/null @@ -1,11 +0,0 @@ -language: go - -go: - - 1.12.x - - 1.13.x - - 1.14.x - - tip - -script: - - go vet - - go test -v -cover -timeout 1800s diff --git a/dh.go b/dh.go index c721f97..40e1011 100644 --- a/dh.go +++ b/dh.go @@ -1,3 +1,5 @@ +// Package dhparam is a pure Golang implementation of the openssl +// dhparam generator no requiring any CGO bindings package dhparam import ( @@ -23,7 +25,7 @@ type DH struct { // Decode reads a DH parameters struct from its PEM data func Decode(pemData []byte) (*DH, error) { - if pemData == nil || len(pemData) == 0 { + if len(pemData) == 0 { return nil, ErrNoPem } diff --git a/generator.go b/generator.go index 1297f3b..eb93f02 100644 --- a/generator.go +++ b/generator.go @@ -59,9 +59,9 @@ func GenerateWithContext(ctx context.Context, bits int, generator Generator, cb } switch generator { - case 2: + case 2: //nolint:mnd padd, rem = 24, 11 - case 5: + case 5: //nolint:mnd padd, rem = 10, 3 default: padd, rem = 2, 1 @@ -70,7 +70,7 @@ func GenerateWithContext(ctx context.Context, bits int, generator Generator, cb for { select { case <-ctx.Done(): - return nil, ctx.Err() + return nil, ctx.Err() //nolint:wrapcheck // Fine in this case default: if prime, err = genPrime(bits, big.NewInt(padd), big.NewInt(rem)); err != nil { return nil, err @@ -107,7 +107,7 @@ func genPrime(bits int, padd, rem *big.Int) (*big.Int, error) { err error p = new(big.Int) qadd = new(big.Int) - q = new(big.Int) + q *big.Int t1 = new(big.Int) ) @@ -146,10 +146,11 @@ func mightBePrime(i *big.Int) bool { return true } +//nolint:mnd func genRand(bits int) (*big.Int, error) { bytes := (bits + 7) / 8 bit := (bits - 1) % 8 - mask := 0xff << uint(bit+1) + mask := 0xff << uint(bit+1) //#nosec:G115 // Should only ever run with positive ints buf := make([]byte, bytes) if _, err := rand.Read(buf); err != nil { @@ -160,7 +161,7 @@ func genRand(bits int) (*big.Int, error) { buf[0] = 1 buf[1] |= 0x80 } else { - buf[0] |= 3 << uint(bit-1) + buf[0] |= 3 << uint(bit-1) //#nosec:G115 // Should only ever run with positive ints } buf[0] &= byte(^mask) diff --git a/generator_test.go b/generator_test.go index 15050ee..2453955 100644 --- a/generator_test.go +++ b/generator_test.go @@ -11,6 +11,7 @@ import ( "time" ) +//nolint:errcheck,gosec,revive func opensslOutput(r GeneratorResult) { switch r { case GeneratorFoundPossiblePrime: @@ -39,15 +40,15 @@ func execGeneratorIntegration(t *testing.T, bitsize int, generator Generator) { if err != nil { t.Fatalf("Unable to create tempfile: %s", err) } - defer os.Remove(f.Name()) + defer os.Remove(f.Name()) //nolint:errcheck if _, err = f.Write(pem); err != nil { t.Fatalf("Unable to write tempfile: %s", err) } - f.Close() + f.Close() //nolint:errcheck,gosec,revive - cmd := exec.Command("openssl", "dhparam", "-inform", "PEM", "-in", f.Name(), "-check", "-noout", "-text") + cmd := exec.Command("openssl", "dhparam", "-inform", "PEM", "-in", f.Name(), "-check", "-noout", "-text") //#nosec:G204 // Only for tests cmd.Stdin = bytes.NewReader(pem) cmd.Stdout = buf cmd.Stderr = buf diff --git a/go.mod b/go.mod index 0a73381..e44d322 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/Luzifer/go-dhparam -go 1.19 +go 1.22 diff --git a/validate.go b/validate.go index 652411d..9ee875f 100644 --- a/validate.go +++ b/validate.go @@ -1,18 +1,19 @@ package dhparam import ( - "math/big" - "errors" + "math/big" ) -const dhCheckPNotPrime = 0x01 -const dhCheckPNotSafePrime = 0x02 -const dhUnableToCheckGenerator = 0x04 -const dhNotSuitableGenerator = 0x08 -const dhCheckQNotPrime = 0x10 -const dhCheckInvalidQValue = 0x20 -const dhCheckInvalidJValue = 0x40 +const ( + dhCheckPNotPrime = 0x01 + dhCheckPNotSafePrime = 0x02 + dhUnableToCheckGenerator = 0x04 + dhNotSuitableGenerator = 0x08 + dhCheckQNotPrime = 0x10 + dhCheckInvalidQValue = 0x20 + dhCheckInvalidJValue = 0x40 +) // ErrAllParametersOK is defined to check whether the returned error from Check is indeed no error // For simplicity reasons it is defined as an error instead of an additional result parameter @@ -71,19 +72,20 @@ func (d DH) Check() ([]error, bool) { return result, ok } +//revive:disable-next-line:confusing-naming // Intended in this case as this is the real functionality func (d DH) check() int { var ret int // Check generator switch d.G { - case 2: + case 2: //nolint:mnd l := new(big.Int) - if l.Mod(d.P, big.NewInt(24)); l.Int64() != 11 { + if l.Mod(d.P, big.NewInt(24)); l.Int64() != 11 { //nolint:mnd ret |= dhNotSuitableGenerator } - case 5: + case 5: //nolint:mnd l := new(big.Int) - if l.Mod(d.P, big.NewInt(10)); l.Int64() != 3 && l.Int64() != 7 { + if l.Mod(d.P, big.NewInt(10)); l.Int64() != 3 && l.Int64() != 7 { //nolint:mnd ret |= dhNotSuitableGenerator } default: