https://github.com/skythen/bertlv
Golang implementation of parsing and building BER-TLV.
https://github.com/skythen/bertlv
bertlv constructed-objects
Last synced: 5 months ago
JSON representation
Golang implementation of parsing and building BER-TLV.
- Host: GitHub
- URL: https://github.com/skythen/bertlv
- Owner: skythen
- License: mit
- Created: 2021-01-07T14:33:24.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-05-16T11:25:47.000Z (about 5 years ago)
- Last Synced: 2024-06-21T04:47:33.927Z (almost 2 years ago)
- Topics: bertlv, constructed-objects
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 8
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# BER-TLV
[](https://travis-ci.org/skythen/bertlv)
[](https://coveralls.io/github/skythen/bertlv?branch=master)
[](http://godoc.org/github.com/skythen/bertlv)
[](https://goreportcard.com/report/github.com/skythen/bertlv)
Package bertlv implements parsing and building of BER-TLV structures.
Please note that this is not a complete implementation of the X.690 standard as it is agnostic about classes (Universal, Application, Context-specific, Private) and therefore does not check for correct encoding of tags/values.
`go get github.com/skythen/bertlv`
## Parse
You can parse BER-TLV encoded data from bytes:
```go
b := []byte{0x71, 0x10, 0xB0, 0x0E, 0x0F, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x0E, 0x05, 0x05, 0x04, 0x03, 0x02, 0x01}
bertlvs, err := Parse(b)
```
### Constructed objects
You can check if a BerTLV is constructed and get first or all children or filter child objects by tag:
```go
if bertlvs[0].Tag.IsConstructed() {
child := bertlvs[0].FirstChild(NewOneByteTag(0x0F))
}
```
## Create
You can create single BER-TLVs with NewBerTLV:
```go
val := []byte{0xB0, 0x0E, 0x0F, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x0E, 0x05, 0x05, 0x04, 0x03, 0x02, 0x01}
bertlv, err := NewBerTLV(NewOneByteTag(0x71), val)
```
If you want to create complex constructed objects you use the Builder:
```go
builder := Builder{}
nestedBuilder := Builder{}
anotherNestedBuilder := Builder{}
berTlvs, err := builder.AddBytes(NewOneByteTag(0x71), // first level, constructed object
nestedBuilder.AddBytes(NewOneByteTag(0xB0), // second level, constructed object
anotherNestedBuilder.
AddBytes(NewOneByteTag(0x0F), []byte{0x01, 0x02, 0x03, 0x04, 0x05}). // third level primitive object
AddBytes(NewOneByteTag(0x0E), []byte{0x05, 0x04, 0x03, 0x02, 0x01}). // third level primitive object
Bytes()).
Bytes()).
BuildBerTLVs()
```
You can also use
- builder.AddEmpty() to add objects without value
- builder.AddByte() to add objects with a value that consists of a single byte
- builder.AddRaw() to add raw bytes