-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
54 lines (45 loc) · 1.27 KB
/
format.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
package asn
import (
"encoding/binary"
"fmt"
"strings"
"github.com/thatmattlove/go-asn/internal/util"
)
// Decimal formats the ASN in asplain format. See RFC5396.
func (asn ASN) Decimal() string {
d := binary.BigEndian.Uint32(asn)
return fmt.Sprint(d)
}
// ASPlain formats the ASN in asplain format. See RFC5396.
func (asn ASN) ASPlain() string {
return asn.Decimal()
}
// ASDotPlus formats the ASN in asdot+ format. See RFC5396.
func (asn ASN) ASDotPlus() string {
high := binary.BigEndian.Uint32(util.ZeroPad(asn[0:2], BYTE_SIZE))
low := binary.BigEndian.Uint32(util.ZeroPad(asn[2:4], BYTE_SIZE))
return fmt.Sprintf("%d.%d", high, low)
}
// ASDot formats the ASN in asdot format. See RFC5396.
func (asn ASN) ASDot() string {
if asn.Size() == 2 {
return asn.Decimal()
}
return asn.ASDotPlus()
}
// String formats the ASN in decimal/asplain format. See RFC5396.
func (asn ASN) String() string {
return asn.Decimal()
}
// Uint32 returns the ASN as a 32-bit unsigned integer.
func (asn ASN) Uint32() uint32 {
return binary.BigEndian.Uint32(asn)
}
// ByteString returns a string representation of each ASN byte.
func (asn ASN) ByteString() string {
bs := []string{}
for _, b := range asn {
bs = append(bs, fmt.Sprint(b))
}
return fmt.Sprintf("{%s}", strings.Join(bs, ","))
}