From 63e66378eea6f971413492c2c4000fd768bffed3 Mon Sep 17 00:00:00 2001 From: yanghao05 Date: Tue, 30 May 2023 11:19:09 +0800 Subject: [PATCH] add hasher --- providers/hashids/config.go | 1 + providers/hashids/hashids.go | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/providers/hashids/config.go b/providers/hashids/config.go index 8c9f713..48d05f9 100644 --- a/providers/hashids/config.go +++ b/providers/hashids/config.go @@ -18,6 +18,7 @@ func DefaultProvider() container.ProviderContainer { } type Config struct { + Alphabet string Salt string MinLength uint } diff --git a/providers/hashids/hashids.go b/providers/hashids/hashids.go index 582bbda..5f257ed 100644 --- a/providers/hashids/hashids.go +++ b/providers/hashids/hashids.go @@ -15,9 +15,21 @@ func Provide(opts ...opt.Option) error { log.Fatal(err) } return container.Container.Provide(func() (*hashids.HashID, error) { - return hashids.NewWithData(&hashids.HashIDData{ - MinLength: int(config.MinLength), - Salt: config.Salt, - }) + data := hashids.NewData() + data.MinLength = int(config.MinLength) + if data.MinLength == 0 { + data.MinLength = 5 + } + + data.Salt = config.Salt + if data.Salt == "" { + data.Salt = "default-salt-key" + } + + if config.Alphabet != "" { + data.Alphabet = config.Alphabet + } + + return hashids.NewWithData(data) }, o.DiOptions()...) }