Files
gocryptotrader/common/crypto/crypto.go
Adrian Gallagher a5b638bfb7 GHA: Add additional checks for common issues (#1922)
* GHA, tests: Add additional checks for common issues

These checks include:
- Ensuring that all testify funcs use their formatted variants (e.g., `assert.Equalf(t, expected, actual)` instead of `assert.Equal(t, expected, actual)`).
- Replacing `%s` with %q
- Enforcing consistent usage of should/must wording for testify assert/require messages

* Add support for checking backticked string format specifiers and fix issues

* tests: Fix error comparisons

* tests: Replace errors.Is(err, nil) usage with testify and automate check

* refactor: Rename ExtractPort to ExtractPortOrDefault

* tests: Replace assert with require for error handling in multiple test files

* tests: Replace assert with require for error handling and improve assertions in data tests

* tests: Fix typo in assertion message for StreamVol test

* OKX: Fix GetOpenInterestAndVolumeStrike test with instrument selection and improved assertions

* OKX: Revert intentional error check

* Improve error message for expiry time check in GetOpenInterestAndVolumeStrike test
2025-05-28 12:26:51 +10:00

106 lines
2.3 KiB
Go

package crypto
import (
"crypto/hmac"
"crypto/md5" //nolint:gosec // Used for exchanges
"crypto/rand"
"crypto/sha1" //nolint:gosec // Used for exchanges
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"errors"
"hash"
"io"
)
// Const declarations for common.go operations
const (
HashSHA1 = iota
HashSHA256
HashSHA512
HashSHA512_384
HashMD5
)
// HexEncodeToString takes in a hexadecimal byte array and returns a string
func HexEncodeToString(input []byte) string {
return hex.EncodeToString(input)
}
// Base64Decode takes in a Base64 string and returns a byte array and an error
func Base64Decode(input string) ([]byte, error) {
result, err := base64.StdEncoding.DecodeString(input)
if err != nil {
return nil, err
}
return result, nil
}
// Base64Encode takes in a byte array then returns an encoded base64 string
func Base64Encode(input []byte) string {
return base64.StdEncoding.EncodeToString(input)
}
// GetRandomSalt returns a random salt
func GetRandomSalt(input []byte, saltLen int) ([]byte, error) {
if saltLen <= 0 {
return nil, errors.New("salt length is too small")
}
salt := make([]byte, saltLen)
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
return nil, err
}
var result []byte
if input != nil {
result = input
}
result = append(result, salt...)
return result, nil
}
// GetMD5 returns a MD5 hash of a byte array
func GetMD5(input []byte) ([]byte, error) {
m := md5.New() //nolint:gosec // hash function used by some exchanges
_, err := m.Write(input)
return m.Sum(nil), err
}
// GetSHA512 returns a SHA512 hash of a byte array
func GetSHA512(input []byte) ([]byte, error) {
sha := sha512.New()
_, err := sha.Write(input)
return sha.Sum(nil), err
}
// GetSHA256 returns a SHA256 hash of a byte array
func GetSHA256(input []byte) ([]byte, error) {
sha := sha256.New()
_, err := sha.Write(input)
return sha.Sum(nil), err
}
// GetHMAC returns a keyed-hash message authentication code using the desired
// hashtype
func GetHMAC(hashType int, input, key []byte) ([]byte, error) {
var hasher func() hash.Hash
switch hashType {
case HashSHA1:
hasher = sha1.New
case HashSHA256:
hasher = sha256.New
case HashSHA512:
hasher = sha512.New
case HashSHA512_384:
hasher = sha512.New384
case HashMD5:
hasher = md5.New
}
h := hmac.New(hasher, key)
_, err := h.Write(input)
return h.Sum(nil), err
}