-
Notifications
You must be signed in to change notification settings - Fork 22
/
oid.go
88 lines (68 loc) · 1.71 KB
/
oid.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package asn1
import (
"bytes"
"fmt"
"reflect"
"strconv"
)
type ObjectIdentifier []int
// Equal reports whether oi and other represent the same identifier.
func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool {
if len(oi) != len(other) {
return false
}
for i := 0; i < len(oi); i++ {
if oi[i] != other[i] {
return false
}
}
return true
}
func (oi ObjectIdentifier) String() string {
var s string
for i, v := range oi {
if i > 0 {
s += "."
}
s += strconv.Itoa(v)
}
return s
}
func isValidRootNode(root int) bool {
switch root {
case 0, 1, 2:
return true
default:
return false
}
}
func NewObjectIdentifier(root int, node []int) (ObjectIdentifier, error) {
if !isValidRootNode(root) {
return nil, fmt.Errorf("error creating object identifier: invalid root node %d", root)
}
if len(node) == 0 {
return nil, fmt.Errorf("error creating object identifier: empty node")
}
switch root {
case 0, 1:
if node[0] >= 40 {
return nil, fmt.Errorf("invalid node")
}
}
oid := []int{root}
oid = append(oid, node...)
return ObjectIdentifier(oid), nil
}
var objectIdentifierType = reflect.TypeOf(ObjectIdentifier{})
type objectIdentifierEncoder ObjectIdentifier
func (b objectIdentifierEncoder) length() int {
return len(b)
}
func (e objectIdentifierEncoder) encode() ([]byte, error) {
b := new(bytes.Buffer)
b.Write(encodeBase128((e[0]*40 + e[1])))
for i := 2; i < len(e); i++ {
b.Write(encodeBase128(e[i]))
}
return b.Bytes(), nil
}