-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhex.go
More file actions
27 lines (21 loc) · 667 Bytes
/
hex.go
File metadata and controls
27 lines (21 loc) · 667 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package srkeyring
import (
"encoding/hex"
"fmt"
"strings"
)
// HexPrefix defines a prefix used in the hex encoded output
type HexPrefix string
// DecodeHex decodes the hex string to bytes and truncates any prefix used
func DecodeHex(str string, prefix HexPrefix) (seed []byte, ok bool) {
if strings.HasPrefix(str, string(prefix)) {
str = strings.TrimPrefix(str, string(prefix))
}
res, err := hex.DecodeString(str)
return res, err == nil
}
// EncodeHex encodes the raw bytes into a hex encoded string and appends any
// prefix required
func EncodeHex(raw []byte, prefix HexPrefix) string {
return fmt.Sprintf("%s%s", prefix, hex.EncodeToString(raw))
}