therootcompany / base62.js

Like base64, but base62. Converts back and forth between base62 strings and (byte) arrays.
https://base62.js.org
MIT License
5 stars 0 forks source link

Base62 Implementations that do and don't agree #1

Open coolaj86 opened 2 years ago

coolaj86 commented 2 years ago

It seems that the algorithms associated with "base_x", GMP, GnuPG, and Bitcoin Base58 all seem to agree: \ (at least in simple tests of a few dozen bytes or less)

Disagree

Unknown

coolaj86 commented 2 years ago

Reference Base62 Strings

Raw   : Hello, 世界 (13 bytes)
Base64: SGVsbG8sIOS4lueVjA (18 chars)
Base62: 1wJfrzvdbuFbL65vcS (18 chars)

Raw   : Hello World (11 bytes)
Base64: SGVsbG8gV29ybGQ (15 chars)
Base62: 73XpUgyMwkGr29M (15 chars)

Raw   : [0] (1 bytes)
Base64: AA (2 chars)
Base62: 00 (2 chars)

Raw   : [0 0 0 0 0 0 0 0 0 0 0 0] (12 bytes)
Base64: AAAAAAAAAAAAAAAA (16 chars)
Base62: 00000000000000000 (17 chars)

Raw   : [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] (24 bytes)
Base64: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA (32 chars)
Base62: 000000000000000000000000000000000 (33 chars)

Raw   : [0 0 0 0 255 255 255 255] (8 bytes)
Base64: AAAAAP____8 (11 chars)
Base62: 000004gfFC3 (11 chars)

Raw   : [255 255 255 255 0 0 0 0] (8 bytes)
Base64: _____wAAAAA (11 chars)
Base62: LygHZwPV2MC (11 chars)

Test Code Reference

package main

import (
    "encoding/base64"
    "fmt"

    "github.com/keybase/saltpack/encoding/basex"
)

func main() {
    for _, src := range [][]byte{
        []byte("Hello, 世界"),
        []byte("Hello World"),
        {0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 255, 255, 255, 255},
        {255, 255, 255, 255, 0, 0, 0, 0},
    } {
        b62 := basex.Base62StdEncoding.EncodeToString(src)
        b64 := base64.RawURLEncoding.EncodeToString(src)

        if src[0] == 0x0 || src[1] == 255 {
            fmt.Printf("Raw   : %v (%d bytes)\n", src, len(src))
        } else {
            fmt.Printf("Raw   : %v (%d bytes)\n", string(src), len(src))
        }
        fmt.Printf("Base64: %s (%d chars)\n", b64, len(b64))
        fmt.Printf("Base62: %s (%d chars)\n", b62, len(b62))
        fmt.Println("")
    }
}