Skip to content

Commit

Permalink
Hex encoder and decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
joohoi committed Jun 26, 2020
1 parent bfc3c79 commit 663b6a2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/pencode/encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
var availableEncoders = map[string]Encoder{
"b64encode": Base64Encoder{},
"b64decode": Base64Decoder{},
"hexencode": HexEncoder{},
"hexdecode": HexDecoder{},
"urlencode": URLEncoder{},
"urldecode": URLDecoder{},
"urlencodeall": URLEncoderAll{},
Expand Down
16 changes: 16 additions & 0 deletions pkg/pencode/hexdecoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pencode

import (
"encoding/hex"
)

type HexDecoder struct{}

func (h HexDecoder) Encode(input []byte) ([]byte, error) {
cleaned := removeAllWhitespace(string(input))
return hex.DecodeString(cleaned)
}

func (h HexDecoder) HelpText() string {
return "Hex string decoder"
}
15 changes: 15 additions & 0 deletions pkg/pencode/hexencoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pencode

import (
"encoding/hex"
)

type HexEncoder struct{}

func (h HexEncoder) Encode(input []byte) ([]byte, error) {
return []byte(hex.EncodeToString(input)), nil
}

func (h HexEncoder) HelpText() string {
return "Hex string encoder"
}
17 changes: 17 additions & 0 deletions pkg/pencode/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package pencode

import (
"strings"
"unicode"
)

func removeAllWhitespace(input string) string {
var b strings.Builder
b.Grow(len(input))
for _, ch := range input {
if !unicode.IsSpace(ch) {
b.WriteRune(ch)
}
}
return b.String()
}

0 comments on commit 663b6a2

Please sign in to comment.