speps / go-hashids

Go (golang) implementation of http://www.hashids.org
MIT License
1.32k stars 109 forks source link

Use uint64 instead of int64 #64

Open kzys opened 10 months ago

kzys commented 10 months ago

go-hashids provides EncodeInt64 but it actually rejects negative numbers in below. Does it make sense to use uint64, or provides a uint64 variant?

// EncodeInt64 hashes an array of int64 to a string containing at least MinLength characters taken from the Alphabet.
// Use DecodeInt64 using the same Alphabet and Salt to get back the array of int64.
func (h *HashID) EncodeInt64(numbers []int64) (string, error) {
    if len(numbers) == 0 {
        return "", errors.New("encoding empty array of numbers makes no sense")
    }
    for _, n := range numbers {
        if n < 0 {
            return "", errors.New("negative number not supported")
        }
    }

I'm using go-hashids along with hashids' Ruby implementation. In some cases, go-hashids rejects inputs that exceed MaxInt, while the Ruby implementation can handle it because Ruby's Integer can be larger than Go's MaxInt.

I made a fork to workaround the issue and happy to upstream something like this.

https://github.com/kzys/go-hashids/