diff --git a/lib/config/snowflake.go b/lib/config/snowflake.go index 9bfec9c28..89bdcd339 100644 --- a/lib/config/snowflake.go +++ b/lib/config/snowflake.go @@ -3,7 +3,7 @@ package config import ( "fmt" - "github.com/artie-labs/transfer/lib/crypto" + "github.com/artie-labs/transfer/lib/cryptography" "github.com/artie-labs/transfer/lib/typing" "github.com/snowflakedb/gosnowflake" ) @@ -39,7 +39,7 @@ func (s Snowflake) ToConfig() (*gosnowflake.Config, error) { } if s.PathToPrivateKey != "" { - key, err := crypto.LoadRSAKey(s.PathToPrivateKey) + key, err := cryptography.LoadRSAKey(s.PathToPrivateKey) if err != nil { return nil, fmt.Errorf("failed to load private key: %w", err) } diff --git a/lib/crypto/rsa.go b/lib/cryptography/cryptography.go similarity index 74% rename from lib/crypto/rsa.go rename to lib/cryptography/cryptography.go index 45ab19b70..9a09becc5 100644 --- a/lib/crypto/rsa.go +++ b/lib/cryptography/cryptography.go @@ -1,8 +1,10 @@ -package crypto +package cryptography import ( "crypto/rsa" + "crypto/sha256" "crypto/x509" + "encoding/hex" "encoding/pem" "fmt" "os" @@ -10,6 +12,17 @@ import ( "github.com/artie-labs/transfer/lib/typing" ) +// HashValue - Hashes a value using SHA256 +func HashValue(value any) any { + if value == nil { + return nil + } + + hash := sha256.New() + hash.Write([]byte(fmt.Sprint(value))) + return hex.EncodeToString(hash.Sum(nil)) +} + func LoadRSAKey(filePath string) (*rsa.PrivateKey, error) { keyBytes, err := os.ReadFile(filePath) if err != nil { diff --git a/lib/cryptography/cryptography_test.go b/lib/cryptography/cryptography_test.go new file mode 100644 index 000000000..bb49b48d6 --- /dev/null +++ b/lib/cryptography/cryptography_test.go @@ -0,0 +1,34 @@ +package cryptography + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestHashValue(t *testing.T) { + { + // If we pass nil in, we should get nil out. + assert.Equal(t, nil, HashValue(nil)) + } + { + // Pass in an empty string + assert.Equal(t, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", HashValue("")) + } + { + // Pass in a string + assert.Equal(t, "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9", HashValue("hello world")) + } + { + // Value should be deterministic. + for range 50 { + assert.Equal(t, "b9a40320d82075681b2500e38160538e5e912bd8f49c03e87367fe82c1fa35d2", HashValue("dusty the mini aussie")) + } + } +} + +func BenchmarkHashValue(b *testing.B) { + for i := 0; i < b.N; i++ { + assert.Equal(b, "b9a40320d82075681b2500e38160538e5e912bd8f49c03e87367fe82c1fa35d2", HashValue("dusty the mini aussie")) + } +}