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:
Mike Schwörer 2023-11-01 00:29:58 +01:00
parent 2fbd5cf965
commit f8c0c0afa0
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 23 additions and 2 deletions

View File

@ -65,6 +65,27 @@ func generateID(prefix string) string {
return prefix + k + checkstr 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 { func validateID(prefix string, value string) error {
if len(value) != idlen { if len(value) != idlen {
return exerr.New(exerr.TypeInvalidCSID, "id has the wrong length").Str("value", value).Build() return exerr.New(exerr.TypeInvalidCSID, "id has the wrong length").Str("value", value).Build()

View File

@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.295" const GoextVersion = "0.0.296"
const GoextVersionTimestamp = "2023-11-01T00:23:17+0100" const GoextVersionTimestamp = "2023-11-01T00:29:58+0100"