Use base64.RawURLEncoding instead of defining base64Alphabet #1

Closed
opened 2019-09-02 13:22:16 +02:00 by mrngm · 4 comments
Collaborator

In https://gitea.hashru.nl/dsprenkels/rushlink/src/branch/master/handlers.go#L56 we can use base64.RawURLEncoding provided in the standard library.

E.g. https://gitea.hashru.nl/dsprenkels/rushlink/src/branch/master/handlers.go#L251

	var base64OwnerToken = make([]byte, 24)
	base64.RawURLEncoding.Encode(base64OwnerToken, storedPaste.OwnerToken[:])
In https://gitea.hashru.nl/dsprenkels/rushlink/src/branch/master/handlers.go#L56 we can use `base64.RawURLEncoding` provided in the standard library. E.g. https://gitea.hashru.nl/dsprenkels/rushlink/src/branch/master/handlers.go#L251 ``` var base64OwnerToken = make([]byte, 24) base64.RawURLEncoding.Encode(base64OwnerToken, storedPaste.OwnerToken[:]) ```
Owner

I am wondering if this is actually possible. Although we use the base64url alphabet, we do not use the base64 encoding. That is, because our IDs are generated in base 64, not in base 256. (I.e. they are not defined over bytes, but over sextets.)

Reusing base64.encodeURL, would be cool, but is obviously impossible.

Closing this as wontfix.

I am wondering if this is actually possible. Although we use the base64url *alphabet*, we do not use the base64 *encoding*. That is, because our IDs are generated in base 64, not in base *256*. (I.e. they are not defined over bytes, but over sextets.) Reusing [`base64.encodeURL`](https://golang.org/src/encoding/base64/base64.go?s=14692:14745#L36), would be cool, but is obviously impossible. Closing this as wontfix.
Author
Collaborator

In 544c093c35/internal/handlers/handlers.go (L257) you do a base64 encoding over a slice of bytes, that seem ultimately filled through generateOwnerToken() in 544c093c35/internal/handlers/handlers.go (L373), where 16 bytes are sourced from rand.Read().

I'm not sure what you mean with

That is, because our IDs are generated in base 64, not in base 256.

because that is (obviously) not the case.

In https://gitea.hashru.nl/dsprenkels/rushlink/src/commit/544c093c353257bc67395e2a085f1c20318a182c/internal/handlers/handlers.go#L257 you do a base64 encoding over a slice of bytes, that seem ultimately filled through `generateOwnerToken()` in https://gitea.hashru.nl/dsprenkels/rushlink/src/commit/544c093c353257bc67395e2a085f1c20318a182c/internal/handlers/handlers.go#L373, where 16 bytes are sourced from `rand.Read()`. I'm not sure what you mean with > That is, because our IDs are generated in base 64, not in base 256. because that is (_obviously_) not the case.
mrngm reopened this issue 2019-09-10 22:59:07 +02:00
Owner

I now see what you mean. I fixed this; applying this patch:

diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go
index 39077ac..a4a0a87 100644
--- a/internal/handlers/handlers.go
+++ b/internal/handlers/handlers.go
@@ -56,7 +56,7 @@ var ReservedPasteKeys [][]byte = [][]byte{[]byte("xd42"), []byte("example")}
 
 // Base64 encoding and decoding
 var base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
-var base64Encoder = base64.NewEncoding(base64Alphabet).WithPadding(base64.NoPadding)
+var base64Encoder = base64.RawURLEncoding.WithPadding(base64.NoPadding)
 
 // Page contents
 var baseTemplate = template.New("empty")

I now see what you mean. I fixed this; applying this patch: ```diff diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 39077ac..a4a0a87 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -56,7 +56,7 @@ var ReservedPasteKeys [][]byte = [][]byte{[]byte("xd42"), []byte("example")} // Base64 encoding and decoding var base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" -var base64Encoder = base64.NewEncoding(base64Alphabet).WithPadding(base64.NoPadding) +var base64Encoder = base64.RawURLEncoding.WithPadding(base64.NoPadding) // Page contents var baseTemplate = template.New("empty") ```
Owner

Note. It seems I misunderstood which base64 encoding was meant, because of the outdated line specifiers. I should just have Ctrl+F'd the code in the snippet.

Note. It seems I misunderstood which base64 encoding was meant, because of the outdated line specifiers. I should just have Ctrl+F'd the code in the snippet.
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: electricdusk/rushlink#1
No description provided.