-
Notifications
You must be signed in to change notification settings - Fork 3
/
ykmodhex.go
58 lines (49 loc) · 937 Bytes
/
ykmodhex.go
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) 2013 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package yubikey
import (
"bytes"
)
const (
ModHexMap = "cbdefghijklnrtuv"
)
func ModHexEncode(src []byte) []byte {
dst := make([]byte, len(src)*2)
dstidx := 0
for _, val := range src {
dst[dstidx] = ModHexMap[(val>>4)&0xf]
dst[dstidx+1] = ModHexMap[val&0xf]
dstidx += 2
}
return dst
}
func ModHexDecode(src []byte) []byte {
dst := make([]byte, (len(src)+1)/2)
alt := false
idx := 0
for _, val := range src {
b := bytes.IndexByte([]byte(ModHexMap), val)
if b == -1 {
b = 0
}
bb := byte(b)
alt = !alt
if alt {
dst[idx] = bb
} else {
dst[idx] <<= 4
dst[idx] |= bb
idx++
}
}
return dst
}
func ModHexP(src []byte) bool {
for _, val := range src {
if bytes.IndexByte([]byte(ModHexMap), val) == -1 {
return false
}
}
return true
}