Skip to content

Commit 056263f

Browse files
author
idk
committed
Start attempting to support base32 for Encrypted Leaseset Version 2, aka base33. Not likely to work yet.
1 parent ce21123 commit 056263f

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

transcoders.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func onion3BtS(b []byte) (string, error) {
212212
return str, nil
213213
}
214214

215-
var TranscoderGarlic64 = NewTranscoderFromFunctions(garlic64StB, garlic64BtS, garlicValidate)
215+
var TranscoderGarlic64 = NewTranscoderFromFunctions(garlic64StB, garlic64BtS, garlic64Validate)
216216

217217
// i2p uses an alternate character set for base64 addresses. This returns an appropriate encoder.
218218
var garlicBase64Encoding = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-~")
@@ -238,13 +238,50 @@ func garlic64BtS(b []byte) (string, error) {
238238
return addr, nil
239239
}
240240

241-
func garlicValidate(b []byte) error {
241+
func garlic64Validate(b []byte) error {
242242
if len(b) < 386 {
243243
return fmt.Errorf("failed to validate garlic addr: %s not an i2p base64 address. len: %d\n", b, len(b))
244244
}
245245
return nil
246246
}
247247

248+
var TranscoderGarlic32 = NewTranscoderFromFunctions(garlic32StB, garlic32BtS, garlic32Validate)
249+
250+
var garlicBase32Encoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567")
251+
252+
func garlic32StB(s string) ([]byte, error) {
253+
// garlic address without the ".b32.i2p" substring, with padding
254+
if len(s) != 52 || len(s) < 55 || len(s) > 63 {
255+
return nil, fmt.Errorf("failed to parse garlic addr: %s not a i2p base32 address. len: %d", s, len(s))
256+
}
257+
garlicHostBytes := make([]byte, 37)
258+
_, err := garlicBase32Encoding.Decode(garlicHostBytes, []byte(s))
259+
if err != nil {
260+
return nil, fmt.Errorf("failed to decode base32 garlic addr: %s %s", s, err)
261+
}
262+
bytes := []byte{}
263+
bytes = append(bytes, garlicHostBytes...)
264+
265+
return bytes, nil
266+
267+
}
268+
269+
func garlic32BtS(b []byte) (string, error) {
270+
if len(b) < 33 || len(b) > 37 {
271+
return "", fmt.Errorf("failed to validate garlic addr: %s not an i2p base64 address. len: %d\n", b, len(b))
272+
}
273+
addr := strings.Replace(strings.ToLower(garlicBase32Encoding.EncodeToString(b)), "=", "", -1)
274+
return addr, nil
275+
}
276+
277+
func garlic32Validate(b []byte) error {
278+
if len(b) < 33 || len(b) > 37 {
279+
return fmt.Errorf("failed to validate garlic addr: %s not an i2p base64 address. len: %d\n", b, len(b))
280+
}
281+
return nil
282+
}
283+
284+
248285
var TranscoderP2P = NewTranscoderFromFunctions(p2pStB, p2pBtS, p2pVal)
249286

250287
func p2pStB(s string) ([]byte, error) {

0 commit comments

Comments
 (0)