Skip to content

Commit fcdadc4

Browse files
committed
Add URL safe 'transform.{En,De}codeBase64` variants
Adds `EncodeBase64URL` and `DecodeBase64URL` to the transform package. Multiple times now we've run into situations where the URL variant is necessary or the target encoder/decoder is more flexible and allows either. This replaces a few silly collections of strings.ReplaceAll for the other variants.
1 parent 80f1e79 commit fcdadc4

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

transform/encode.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,17 @@ func Inflate(compressed []byte) ([]byte, bool) {
3434
return inflated, true
3535
}
3636

37+
// EncodeBase64 uses standard format non-URL safe base64 encoding to encode a string.
3738
func EncodeBase64(s string) string {
3839
return base64.StdEncoding.EncodeToString([]byte(s))
3940
}
4041

42+
// EncodeBase64URL encodes to URL safe base64 with padding.
43+
func EncodeBase64URL(s string) string {
44+
return base64.URLEncoding.EncodeToString([]byte(s))
45+
}
46+
47+
// DecodeBase64 decodes base64 with standard encoding
4148
func DecodeBase64(s string) string {
4249
decoded, err := base64.StdEncoding.DecodeString(s)
4350
if err != nil {
@@ -49,6 +56,21 @@ func DecodeBase64(s string) string {
4956
return string(decoded)
5057
}
5158

59+
// DecodeBase64URL decodes URL safe base64 variant.
60+
func DecodeBase64URL(s string) string {
61+
// Internally uses the non-padded version for decoding so this will allow forcing padded
62+
// data to be non-padded and support both transparently.
63+
s = strings.ReplaceAll(s, `=`, ``)
64+
decoded, err := base64.RawURLEncoding.DecodeString(s)
65+
if err != nil {
66+
output.PrintFrameworkError(err.Error())
67+
68+
return ""
69+
}
70+
71+
return string(decoded)
72+
}
73+
5274
func Title(s string) string {
5375
return cases.Title(language.Und, cases.NoLower).String(s)
5476
}

transform/encode_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,46 @@ func TestEncodeBase64(t *testing.T) {
2424
t.Log(encoded)
2525
}
2626

27+
func TestEncodeBase64URL(t *testing.T) {
28+
encoded := EncodeBase64URL("Theskyabovetheportwasthecoloroftelevision,tunedtoadeadchannel.\xff\xff\xff\x3e\xfe\x00")
29+
30+
if encoded != "VGhlc2t5YWJvdmV0aGVwb3J0d2FzdGhlY29sb3JvZnRlbGV2aXNpb24sdHVuZWR0b2FkZWFkY2hhbm5lbC7___8-_gA=" {
31+
t.Fatal(encoded)
32+
}
33+
34+
t.Log(encoded)
35+
}
36+
37+
func TestDecodeBase64URL(t *testing.T) {
38+
decoded := DecodeBase64URL("VGhlc2t5YWJvdmV0aGVwb3J0d2FzdGhlY29sb3JvZnRlbGV2aXNpb24sdHVuZWR0b2FkZWFkY2hhbm5lbC7___8-_gA=")
39+
40+
if decoded != "Theskyabovetheportwasthecoloroftelevision,tunedtoadeadchannel.\xff\xff\xff\x3e\xfe\x00" {
41+
t.Fatal(decoded)
42+
}
43+
44+
t.Log(decoded)
45+
}
46+
47+
func TestDecodeBase64URL_NoPad(t *testing.T) {
48+
decoded := DecodeBase64URL("VGhlc2t5YWJvdmV0aGVwb3J0d2FzdGhlY29sb3JvZnRlbGV2aXNpb24sdHVuZWR0b2FkZWFkY2hhbm5lbC7___8-_gA")
49+
50+
if decoded != "Theskyabovetheportwasthecoloroftelevision,tunedtoadeadchannel.\xff\xff\xff\x3e\xfe\x00" {
51+
t.Fatal(decoded)
52+
}
53+
54+
t.Log(decoded)
55+
}
56+
57+
func TestDecodeBase64URL_WayTooMuchPadding(t *testing.T) {
58+
decoded := DecodeBase64URL("VGhlc2t5YWJvdmV0aGVwb3J0d2FzdGhlY29sb3JvZnRlbGV2aXNpb24sdHVuZWR0b2FkZWFkY2hhbm5lbC7___8-_gA=======")
59+
60+
if decoded != "Theskyabovetheportwasthecoloroftelevision,tunedtoadeadchannel.\xff\xff\xff\x3e\xfe\x00" {
61+
t.Fatal(decoded)
62+
}
63+
64+
t.Log(decoded)
65+
}
66+
2767
func TestDecodeBase64(t *testing.T) {
2868
decoded := DecodeBase64("Zm9v")
2969

0 commit comments

Comments
 (0)