Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(repo): 合并重复的 SHA-256 十六进制摘要计算为 hexSHA256 (#402)

fx-kurumif26ce14503eb
diff --git a/internal/repo/bot_identities.go b/internal/repo/bot_identities.go
index c01bad4c..6e7d7c1f 100644
--- a/internal/repo/bot_identities.go
+++ b/internal/repo/bot_identities.go
@@ -4,7 +4,6 @@ import (
"agentflow/internal/domain"
"context"
"crypto/rand"
- "crypto/sha256"
"crypto/subtle"
"encoding/hex"
"errors"
@@ -19,10 +18,6 @@ var ErrBotBindingChallenge = errors.New("绑定请求已失效,请回到企业
var ErrBotBindingCode = errors.New("确认码无效、已过期或不属于当前发送者,请重新确认")
func botIdentityID(bot, sender string) string { return BotMessageID(bot, sender) }
-func botBindingHash(value string) string {
- sum := sha256.Sum256([]byte(value))
- return hex.EncodeToString(sum[:])
-}
func botBindingNonce(n int) (string, error) {
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
@@ -67,13 +62,13 @@ func (r *UsersRepo) NewBotBindingChallenge(bot, sender string, now time.Time) (s
if err := tx.Model(&domain.BotBindingChallenge{}).Where("binding_id = ? AND status IN ?", id, []string{"pending", "claimed"}).Update("status", "expired").Error; err != nil {
return err
}
- return tx.Create(&domain.BotBindingChallenge{ID: botBindingHash(token), BindingID: id, Status: "pending", ExpiresAt: now.Add(15 * time.Minute), CreatedAt: now}).Error
+ return tx.Create(&domain.BotBindingChallenge{ID: hexSHA256(token), BindingID: id, Status: "pending", ExpiresAt: now.Add(15 * time.Minute), CreatedAt: now}).Error
})
return token, err
}
func (r *UsersRepo) BotBindingChallengeStatus(token string, userID int64, now time.Time) (string, error) {
var out domain.BotBindingChallenge
- if err := r.DB.First(&out, "id = ?", botBindingHash(token)).Error; err != nil {
+ if err := r.DB.First(&out, "id = ?", hexSHA256(token)).Error; err != nil {
return "", ErrBotBindingChallenge
}
if !out.ExpiresAt.After(now) || out.Status == "expired" {
@@ -90,10 +85,10 @@ func (r *UsersRepo) ClaimBotBindingChallenge(token string, userID int64, now tim
}
// Derive a stable 80-bit code from the browser-only random nonce and account.
// Only its hash is stored; reopening a request does not rotate the code.
- code := botBindingHash(token + "\x00" + strconv.FormatInt(userID, 10))[:20]
+ code := hexSHA256(token + "\x00" + strconv.FormatInt(userID, 10))[:20]
err := r.DB.Transaction(func(tx *gorm.DB) error {
var challenge domain.BotBindingChallenge
- if err := tx.First(&challenge, "id = ?", botBindingHash(token)).Error; err != nil {
+ if err := tx.First(&challenge, "id = ?", hexSHA256(token)).Error; err != nil {
return ErrBotBindingChallenge
}
binding, err := r.botBindingRow(tx, challenge.BindingID)
@@ -112,7 +107,7 @@ func (r *UsersRepo) ClaimBotBindingChallenge(token string, userID int64, now tim
if challenge.Status == "claimed" {
return nil
}
- return tx.Model(&challenge).Updates(map[string]any{"user_id": userID, "code_hash": botBindingHash(code), "status": "claimed", "attempts": 0}).Error
+ return tx.Model(&challenge).Updates(map[string]any{"user_id": userID, "code_hash": hexSHA256(code), "status": "claimed", "attempts": 0}).Error
})
return strings.ToUpper(code), err
}
@@ -135,7 +130,7 @@ func (r *UsersRepo) ConfirmBotUserBinding(bot, sender, code string, now time.Tim
if !challenge.ExpiresAt.After(now) || challenge.Attempts >= 5 {
return ErrBotBindingCode
}
- if subtle.ConstantTimeCompare([]byte(challenge.CodeHash), []byte(botBindingHash(strings.ToLower(strings.TrimSpace(code))))) != 1 {
+ if subtle.ConstantTimeCompare([]byte(challenge.CodeHash), []byte(hexSHA256(strings.ToLower(strings.TrimSpace(code))))) != 1 {
denied = true
updates := map[string]any{"attempts": challenge.Attempts + 1}
if challenge.Attempts+1 >= 5 {
diff --git a/internal/repo/bot_identities_test.go b/internal/repo/bot_identities_test.go
index 6401cd1d..aad4a95f 100644
--- a/internal/repo/bot_identities_test.go
+++ b/internal/repo/bot_identities_test.go
@@ -42,7 +42,7 @@ func TestBotBindingRequiresBothIdentitiesAndRejectsReplay(t *testing.T) {
t.Fatal("cross-Bot confirmation")
}
var challenge domain.BotBindingChallenge
- s.DB.First(&challenge, "id = ?", botBindingHash(token))
+ s.DB.First(&challenge, "id = ?", hexSHA256(token))
if challenge.CodeHash == code || challenge.ID == token {
t.Fatal("plaintext challenge stored")
}
@@ -182,7 +182,7 @@ func TestBotBindingMySQLConcurrentConfirmation(t *testing.T) {
t.Fatal("MySQL duplicate confirmation", winners)
}
- expired := domain.BotBindingChallenge{ID: botBindingHash(bot + "-old"), BindingID: id, Status: "expired", ExpiresAt: now.Add(-25 * time.Hour)}
+ expired := domain.BotBindingChallenge{ID: hexSHA256(bot + "-old"), BindingID: id, Status: "expired", ExpiresAt: now.Add(-25 * time.Hour)}
if err := s.DB.Create(&expired).Error; err != nil {
t.Fatal(err)
}
diff --git a/internal/repo/bot_interactions.go b/internal/repo/bot_interactions.go
index 7cd245fc..ae153b50 100644
--- a/internal/repo/bot_interactions.go
+++ b/internal/repo/bot_interactions.go
@@ -3,7 +3,6 @@ package repo
import (
"agentflow/internal/domain"
"crypto/rand"
- "crypto/sha256"
"encoding/hex"
"errors"
"time"
@@ -15,12 +14,10 @@ import (
type BotInteractionsRepo struct{ *deps }
func BotConversationID(botID, kind, chatID string) string {
- sum := sha256.Sum256([]byte(botID + "\x00" + kind + "\x00" + chatID))
- return hex.EncodeToString(sum[:])
+ return hexSHA256(botID + "\x00" + kind + "\x00" + chatID)
}
func BotMessageID(botID, msgID string) string {
- sum := sha256.Sum256([]byte(botID + "\x00" + msgID))
- return hex.EncodeToString(sum[:])
+ return hexSHA256(botID + "\x00" + msgID)
}
func newBotSessionID() string {
var raw [16]byte
diff --git a/internal/repo/hash.go b/internal/repo/hash.go
new file mode 100644
index 00000000..2b1fead0
--- /dev/null
+++ b/internal/repo/hash.go
@@ -0,0 +1,12 @@
+package repo
+
+import (
+ "crypto/sha256"
+ "encoding/hex"
+)
+
+// hexSHA256 returns the lowercase hex SHA-256 digest of value.
+func hexSHA256(value string) string {
+ sum := sha256.Sum256([]byte(value))
+ return hex.EncodeToString(sum[:])
+}
diff --git a/internal/repo/hash_test.go b/internal/repo/hash_test.go
new file mode 100644
index 00000000..7fe8ad9d
--- /dev/null
+++ b/internal/repo/hash_test.go
@@ -0,0 +1,44 @@
+package repo
+
+import (
+ "crypto/sha256"
+ "encoding/hex"
+ "strings"
+ "testing"
+)
+
+func legacySHA256Hex(value string) string {
+ sum := sha256.Sum256([]byte(value))
+ return hex.EncodeToString(sum[:])
+}
+
+func TestHexSHA256KnownVectors(t *testing.T) {
+ cases := map[string]string{
+ "": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
+ "abc": "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
+ }
+ for in, want := range cases {
+ if got := hexSHA256(in); got != want {
+ t.Fatalf("hexSHA256(%q) = %s, want %s", in, got, want)
+ }
+ }
+}
+
+func TestHashCallersKeepLegacyOutput(t *testing.T) {
+ if got, want := BotConversationID("bot", "group", "chat"), legacySHA256Hex("bot\x00group\x00chat"); got != want {
+ t.Fatalf("BotConversationID = %s, want %s", got, want)
+ }
+ if got, want := BotMessageID("bot", "msg"), legacySHA256Hex("bot\x00msg"); got != want {
+ t.Fatalf("BotMessageID = %s, want %s", got, want)
+ }
+ if got, want := botIdentityID("bot", "sender"), legacySHA256Hex("bot\x00sender"); got != want {
+ t.Fatalf("botIdentityID = %s, want %s", got, want)
+ }
+ long := strings.Repeat("r", 80)
+ if got, want := integrationLockName(long), "agentflow:integrate:"+legacySHA256Hex(long)[:40]; got != want {
+ t.Fatalf("integrationLockName(long) = %s, want %s", got, want)
+ }
+ if got := integrationLockName("repo-1"); got != "agentflow:integrate:repo-1" {
+ t.Fatalf("integrationLockName(short) = %s", got)
+ }
+}
diff --git a/internal/repo/insights.go b/internal/repo/insights.go
index 594049a8..351bf2c8 100644
--- a/internal/repo/insights.go
+++ b/internal/repo/insights.go
@@ -1,13 +1,12 @@
package repo
import (
- "crypto/sha256"
- "encoding/hex"
"encoding/json"
"errors"
"time"
"agentflow/internal/domain"
+
"gorm.io/gorm"
)
@@ -40,10 +39,6 @@ type insightExecutionRow struct {
}
func (insightExecutionRow) TableName() string { return "insight_executions" }
-func insightHash(value string) string {
- sum := sha256.Sum256([]byte(value))
- return hex.EncodeToString(sum[:])
-}
func (r *InsightsRepo) CreateInsightSource(source domain.InsightSource, tokenHash string) error {
return r.DB.Create(&insightSourceRow{ID: source.ID, ProjectID: source.ProjectID, TokenHash: tokenHash, Payload: jsonBytes(source), ExpiresAt: source.ExpiresAt}).Error
@@ -94,7 +89,7 @@ func (r *InsightsRepo) IngestInsightExecution(source domain.InsightSource, execu
if err != nil {
return false, err
}
- row := insightExecutionRow{ID: insightHash(source.ID + "\x00" + execution.ID), ExecutionKey: insightHash(source.ID + "\x00" + execution.ExternalExecutionID), SourceID: source.ID, ProjectID: source.ProjectID, UserID: source.UserMappings[execution.ExternalUserID], Payload: payload, ContentHash: insightHash(string(payload)), FinishedAt: execution.FinishedAt, ReceivedAt: time.Now().UTC()}
+ row := insightExecutionRow{ID: hexSHA256(source.ID + "\x00" + execution.ID), ExecutionKey: hexSHA256(source.ID + "\x00" + execution.ExternalExecutionID), SourceID: source.ID, ProjectID: source.ProjectID, UserID: source.UserMappings[execution.ExternalUserID], Payload: payload, ContentHash: hexSHA256(string(payload)), FinishedAt: execution.FinishedAt, ReceivedAt: time.Now().UTC()}
// Serialize against revocation across instances. SQLite test DB has one connection.
duplicate := false
err = r.DB.Transaction(func(tx *gorm.DB) error {
diff --git a/internal/repo/integration_lock.go b/internal/repo/integration_lock.go
index c4b80098..534b8b58 100644
--- a/internal/repo/integration_lock.go
+++ b/internal/repo/integration_lock.go
@@ -2,9 +2,7 @@ package repo
import (
"context"
- "crypto/sha256"
"database/sql"
- "encoding/hex"
"fmt"
"strings"
"sync"
@@ -60,6 +58,5 @@ func integrationLockName(repositoryID string) string {
if len(name) <= 64 {
return name
}
- sum := sha256.Sum256([]byte(id))
- return "agentflow:integrate:" + hex.EncodeToString(sum[:])[:40]
+ return "agentflow:integrate:" + hexSHA256(id)[:40]
}
diff --git a/internal/repo/notifications.go b/internal/repo/notifications.go
index fe075d9a..b50d6738 100644
--- a/internal/repo/notifications.go
+++ b/internal/repo/notifications.go
@@ -2,8 +2,6 @@ package repo
import (
"agentflow/internal/domain"
- "crypto/sha256"
- "encoding/hex"
"encoding/json"
"fmt"
"gorm.io/gorm"
@@ -308,8 +306,7 @@ func (r *NotificationsRepo) enqueueNotification(tx *gorm.DB, key string, binding
if !bot.Enabled || bot.Target == nil || bot.ProjectID != item.ProjectID {
return nil
}
- sum := sha256.Sum256([]byte(key))
- item.ID = hex.EncodeToString(sum[:])
+ item.ID = hexSHA256(key)
item.TargetID = bot.Target.ID
item.TargetName = bot.Target.Name
item.TargetType = bot.Target.Type