Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode message directly into permalink? #273

Open
ferdnyc opened this issue May 27, 2024 · 0 comments
Open

Encode message directly into permalink? #273

ferdnyc opened this issue May 27, 2024 · 0 comments

Comments

@ferdnyc
Copy link

ferdnyc commented May 27, 2024

With the existing hash-function based permalinking, there's tension between the desire to deterministically create the same string from the same hash each time, vs. having the hash select only a template string, with the randomized elements being chosen anew each time it's loaded. (See #109, for example.)

But it strikes me that the messages in question are small enough, that it might make sense to just encode the message itself into the permalink. That way, it can be recreated without any dependency on the hash function used, or the lookup implementation.

Python's standard library already has the base64 module, with its urlsafe_b64encode and urlsafe_b64decode functions. Here's how a typical message looks when encoded to URLsafe base64:

>>> from base64 import urlsafe_b64encode, urlsafe_b64decode
>>> msg = "Put everything in its right place"
>>> coded = urlsafe_b64encode(msg.encode("utf-8"))
>>> coded
b'UHV0IGV2ZXJ5dGhpbmcgaW4gaXRzIHJpZ2h0IHBsYWNl'

The message can easily be reversed back into its text form:

>>> urlsafe_b64decode(coded).decode("utf-8")
'Put everything in its right place'

The longest message in the data, currently, is this one at 263 characters:

I'll mention this again, if you're git-blaming this, don't come slap me personally. This code straight ported from another project and we WILL refactor this in the future. This is a temporary solution. OK I guess you can slap me for porting this as is, but still.

Encoded to base64, it becomes 352 characters, which is long but not unreasonable as a URL length. (Have you seen Google Maps URLs?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant