Skip to content

Commit

Permalink
simplify some method hierarchies
Browse files Browse the repository at this point in the history
  • Loading branch information
dropwhile committed Aug 22, 2023
1 parent 69aca1f commit 618f375
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 37 deletions.
41 changes: 7 additions & 34 deletions pkg/camo/encoding/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,6 @@ func generateUrlMac(hmacKey []byte, oBytes []byte, oExtraHdrBytes []byte) []byte
return mac.Sum(nil)
}

func hexEncode(data []byte) string {
return hex.EncodeToString(data)
}

func hexDecode(data string) ([]byte, error) {
return hex.DecodeString(data)
}

func b64Encode(data []byte) string {
return base64.RawURLEncoding.EncodeToString(data)
}

func b64Decode(data string) ([]byte, error) {
decBytes, ok := base64.RawURLEncoding.DecodeString(data)
return decBytes, ok
}

func extraHdrUnmarshal(extraHdrBytes []byte) (SimpleHeader, error) {
var headers SimpleHeader
err := json.Unmarshal(extraHdrBytes, &headers)
Expand All @@ -104,17 +87,7 @@ func extraHdrMarshal(headers SimpleHeader) ([]byte, error) {
return headerBytes, nil
}

func decodeURL(codec Codec, hmackey []byte, encDig, encURL, encExtraHdr string) (string, SimpleHeader, error) {
var decode decodeFunc
switch codec {
case B64Codec:
decode = b64Decode
case HexCodec:
decode = hexDecode
default:
return "", nil, fmt.Errorf("bad codec specified")
}

func decodeURL(decode decodeFunc, hmackey []byte, encDig, encURL, encExtraHdr string) (string, SimpleHeader, error) {
macBytes, err := decode(encDig)
if err != nil {
return "", nil, fmt.Errorf("bad mac decode")
Expand Down Expand Up @@ -156,14 +129,14 @@ func decodeURL(codec Codec, hmackey []byte, encDig, encURL, encExtraHdr string)
// HMAC was verified. Tries either HexDecode or B64Decode, depending on the
// length of the encoded hmac.
func DecodeURL(hmackey []byte, encDig, encURL, encExtraHdr string) (string, SimpleHeader, bool) {
var codec Codec
var decode decodeFunc
if len(encDig) == 40 {
codec = HexCodec
decode = hex.DecodeString
} else {
codec = B64Codec
decode = base64.RawURLEncoding.DecodeString
}

urlBytes, extraHdr, err := decodeURL(codec, hmackey, encDig, encURL, encExtraHdr)
urlBytes, extraHdr, err := decodeURL(decode, hmackey, encDig, encURL, encExtraHdr)
if err != nil {
if mlog.HasDebug() {
mlog.Debugf("Bad Decode of URL: %s", err)
Expand All @@ -179,9 +152,9 @@ func EncodeURL(codec Codec, hmacKey []byte, oURL string, oExtraHdr SimpleHeader)
var encFunc encodeFunc
switch codec {
case B64Codec:
encFunc = b64Encode
encFunc = base64.RawURLEncoding.EncodeToString
case HexCodec:
encFunc = hexEncode
encFunc = hex.EncodeToString
default:
return "", fmt.Errorf("bad encoded specified")
}
Expand Down
17 changes: 14 additions & 3 deletions pkg/camo/encoding/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package encoding

import (
"encoding/base64"
"encoding/hex"
"fmt"
"testing"

Expand Down Expand Up @@ -122,13 +124,13 @@ func BenchmarkB64Encoder(b *testing.B) {

func BenchmarkHexDecoder(b *testing.B) {
for i := 0; i < b.N; i++ {
decodeURL(HexCodec, []byte("test"), "0f6def1cb147b0e84f39cbddc5ea10c80253a6f3", "687474703a2f2f676f6c616e672e6f72672f646f632f676f706865722f66726f6e74706167652e706e67", "")
decodeURL(hex.DecodeString, []byte("test"), "0f6def1cb147b0e84f39cbddc5ea10c80253a6f3", "687474703a2f2f676f6c616e672e6f72672f646f632f676f706865722f66726f6e74706167652e706e67", "")
}
}

func BenchmarkB64Decoder(b *testing.B) {
for i := 0; i < b.N; i++ {
decodeURL(B64Codec, []byte("test"), "D23vHLFHsOhPOcvdxeoQyAJTpvM", "aHR0cDovL2dvbGFuZy5vcmcvZG9jL2dvcGhlci9mcm9udHBhZ2UucG5n", "")
decodeURL(base64.RawURLEncoding.DecodeString, []byte("test"), "D23vHLFHsOhPOcvdxeoQyAJTpvM", "aHR0cDovL2dvbGFuZy5vcmcvZG9jL2dvcGhlci9mcm9udHBhZ2UucG5n", "")
}
}

Expand Down Expand Up @@ -225,8 +227,17 @@ func TestBadDecodes(t *testing.T) {
t.Parallel()
for _, p := range badTests {
hmacKey := []byte(p.hmac)

var decode decodeFunc
switch p.codec {
case B64Codec:
decode = base64.RawURLEncoding.DecodeString
case HexCodec:
decode = hex.DecodeString
}

// test specific decoder
encodedURL, extraHdr, err := decodeURL(p.codec, hmacKey, p.edig, p.eURL, p.eExtraHdr)
encodedURL, extraHdr, err := decodeURL(decode, hmacKey, p.edig, p.eURL, p.eExtraHdr)
assert.Check(t, err != nil, "decoded url verfied when it shouldn't have")
assert.Check(t, is.Equal(encodedURL, ""), "decoded url result not empty")
assert.Check(t, is.Equal(len(extraHdr), 0), "decoded extraHdr header map is not empty")
Expand Down

0 comments on commit 618f375

Please sign in to comment.