Giter Club home page Giter Club logo

base62's Issues

Could you provide sample output?

Which base62 implementations is this compatible with?

Reference Strings

For reference, I believe this is what base62 encoding and decoding should look like:

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

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

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

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

Reference Implementation

I generated that using https://github.com/keybase/saltpack/encoding/basex, which seems to be correct and agree with other implementations, such as https://github.com/oconnor663/basex_gmp.

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, 255, 255, 255, 255},
		{255, 255, 255, 255, 0, 0, 0, 0},
	} {
		// Uses the GMP character set "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
		b62 := basex.Base62StdEncoding.EncodeToString(src)
		b64 := base64.RawURLEncoding.EncodeToString(src)

		fmt.Printf("Base64: %s (%d chars)\n", b64, len(b64))
		fmt.Printf("Base62: %s (%d chars)\n", b62, len(b62))
		fmt.Println("")
	}
}

Undefined behavior while decoding characters not in the base62 alphabet

When trying to decode something that contains invalid base62 characters, the behavior is undefined and not always the same:

  • with ascii characters, decoding works but the result is total garbage
  • with more complex utf-8 characters, an exception is thrown:
java.lang.ArrayIndexOutOfBoundsException: -16

	at io.seruco.encoding.base62.Base62.translate(Base62.java:84)

Code that triggered this case:

Base62.createInstance().decode("😱".getBytes());

That might be critical when the encoded string is user/external input.

I see plenty of possible solutions here, and not sure which one you prefer. If you pick one I am willing to make a PR.

  • Do as JDK Base64: throw IllegalArgumentException if the string is not valid according to the chosen alphabet
  • Implement a method to check if a string is valid according to the chosen alphabet
  • Publicly expose the alphabets to allow callers to make their own checks.

WDYT?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.