jxskiss / base62 Goto Github PK
View Code? Open in Web Editor NEWCompact and high performace implementation of base62 algorithm for Golang.
License: MIT License
Compact and high performace implementation of base62 algorithm for Golang.
License: MIT License
Line 222 in 9b9fb35
Wouldn't this be the same as return string(bytes)
and return []byte(str)
?
If I'm not mistaken the masks used are for the algorithm presuming it's reading the input string backwards?
Was trying to implement this in c and I'm still having trouble. I got curious if the algorithm can be "flipped" the other way around. Instead of reading what I'm assuming is 6 bits from the end, having it read 6 bits from the front. I think this changes the mask from (63 & 62 & 31 & 30) to (63 & 62 & 61 & 60)?
Compare the output from jxskiss/base62 to that from keys-pub/keys/encoding:
package main
import (
"fmt"
"github.com/jxskiss/base62"
"github.com/keys-pub/keys/encoding"
)
func main() {
id := []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}
fmt.Println(base62.EncodeToString(id[:]))
fmt.Println(encoding.EncodeBase62(id[:]))
}
Output:
fffffffffffffffffffffffffH
7n42DGM5Tflk9n8mt7Fhc7
I believe keys-pub/keys/encoding is correct and jxskiss/base62 is not.
For reference, I here's the output of this library (jxskiss) compared to saltpack, which seems to also be compatible with the GMP and GnuPG Base62 implementations, even when using the same alphabet:
enc := base62.NewEncoding("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
Raw : Hello, 世界 (13 bytes)
Base64 : SGVsbG8sIOS4lueVjA (18 chars)
jxskiss : CMvvMYBvWmoRinMP81 (18 chars)
saltpack: 1wJfrzvdbuFbL65vcS (18 chars)
Raw : Hello World (11 bytes)
Base64 : SGVsbG8gV29ybGQ (15 chars)
jxskiss : ancSlT58ln6RbX4 (15 chars)
saltpack: 73XpUgyMwkGr29M (15 chars)
Raw : [0] (1 bytes)
Base64 : AA (2 chars)
jxskiss : 00 (2 chars)
saltpack: 00 (2 chars)
Raw : [0 0 0 0 0 0 0 0 0 0 0 0] (12 bytes)
Base64 : AAAAAAAAAAAAAAAA (16 chars)
jxskiss : 0000000000000000 (16 chars)
saltpack: 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)
jxskiss : 00000000000000000000000000000000 (32 chars)
saltpack: 000000000000000000000000000000000 (33 chars)
Raw : [0 0 0 0 255 255 255 255] (8 bytes)
Base64 : AAAAAP____8 (11 chars)
jxskiss : VVVVVV300000 (12 chars)
saltpack: 000004gfFC3 (11 chars)
Raw : [255 255 255 255 0 0 0 0] (8 bytes)
Base64 : _____wAAAAA (11 chars)
jxskiss : 00000yVVVVV7 (12 chars)
saltpack: LygHZwPV2MC (11 chars)
As you can see, it's not just a matter of the output being reversed, but the character sequences are entirely different.
package main
import (
"encoding/base64"
"fmt"
"github.com/jxskiss/base62"
"github.com/keybase/saltpack/encoding/basex"
)
func main() {
enc := base62.NewEncoding("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
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 := enc.Encode(src)
b64 := base64.RawURLEncoding.EncodeToString(src)
sp62 := basex.Base62StdEncoding.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("jxskiss : %s (%d chars)\n", b62, len(b62))
fmt.Printf("saltpack: %s (%d chars)\n", sp62, len(sp62))
fmt.Println("")
}
}
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.