keycard-go/derivationpath/encoder.go

37 lines
686 B
Go
Raw Normal View History

2019-03-15 16:03:01 +00:00
package derivationpath
import (
"bytes"
"encoding/binary"
"fmt"
"strings"
)
func Encode(rawPath []uint32) string {
2022-08-02 10:18:52 +00:00
segments := []string{string(rune(tokenMaster))}
2019-03-15 16:03:01 +00:00
for _, i := range rawPath {
suffix := ""
if i >= hardenedStart {
i = i - hardenedStart
2022-08-02 10:18:52 +00:00
suffix = string(rune(tokenHardened))
2019-03-15 16:03:01 +00:00
}
segments = append(segments, fmt.Sprintf("%d%s", i, suffix))
}
2022-08-02 10:18:52 +00:00
return strings.Join(segments, string(rune(tokenSeparator)))
2019-03-15 16:03:01 +00:00
}
func EncodeFromBytes(data []byte) (string, error) {
buf := bytes.NewBuffer(data)
rawPath := make([]uint32, buf.Len()/4)
err := binary.Read(buf, binary.BigEndian, &rawPath)
if err != nil {
return "", err
}
return Encode(rawPath), nil
}