Browse Source

Handle upper or mixed case modhex input

master
Blink The Things 3 years ago
parent
commit
7ea123c915
2 changed files with 43 additions and 19 deletions
  1. +31
    -18
      modhex.go
  2. +12
    -1
      modhex_test.go

+ 31
- 18
modhex.go View File

@ -4,6 +4,7 @@ package modhex
import (
"errors"
"fmt"
"strings"
)
const modhexTable = "cbdefghijklnrtuv"
@ -24,20 +25,31 @@ func EncodedLen(n int) int {
}
// Encode encodes src into EncodedLen(len(src)) bytes of dst.
// It returns the number of bytes written to dst, but this is
// always EncodedLen(len(src)).
// Encode implements modhex encoding.
//
// Encode returns the number of bytes written to dst, but this is always
// EncodedLen(len(src)).
func Encode(dst, src []byte) int {
i := 0
// keep track of the number of bytes written to dst
len := 0
// convert each byte of src and store in dst
for _, v := range src {
dst[i] = modhexTable[v>>4]
dst[i+1] = modhexTable[v&0x0f]
i += 2
// convert the upper nibble to modhex
// and store it into dst
dst[len] = modhexTable[v>>4]
len++
// convert the lower nibble to modhex
// and store it into dst
dst[len] = modhexTable[v&0x0f]
len++
}
return len(src) * 2
// return the number of bytes written
return len
}
// EncodeToString returns the modhex encoding of src.
// EncodeToString returns the modhex encoding of src as a string.
func EncodeToString(src []byte) string {
// make a new slice to hold the encoded bytes
dst := make([]byte, EncodedLen(len(src)))
@ -58,10 +70,9 @@ func DecodedLen(n int) int {
// Decode decodes src into DecodedLen(len(src)) bytes, returning
// the actual number of bytes written to dst.
//
// Decode expects that src contains only modhex characters and that
// src has an even length.
// If the input is malformed, Decode returns the number
// of bytes decoded before the error.
// Decode expects that src contains only modhex characters and that src has an
// even length. If the input is malformed, Decode returns the number of bytes
// decoded before the error.
func Decode(dst, src []byte) (int, error) {
i, j := 0, 1
for ; j < len(src); j += 2 {
@ -90,13 +101,15 @@ func Decode(dst, src []byte) (int, error) {
return i, nil
}
// DecodeString returns the bytes represesented by the modhex string s.
// DecodeString returns the bytes represented by the modhex string s.
//
// Decode expects that s contains only modhex characters and that s
// has an even length.
// If the input is malformed, Decode returns the number
// of bytes decoded before the error.
// DecodeString expects that s contains only modhex characters and that s
// has an even length. If the input is malformed, DecodeString returns the
// number of bytes decoded before the error.
func DecodeString(s string) ([]byte, error) {
// convert the string to lower case since the map uses lower case
s = strings.ToLower(s)
// convert the input string to an array of bytes
src := []byte(s)


+ 12
- 1
modhex_test.go View File

@ -2,6 +2,7 @@ package modhex
import (
"encoding/hex"
"strings"
"testing"
)
@ -17,6 +18,16 @@ func TestEncodeHexDecodeHex(t *testing.T) {
hex: "2d344e83",
},
{
modhex: "DTEFFUJE",
hex: "2d344e83",
},
{
modhex: "dTeFfUjE",
hex: "2d344e83",
},
{
modhex: "hknhfjbrjnlnldnhcujvddbikngjrtgh",
hex: "69b6481c8baba2b60e8f22179b58cd56",
@ -32,7 +43,7 @@ func TestEncodeHexDecodeHex(t *testing.T) {
src, _ := hex.DecodeString(tc.hex)
modhex := EncodeToString(src)
if modhex != tc.modhex {
if modhex != strings.ToLower(modhex) {
t.Errorf("Encode Incorrect: Actual: %s; Expected: %s\n", modhex, tc.modhex)
}


Loading…
Cancel
Save