Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
feat: moved crypto to lib and removed hash function
=7c5a7442191a
diff --git a/backend-go/internal/crypto/hash/hash.go b/backend-go/internal/crypto/hash/hash.godeleted file mode 100644index 083ae92b62..0000000000--- a/backend-go/internal/crypto/hash/hash.go+++ /dev/null@@ -1,115 +0,0 @@-// Package hash provides cryptographic hash and HMAC functions used across-// the Infisical backend: SHA-256, SHA-1, HMAC-SHA-256, HMAC-SHA-1, and-// constant-time comparison.-package hash--import (- "crypto/hmac"- "crypto/sha1" //nolint:gosec // SHA-1 needed for certificate fingerprints and legacy compatibility- "crypto/sha256"- "crypto/sha512"- "crypto/subtle"- "encoding/hex"- "fmt"- "hash"-)--// Algorithm identifies a hash function.-type Algorithm string--const (- AlgoSHA1 Algorithm = "sha1"- AlgoSHA256 Algorithm = "sha256"- AlgoSHA384 Algorithm = "sha384"- AlgoSHA512 Algorithm = "sha512"-)--// SHA256 returns the raw SHA-256 digest (32 bytes).-func SHA256(data []byte) []byte {- h := sha256.Sum256(data)- return h[:]-}--// SHA256Hex returns the hex-encoded SHA-256 digest.-func SHA256Hex(data []byte) string {- return hex.EncodeToString(SHA256(data))-}--// SHA1 returns the raw SHA-1 digest (20 bytes).-// Use only where required for compatibility (e.g. certificate fingerprints).-func SHA1(data []byte) []byte {- h := sha1.Sum(data)- return h[:]-}--// SHA1Hex returns the hex-encoded SHA-1 digest.-func SHA1Hex(data []byte) string {- return hex.EncodeToString(SHA1(data))-}--// SHA384 returns the raw SHA-384 digest (48 bytes).-func SHA384(data []byte) []byte {- h := sha512.Sum384(data)- return h[:]-}--// SHA512 returns the raw SHA-512 digest (64 bytes).-func SHA512Bytes(data []byte) []byte {- h := sha512.Sum512(data)- return h[:]-}--// HMACSHA256 computes HMAC-SHA-256.-func HMACSHA256(key, data []byte) []byte {- return computeHMAC(sha256.New, key, data)-}--// HMACSHA1 computes HMAC-SHA-1.-// Use only where required for protocol compatibility.-func HMACSHA1(key, data []byte) []byte {- return computeHMAC(sha1.New, key, data) //nolint:gosec // SHA-1 required for protocol compatibility-}--// TimingSafeEqual performs constant-time comparison of two byte slices.-// Returns true only if a and b have the same length and identical contents.-func TimingSafeEqual(a, b []byte) bool {- return subtle.ConstantTimeCompare(a, b) == 1-}--func computeHMAC(newHash func() hash.Hash, key, data []byte) []byte {- mac := hmac.New(newHash, key)- mac.Write(data)- return mac.Sum(nil)-}--// NewHasher returns a hash.Hash for the given algorithm.-func NewHasher(algorithm Algorithm) (hash.Hash, error) {- switch algorithm {- case AlgoSHA256:- return sha256.New(), nil- case AlgoSHA384:- return sha512.New384(), nil- case AlgoSHA512:- return sha512.New(), nil- case AlgoSHA1:- return sha1.New(), nil //nolint:gosec // SHA-1 required for protocol compatibility- default:- return nil, fmt.Errorf("unsupported hash algorithm: %s", algorithm)- }-}--// NewHMAC returns an HMAC hash.Hash for the given algorithm and key.-func NewHMAC(algorithm Algorithm, key []byte) (hash.Hash, error) {- switch algorithm {- case AlgoSHA256:- return hmac.New(sha256.New, key), nil- case AlgoSHA384:- return hmac.New(sha512.New384, key), nil- case AlgoSHA512:- return hmac.New(sha512.New, key), nil- case AlgoSHA1:- return hmac.New(sha1.New, key), nil //nolint:gosec // SHA-1 required for protocol compatibility- default:- return nil, fmt.Errorf("unsupported HMAC algorithm: %s", algorithm)- }-}diff --git a/backend-go/internal/crypto/hash/hash_test.go b/backend-go/internal/crypto/hash/hash_test.godeleted file mode 100644index 604b62094b..0000000000--- a/backend-go/internal/crypto/hash/hash_test.go+++ /dev/null@@ -1,86 +0,0 @@-package hash_test--import (- "encoding/hex"- "testing"-- "github.com/infisical/api/internal/crypto/hash"-)--func TestSHA256Hex(t *testing.T) {- // echo -n "hello" | sha256sum- want := "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"- got := hash.SHA256Hex([]byte("hello"))- if got != want {- t.Fatalf("SHA256Hex(\"hello\") = %s, want %s", got, want)- }-}--func TestSHA1Hex(t *testing.T) {- // echo -n "hello" | sha1sum- want := "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"- got := hash.SHA1Hex([]byte("hello"))- if got != want {- t.Fatalf("SHA1Hex(\"hello\") = %s, want %s", got, want)- }-}--func TestHMACSHA256(t *testing.T) {- key := []byte("secret")- data := []byte("hello")- got := hex.EncodeToString(hash.HMACSHA256(key, data))- // python3 -c "import hmac,hashlib; print(hmac.new(b'secret',b'hello',hashlib.sha256).hexdigest())"- want := "88aab3ede8d3adf94d26ab90d3bafd4a2083070c3bcce9c014ee04a443847c0b"- if got != want {- t.Fatalf("HMACSHA256 = %s, want %s", got, want)- }-}--func TestTimingSafeEqual(t *testing.T) {- a := []byte("same")- b := []byte("same")- c := []byte("diff")- d := []byte("longer")-- if !hash.TimingSafeEqual(a, b) {- t.Fatal("expected equal slices to match")- }- if hash.TimingSafeEqual(a, c) {- t.Fatal("expected different slices to not match")- }- if hash.TimingSafeEqual(a, d) {- t.Fatal("expected different-length slices to not match")- }-}--func TestNewHasher(t *testing.T) {- for _, algo := range []hash.Algorithm{hash.AlgoSHA256, hash.AlgoSHA384, hash.AlgoSHA512, hash.AlgoSHA1} {- h, err := hash.NewHasher(algo)- if err != nil {- t.Fatalf("NewHasher(%q): %v", algo, err)- }- h.Write([]byte("test"))- if len(h.Sum(nil)) == 0 {- t.Fatalf("NewHasher(%q) produced empty digest", algo)- }- }-- _, err := hash.NewHasher("md5")- if err == nil {- t.Fatal("expected error for unsupported algorithm")- }-}--func TestNewHMAC(t *testing.T) {- key := []byte("key")- for _, algo := range []hash.Algorithm{hash.AlgoSHA256, hash.AlgoSHA384, hash.AlgoSHA512, hash.AlgoSHA1} {- h, err := hash.NewHMAC(algo, key)- if err != nil {- t.Fatalf("NewHMAC(%q): %v", algo, err)- }- h.Write([]byte("test"))- if len(h.Sum(nil)) == 0 {- t.Fatalf("NewHMAC(%q) produced empty digest", algo)- }- }-}diff --git a/backend-go/internal/crypto/cipher/cipher.go b/backend-go/internal/libs/crypto/cipher/cipher.gosimilarity index 100%rename from backend-go/internal/crypto/cipher/cipher.gorename to backend-go/internal/libs/crypto/cipher/cipher.godiff --git a/backend-go/internal/crypto/cipher/cipher_test.go b/backend-go/internal/libs/crypto/cipher/cipher_test.gosimilarity index 97%rename from backend-go/internal/crypto/cipher/cipher_test.gorename to backend-go/internal/libs/crypto/cipher/cipher_test.goindex 00315a5200..e3debc67e2 100644--- a/backend-go/internal/crypto/cipher/cipher_test.go+++ b/backend-go/internal/libs/crypto/cipher/cipher_test.go@@ -5,7 +5,7 @@ import ("crypto/rand""testing"- "github.com/infisical/api/internal/crypto/cipher"+ "github.com/infisical/api/internal/libs/crypto/cipher")func TestEncryptDecryptRoundTrip(t *testing.T) {diff --git a/backend-go/internal/crypto/sign/sign.go b/backend-go/internal/libs/crypto/sign/sign.gosimilarity index 100%rename from backend-go/internal/crypto/sign/sign.gorename to backend-go/internal/libs/crypto/sign/sign.godiff --git a/backend-go/internal/crypto/sign/sign_test.go b/backend-go/internal/libs/crypto/sign/sign_test.gosimilarity index 98%rename from backend-go/internal/crypto/sign/sign_test.gorename to backend-go/internal/libs/crypto/sign/sign_test.goindex cf831807bd..25ce687570 100644--- a/backend-go/internal/crypto/sign/sign_test.go+++ b/backend-go/internal/libs/crypto/sign/sign_test.go@@ -3,7 +3,7 @@ package sign_testimport ("testing"- "github.com/infisical/api/internal/crypto/sign"+ "github.com/infisical/api/internal/libs/crypto/sign")func TestRSAGenerateAndPublicKey(t *testing.T) {diff --git a/backend-go/internal/services/kms/kms.go b/backend-go/internal/services/kms/kms.goindex e7ce5768e0..4b423aabd6 100644--- a/backend-go/internal/services/kms/kms.go+++ b/backend-go/internal/services/kms/kms.go@@ -13,9 +13,9 @@ import ("github.com/jackc/pgx/v5""github.com/infisical/api/internal/config"- "github.com/infisical/api/internal/crypto/cipher""github.com/infisical/api/internal/database/pg""github.com/infisical/api/internal/database/pg/pglock"+ "github.com/infisical/api/internal/libs/crypto/cipher""github.com/infisical/api/internal/libs/errutil")