Implement Identity encoding

This commit is contained in:
Wigy
2016-12-19 14:34:26 +01:00
committed by Jakub Sztandera
parent c264cdd0ae
commit 374a93370a
2 changed files with 54 additions and 2 deletions

View File

@@ -12,6 +12,7 @@ import (
type Encoding int
const (
Identity = 0x00
Base1 = '1'
Base2 = '0'
Base8 = '7'
@@ -38,6 +39,9 @@ var ErrUnsupportedEncoding = fmt.Errorf("selected encoding not supported")
func Encode(base Encoding, data []byte) (string, error) {
switch base {
case Identity:
// 0x00 inside a string is OK in golang and causes no problems with the length calculation.
return string(Identity) + string(data), nil
case Base16, Base16Upper:
return string(Base16) + hex.EncodeToString(data), nil
case Base32, Base32Upper:
@@ -67,6 +71,8 @@ func Decode(data string) (Encoding, []byte, error) {
}
switch data[0] {
case Identity:
return Identity, []byte(data[1:]), nil
case Base16, Base16Upper:
bytes, err := hex.DecodeString(data[1:])
return Base16, bytes, err