123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package cryptobyte_test
- import (
- "encoding/asn1"
- "fmt"
- "golang.org/x/crypto/cryptobyte"
- )
- func ExampleString_lengthPrefixed() {
-
-
-
- input := cryptobyte.String([]byte{0, 12, 5, 'h', 'e', 'l', 'l', 'o', 5, 'w', 'o', 'r', 'l', 'd'})
- var result []string
- var values cryptobyte.String
- if !input.ReadUint16LengthPrefixed(&values) ||
- !input.Empty() {
- panic("bad format")
- }
- for !values.Empty() {
- var value cryptobyte.String
- if !values.ReadUint8LengthPrefixed(&value) {
- panic("bad format")
- }
- result = append(result, string(value))
- }
-
- fmt.Printf("%#v\n", result)
- }
- func ExampleString_asn1() {
-
-
-
-
-
- input := cryptobyte.String([]byte{0x30, 12, 0xa6, 3, 2, 1, 2, 4, 5, 'h', 'e', 'l', 'l', 'o'})
- var (
- version int64
- data, inner, versionBytes cryptobyte.String
- haveVersion bool
- )
- if !input.ReadASN1(&inner, cryptobyte.Tag(asn1.TagSequence).Constructed()) ||
- !input.Empty() ||
- !inner.ReadOptionalASN1(&versionBytes, &haveVersion, cryptobyte.Tag(6).Constructed().ContextSpecific()) ||
- (haveVersion && !versionBytes.ReadASN1Integer(&version)) ||
- (haveVersion && !versionBytes.Empty()) ||
- !inner.ReadASN1(&data, asn1.TagOctetString) ||
- !inner.Empty() {
- panic("bad format")
- }
-
- fmt.Printf("haveVersion: %t, version: %d, data: %s\n", haveVersion, version, string(data))
- }
- func ExampleBuilder_asn1() {
-
-
-
-
-
- version := int64(2)
- data := []byte("hello")
- const defaultVersion = 0
- var b cryptobyte.Builder
- b.AddASN1(cryptobyte.Tag(asn1.TagSequence).Constructed(), func(b *cryptobyte.Builder) {
- if version != defaultVersion {
- b.AddASN1(cryptobyte.Tag(6).Constructed().ContextSpecific(), func(b *cryptobyte.Builder) {
- b.AddASN1Int64(version)
- })
- }
- b.AddASN1OctetString(data)
- })
- result, err := b.Bytes()
- if err != nil {
- panic(err)
- }
-
- fmt.Printf("%x\n", result)
- }
- func ExampleBuilder_lengthPrefixed() {
-
-
-
- input := []string{"hello", "world"}
- var b cryptobyte.Builder
- b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
- for _, value := range input {
- b.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
- b.AddBytes([]byte(value))
- })
- }
- })
- result, err := b.Bytes()
- if err != nil {
- panic(err)
- }
-
- fmt.Printf("%x\n", result)
- }
|