Implement Identity encoding
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user