v0.0.296 add csid.generateIDFromSeed
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m4s

This commit is contained in:
2023-11-01 00:29:58 +01:00
parent 2fbd5cf965
commit f8c0c0afa0
2 changed files with 23 additions and 2 deletions

View File

@@ -65,6 +65,27 @@ func generateID(prefix string) string {
return prefix + k + checkstr
}
func generateIDFromSeed(prefix string, seed string) string {
h := sha256.New()
iddata := ""
for len(iddata) < idlen-len(prefix)-checklen {
h.Write([]byte(seed))
bs := h.Sum(nil)
iddata += langext.NewAnyBaseConverter(idCharset).Encode(bs)
}
checksum := 0
for i := 0; i < idlen-len(prefix)-checklen; i++ {
ichr := int(iddata[i])
checksum = (checksum + charSetReverseMap[ichr]) % (idCharsetLen)
}
checkstr := string(idCharset[checksum%idCharsetLen])
return prefix + iddata[:(idlen-len(prefix)-checklen)] + checkstr
}
func validateID(prefix string, value string) error {
if len(value) != idlen {
return exerr.New(exerr.TypeInvalidCSID, "id has the wrong length").Str("value", value).Build()