Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linter fixes for the crypto and encoding k6 modules #3460

Merged
merged 5 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 29 additions & 34 deletions js/modules/k6/crypto/crypto.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Package crypto provides common hashing function for the k6
package crypto

import (
"crypto/hmac"
"crypto/md5"
"crypto/md5" // #nosec G501 // MD5 is weak, but we need it for compatibility
"crypto/rand"
"crypto/sha1"
"crypto/sha1" // #nosec G505 // SHA1 is weak, but we need it for compatibility
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
Expand All @@ -13,8 +14,8 @@ import (
"fmt"
"hash"

"golang.org/x/crypto/md4"
"golang.org/x/crypto/ripemd160"
"golang.org/x/crypto/md4" //nolint:staticcheck // #nosec G501 // MD4 is weak, but we need it for compatibility
"golang.org/x/crypto/ripemd160" // no lint:staticcheck // #nosec G501 // RIPEMD160 is weak, but we need it for compatibility

"github.com/dop251/goja"

Expand All @@ -29,7 +30,8 @@ type (

// Crypto represents an instance of the crypto module.
Crypto struct {
vu modules.VU
randReader func(b []byte) (n int, err error)
vu modules.VU
}
)

Expand All @@ -46,7 +48,7 @@ func New() *RootModule {
// NewModuleInstance implements the modules.Module interface to return
// a new instance for each VU.
func (*RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
return &Crypto{vu: vu}
return &Crypto{vu: vu, randReader: rand.Read}
}

// Exports returns the exports of the execution module.
Expand Down Expand Up @@ -77,7 +79,7 @@ func (c *Crypto) randomBytes(size int) (*goja.ArrayBuffer, error) {
return nil, errors.New("invalid size")
}
bytes := make([]byte, size)
_, err := rand.Read(bytes)
_, err := c.randReader(bytes)
if err != nil {
return nil, err
}
Expand All @@ -87,65 +89,47 @@ func (c *Crypto) randomBytes(size int) (*goja.ArrayBuffer, error) {

// md4 returns the MD4 hash of input in the given encoding.
func (c *Crypto) md4(input interface{}, outputEncoding string) (interface{}, error) {
hasher := c.createHash("md4")
hasher.Update(input)
return hasher.Digest(outputEncoding)
return c.buildInputsDigest("md4", input, outputEncoding)
}

// md5 returns the MD5 hash of input in the given encoding.
func (c *Crypto) md5(input interface{}, outputEncoding string) (interface{}, error) {
hasher := c.createHash("md5")
hasher.Update(input)
return hasher.Digest(outputEncoding)
return c.buildInputsDigest("md5", input, outputEncoding)
}

// sha1 returns the SHA1 hash of input in the given encoding.
func (c *Crypto) sha1(input interface{}, outputEncoding string) (interface{}, error) {
hasher := c.createHash("sha1")
hasher.Update(input)
return hasher.Digest(outputEncoding)
return c.buildInputsDigest("sha1", input, outputEncoding)
}

// sha256 returns the SHA256 hash of input in the given encoding.
func (c *Crypto) sha256(input interface{}, outputEncoding string) (interface{}, error) {
hasher := c.createHash("sha256")
hasher.Update(input)
return hasher.Digest(outputEncoding)
return c.buildInputsDigest("sha256", input, outputEncoding)
}

// sha384 returns the SHA384 hash of input in the given encoding.
func (c *Crypto) sha384(input interface{}, outputEncoding string) (interface{}, error) {
hasher := c.createHash("sha384")
hasher.Update(input)
return hasher.Digest(outputEncoding)
return c.buildInputsDigest("sha384", input, outputEncoding)
}

// sha512 returns the SHA512 hash of input in the given encoding.
func (c *Crypto) sha512(input interface{}, outputEncoding string) (interface{}, error) {
hasher := c.createHash("sha512")
hasher.Update(input)
return hasher.Digest(outputEncoding)
return c.buildInputsDigest("sha512", input, outputEncoding)
}

// sha512_224 returns the SHA512/224 hash of input in the given encoding.
func (c *Crypto) sha512_224(input interface{}, outputEncoding string) (interface{}, error) {
hasher := c.createHash("sha512_224")
hasher.Update(input)
return hasher.Digest(outputEncoding)
return c.buildInputsDigest("sha512_224", input, outputEncoding)
}

// shA512_256 returns the SHA512/256 hash of input in the given encoding.
func (c *Crypto) sha512_256(input interface{}, outputEncoding string) (interface{}, error) {
hasher := c.createHash("sha512_256")
hasher.Update(input)
return hasher.Digest(outputEncoding)
return c.buildInputsDigest("sha512_256", input, outputEncoding)
}

// ripemd160 returns the RIPEMD160 hash of input in the given encoding.
func (c *Crypto) ripemd160(input interface{}, outputEncoding string) (interface{}, error) {
hasher := c.createHash("ripemd160")
hasher.Update(input)
return hasher.Digest(outputEncoding)
return c.buildInputsDigest("ripemd160", input, outputEncoding)
}

// createHash returns a Hasher instance that uses the given algorithm.
Expand All @@ -157,6 +141,17 @@ func (c *Crypto) createHash(algorithm string) *Hasher {
}
}

// buildInputsDigest implements basic digest calculation for given algorithm and input/output
func (c *Crypto) buildInputsDigest(alg string, input interface{}, outputEncoding string) (interface{}, error) {
hasher := c.createHash(alg)

if err := hasher.Update(input); err != nil {
return nil, fmt.Errorf("%s failed: %w", alg, err)
}

return hasher.Digest(outputEncoding)
}

// hexEncode returns a string with the hex representation of the provided byte
// array or ArrayBuffer.
func (c *Crypto) hexEncode(data interface{}) (string, error) {
Expand Down