bcrypt Password Hash in GO
Match bcrypt password hashes in their canonical $2a$/$2b$/$2y$ prefixed format.
Try it in the GO tester →Pattern
regexGO
\$2[abxy]?\$\d{2}\$[./A-Za-z0-9]{53} (flags: g)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\$2[abxy]?\$\d{2}\$[./A-Za-z0-9]{53}`)
input := `$2a$12$R9h/cIPz0gi.URNNX3kh2OPST9/PgBkqquzi.Ss7KIUgO2t0jWMUW`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
How the pattern works
Version prefix $2 with optional suffix letter, cost parameter (two digits), salt + hash encoded in bcrypt's base64 alphabet for a fixed 53 trailing characters.
Examples
Input
$2a$12$R9h/cIPz0gi.URNNX3kh2OPST9/PgBkqquzi.Ss7KIUgO2t0jWMUWMatches
$2a$12$R9h/cIPz0gi.URNNX3kh2OPST9/PgBkqquzi.Ss7KIUgO2t0jWMUW