SHA-1 Hash (Deprecated — Detection Only) Regex for Go
/^[a-f0-9]{40}$/iWhat this pattern does
This page provides a lightweight, single-purpose regular expression for matching sha-1 hash (deprecated — detection only), ported and verified for Go. In security-sensitive code, using an unverified regex can open the door to both false positives and denial-of-service attacks. The snippet below is ready to drop into your Go project — whether you're validating in a Gin handler, a gRPC service, or a command-line tool.
Go Implementation
// SHA-1 Hash (Deprecated — Detection Only)
// ReDoS-safe | RegexVault — Security > Password Formats
package validation
import "regexp"
var sha1HashDeprecatedDetectionOnlyRe = regexp.MustCompile(`^[a-f0-9]{40}$`)
func ValidateSha1HashDeprecatedDetectionOnly(s string) bool {
return sha1HashDeprecatedDetectionOnlyRe.MatchString(s)
}
// Example
// fmt.Println(ValidateSha1HashDeprecatedDetectionOnly("da39a3ee5e6b4b0d3255bfef95601890afd80709")) // trueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
da39a3ee5e6b4b0d3255bfef95601890afd80709 | da39a3ee5e6b4b0d3255bfef95601890afd8070 |
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d | da39a3ee5e6b4b0d3255bfef95601890afd807090 |
| — | ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ |
When to use this pattern
This pattern is drawn from the Security > Password Formats category and carries a ReDoS-safe certification. That matters for Go developers because Go's RE2 engine is inherently safe from catastrophic backtracking, but this pattern has been additionally verified for correctness. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Git historically used SHA-1 for commit hashing. GitHub and major git hosts have enabled SHA-256 object hashing for new repositories. SHA-1 used in TLS certificates was deprecated in 2017.
Technical Notes
SHA-1 is formally deprecated by NIST. Collision attacks were demonstrated in 2017 (SHAttered attack). Never use for digital signatures, certificates, or password hashing. Use SHA-256 or SHA-3 minimum for all new applications.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern