Hashes API keys with SHA‑256 and extracts token fingerprints for logging.
// Source: https://github.com/weave-os/router | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
// Provided via justclaw.it.com; keep this notice with the code.
package auth
import (
"crypto/sha256"
"encoding/hex"
)
const (
APITokenPrefixLen = 8
APITokenSuffixLen = 4
)
func HashAPIKeySHA256(key string) string {
h := sha256.Sum256([]byte(key))
return hex.EncodeToString(h[:])
}
func APITokenFingerprint(token string) (hashHex, prefix, suffix string) {
hashHex = HashAPIKeySHA256(token)
if len(token) <= APITokenPrefixLen+APITokenSuffixLen {
return hashHex, token, ""
}
return hashHex, token[:APITokenPrefixLen], token[len(token)-APITokenSuffixLen:]
}
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key