seruco / base62 Goto Github PK
View Code? Open in Web Editor NEWBase62 Encoder/Decoder for Java
License: MIT License
Base62 Encoder/Decoder for Java
License: MIT License
Which base62 implementations is this compatible with?
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)
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("")
}
}
When trying to decode something that contains invalid base62 characters, the behavior is undefined and not always the same:
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.
WDYT?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.