MCPcopy
hub / github.com/mudler/LocalAI / HashAPIKey

Function HashAPIKey

core/http/auth/apikeys.go:39–47  ·  view source on GitHub ↗

HashAPIKey returns the HMAC-SHA256 hex digest of the given plaintext key. If hmacSecret is empty, falls back to plain SHA-256 for backward compatibility.

(plaintext, hmacSecret string)

Source from the content-addressed store, hash-verified

37// HashAPIKey returns the HMAC-SHA256 hex digest of the given plaintext key.
38// If hmacSecret is empty, falls back to plain SHA-256 for backward compatibility.
39func HashAPIKey(plaintext, hmacSecret string) string {
40 if hmacSecret == "" {
41 h := sha256.Sum256([]byte(plaintext))
42 return hex.EncodeToString(h[:])
43 }
44 mac := hmac.New(sha256.New, []byte(hmacSecret))
45 mac.Write([]byte(plaintext))
46 return hex.EncodeToString(mac.Sum(nil))
47}
48
49// CreateAPIKey generates and stores a new API key for the given user.
50// Returns the plaintext key (shown once) and the database record.

Callers 11

middleware_test.goFile · 0.92
session_test.goFile · 0.92
apikeys_test.goFile · 0.92
RegisterAuthRoutesFunction · 0.92
CreateSessionFunction · 0.85
ValidateSessionFunction · 0.85
DeleteSessionFunction · 0.85
RotateSessionFunction · 0.85
GenerateAPIKeyFunction · 0.85
ValidateAPIKeyFunction · 0.85
ValidateInviteFunction · 0.85

Calls 1

WriteMethod · 0.45

Tested by

no test coverage detected