@@ -201,18 +201,43 @@ func (h *HashID) EncodeInt64(numbers []int64) (string, error) {
201
201
// It is symmetric with Encode if the Alphabet and Salt are the same ones which were used to hash.
202
202
// MinLength has no effect on Decode.
203
203
func (h * HashID ) Decode (hash string ) []int {
204
- result64 := h .DecodeInt64 (hash )
204
+ result , err := h .DecodeWithError (hash )
205
+ if err != nil {
206
+ panic (err )
207
+ }
208
+ return result
209
+ }
210
+
211
+ // Decode unhashes the string passed to an array of int.
212
+ // It is symmetric with Encode if the Alphabet and Salt are the same ones which were used to hash.
213
+ // MinLength has no effect on Decode.
214
+ func (h * HashID ) DecodeWithError (hash string ) ([]int , error ) {
215
+ result64 , err := h .DecodeInt64WithError (hash )
216
+ if err != nil {
217
+ return nil , err
218
+ }
205
219
result := make ([]int , 0 , len (result64 ))
206
220
for _ , id := range result64 {
207
221
result = append (result , int (id ))
208
222
}
209
- return result
223
+ return result , nil
210
224
}
211
225
212
226
// DecodeInt64 unhashes the string passed to an array of int64.
213
227
// It is symmetric with EncodeInt64 if the Alphabet and Salt are the same ones which were used to hash.
214
228
// MinLength has no effect on DecodeInt64.
215
229
func (h * HashID ) DecodeInt64 (hash string ) []int64 {
230
+ result , err := h .DecodeInt64WithError (hash )
231
+ if err != nil {
232
+ panic (err )
233
+ }
234
+ return result
235
+ }
236
+
237
+ // DecodeInt64 unhashes the string passed to an array of int64.
238
+ // It is symmetric with EncodeInt64 if the Alphabet and Salt are the same ones which were used to hash.
239
+ // MinLength has no effect on DecodeInt64.
240
+ func (h * HashID ) DecodeInt64WithError (hash string ) ([]int64 , error ) {
216
241
hashes := splitRunes ([]rune (hash ), h .guards )
217
242
hashIndex := 0
218
243
if len (hashes ) == 2 || len (hashes ) == 3 {
@@ -230,11 +255,15 @@ func (h *HashID) DecodeInt64(hash string) []int64 {
230
255
for _ , subHash := range hashes {
231
256
buffer := append ([]rune {lottery }, append (h .salt , alphabet ... )... )
232
257
alphabet = consistentShuffle (alphabet , buffer [:len (alphabet )])
233
- result = append (result , unhash (subHash , alphabet ))
258
+ number , err := unhash (subHash , alphabet )
259
+ if err != nil {
260
+ return nil , err
261
+ }
262
+ result = append (result , number )
234
263
}
235
264
}
236
265
237
- return result
266
+ return result , nil
238
267
}
239
268
240
269
func splitRunes (input , seps []rune ) [][]rune {
@@ -278,7 +307,7 @@ func hash(input int64, alphabet []rune) []rune {
278
307
return reversed
279
308
}
280
309
281
- func unhash (input , alphabet []rune ) int64 {
310
+ func unhash (input , alphabet []rune ) ( int64 , error ) {
282
311
result := int64 (0 )
283
312
for i , inputRune := range input {
284
313
alphabetPos := - 1
@@ -289,12 +318,12 @@ func unhash(input, alphabet []rune) int64 {
289
318
}
290
319
}
291
320
if alphabetPos == - 1 {
292
- panic ( "should not happen, alphabet used for hash was different" )
321
+ return 0 , errors . New ( " alphabet used for hash was different" )
293
322
}
294
323
295
324
result += int64 (alphabetPos ) * int64 (math .Pow (float64 (len (alphabet )), float64 (len (input )- i - 1 )))
296
325
}
297
- return result
326
+ return result , nil
298
327
}
299
328
300
329
func consistentShuffle (alphabet , salt []rune ) []rune {
0 commit comments