mirror of
https://github.com/waku-org/go-multiaddr.git
synced 2025-02-23 03:28:12 +00:00
don't use a function to guarantee correct padding
This commit is contained in:
parent
477514c812
commit
84811f919b
@ -252,30 +252,16 @@ var TranscoderGarlic32 = NewTranscoderFromFunctions(garlic32StB, garlic32BtS, ga
|
|||||||
var garlicBase32Encoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567")
|
var garlicBase32Encoding = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567")
|
||||||
|
|
||||||
func garlic32StB(s string) ([]byte, error) {
|
func garlic32StB(s string) ([]byte, error) {
|
||||||
//s = strings.Replace(s, ".b32.i2p", "", -1)
|
|
||||||
// garlic address without the ".b32.i2p" substring
|
|
||||||
|
|
||||||
// an i2p base32 address with a length of greater than 55 characters is
|
// an i2p base32 address with a length of greater than 55 characters is
|
||||||
// using an Encrypted Leaseset v2.
|
// using an Encrypted Leaseset v2. all other base32 addresses will always be
|
||||||
if len(s) < 55 {
|
// exactly 52 characters
|
||||||
if len(s) != 52 {
|
if len(s) < 55 && len(s) != 52 {
|
||||||
// all other base32 addresses will always be exactly 52 characters
|
return nil, fmt.Errorf("failed to parse garlic addr: %s not a i2p base32 address. len: %d", s, len(s))
|
||||||
return nil, fmt.Errorf("failed to parse garlic addr: %s not a i2p base32 address. len: %d", s, len(s))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//compute the length to pad the address to, usually 56 or 64
|
for len(s)%8 != 0 {
|
||||||
padout := func(s string) string {
|
s += "="
|
||||||
if len(s)%8 == 0 {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
x := int((len(s)/8)+1) * 8
|
|
||||||
for len(s) < x {
|
|
||||||
s += "="
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
}
|
||||||
|
garlicHostBytes, err := garlicBase32Encoding.DecodeString(s)
|
||||||
garlicHostBytes, err := garlicBase32Encoding.DecodeString(padout(s))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to decode base32 garlic addr: %s, err: %v len: %v", s, err, len(s))
|
return nil, fmt.Errorf("failed to decode base32 garlic addr: %s, err: %v len: %v", s, err, len(s))
|
||||||
}
|
}
|
||||||
@ -291,12 +277,9 @@ func garlic32BtS(b []byte) (string, error) {
|
|||||||
|
|
||||||
func garlic32Validate(b []byte) error {
|
func garlic32Validate(b []byte) error {
|
||||||
// an i2p base64 for an Encrypted Leaseset v2 will be at least 35 bytes
|
// an i2p base64 for an Encrypted Leaseset v2 will be at least 35 bytes
|
||||||
// long
|
// long other than that, they will be exactly 32 bytes
|
||||||
if len(b) < 35 {
|
if len(b) < 35 && len(b) != 32 {
|
||||||
// other than that, they will be exactly 32 bytes
|
return fmt.Errorf("failed to validate garlic addr: %s not an i2p base32 address. len: %d\n", b, len(b))
|
||||||
if len(b) != 32 {
|
|
||||||
return fmt.Errorf("failed to validate garlic addr: %s not an i2p base32 address. len: %d\n", b, len(b))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user