Code Monkey home page Code Monkey logo

base62's People

Contributors

fufuok avatar jxskiss avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

base62's Issues

Question to make sense of the algorithm

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)?

Incorrect Encoding of `ffffffffffffffffffffffffffffffff`

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.

Does not agree with Saltpack / GMP

Reference Strings

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.

Test Output Reference

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("")
	}
}

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.