jasn1/asn1/tag.go

71 lines
1.1 KiB
Go

package asn1
import (
"fmt"
"time"
)
type TagType uint8
const (
INTEGER = 0x02
BIT_STRING = 0x03
OCTET_STRING = 0x04
NULL = 0x05
OBJECT_IDENTIFIER = 0x06
UTF8_STRING = 0x0C
SEQUENCE_OF = 0x10
SET_OF = 0x11
PRINTABLE_STRING = 0x13
IA5_STRING = 0x16
UTC_TIME = 0x17
GENERALIZED_TIME = 0x18
)
type TagClass uint8
const (
UNIVERSAL = iota
APPLICATION
CONTEXT_SPECIFIC
PRIVATE
)
type Tag struct {
Kind TagType
Constructed bool
Class TagClass
Children []*Tag
Value []byte
}
func (tag *Tag) String() string {
repr := fmt.Sprintf("Tag[Kind:%d,", tag.Kind)
if tag.Constructed {
repr += fmt.Sprintf("Children:%s]", tag.Children)
} else {
repr += fmt.Sprintf("Value:[% X]]", tag.Value)
}
return repr
}
func (tag *Tag) At(idxs ...uint) *Tag {
var at_tag *Tag = tag
for _, idx := range idxs {
at_tag = at_tag.Children[idx]
}
return at_tag
}
func (tag *Tag) BoolValue() bool {
return tag.Value[0] == 0xFF
}
func (tag *Tag) DateValue() time.Time {
date, _ := time.Parse("060102150405Z", string(tag.Value))
return date
}