segmentio / ksuid

K-Sortable Globally Unique IDs
https://segment.com/blog/a-brief-history-of-the-uuid/
MIT License
4.82k stars 174 forks source link

Is there a regex pattern to validate the ksuid ? #33

Open lanpar opened 5 years ago

achille-roussel commented 5 years ago

KSUID textual representations are base62, so anything regex that checks for the 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz character set should work.

alexpop commented 3 years ago

A quick stab at it:

func IsValidKsuid(str string) bool {
    re := regexp.MustCompile("^[a-zA-Z0-9]{27}$")
    return re.MatchString(str)
}
ShawnMilo commented 3 years ago

The KSUID is essentially a hash, so other than @alexpop's length + alphabet check, I don't think you can "prove" it's a valid ksuid using something like a checksum. However, you can run it through ksuid -f inspect at the command line or using the library. This should prove it's actually a KSUID, not just any random 27-character string.