Skip to content

Commit 84811f9

Browse files
author
idk
committed
don't use a function to guarantee correct padding
1 parent 477514c commit 84811f9

File tree

1 file changed

+12
-29
lines changed

1 file changed

+12
-29
lines changed

transcoders.go

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -252,30 +252,16 @@ var TranscoderGarlic32 = NewTranscoderFromFunctions(garlic32StB, garlic32BtS, ga
252252
var garlicBase32Encoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567")
253253

254254
func garlic32StB(s string) ([]byte, error) {
255-
//s = strings.Replace(s, ".b32.i2p", "", -1)
256-
// garlic address without the ".b32.i2p" substring
257-
258255
// an i2p base32 address with a length of greater than 55 characters is
259-
// using an Encrypted Leaseset v2.
260-
if len(s) < 55 {
261-
if len(s) != 52 {
262-
// all other base32 addresses will always be exactly 52 characters
263-
return nil, fmt.Errorf("failed to parse garlic addr: %s not a i2p base32 address. len: %d", s, len(s))
264-
}
265-
}
266-
//compute the length to pad the address to, usually 56 or 64
267-
padout := func(s string) string {
268-
if len(s)%8 == 0 {
269-
return s
270-
}
271-
x := int((len(s)/8)+1) * 8
272-
for len(s) < x {
273-
s += "="
274-
}
275-
return s
276-
}
277-
278-
garlicHostBytes, err := garlicBase32Encoding.DecodeString(padout(s))
256+
// using an Encrypted Leaseset v2. all other base32 addresses will always be
257+
// exactly 52 characters
258+
if len(s) < 55 && len(s) != 52 {
259+
return nil, fmt.Errorf("failed to parse garlic addr: %s not a i2p base32 address. len: %d", s, len(s))
260+
}
261+
for len(s)%8 != 0 {
262+
s += "="
263+
}
264+
garlicHostBytes, err := garlicBase32Encoding.DecodeString(s)
279265
if err != nil {
280266
return nil, fmt.Errorf("failed to decode base32 garlic addr: %s, err: %v len: %v", s, err, len(s))
281267
}
@@ -291,12 +277,9 @@ func garlic32BtS(b []byte) (string, error) {
291277

292278
func garlic32Validate(b []byte) error {
293279
// an i2p base64 for an Encrypted Leaseset v2 will be at least 35 bytes
294-
// long
295-
if len(b) < 35 {
296-
// other than that, they will be exactly 32 bytes
297-
if len(b) != 32 {
298-
return fmt.Errorf("failed to validate garlic addr: %s not an i2p base32 address. len: %d\n", b, len(b))
299-
}
280+
// long other than that, they will be exactly 32 bytes
281+
if len(b) < 35 && len(b) != 32 {
282+
return fmt.Errorf("failed to validate garlic addr: %s not an i2p base32 address. len: %d\n", b, len(b))
300283
}
301284
return nil
302285
}

0 commit comments

Comments
 (0)