Golang encoding/asn1.Unmarshal function example

package encoding/asn1

Unmarshal parses the DER-encoded ASN.1 data structure b(1st param) and uses the reflect package to fill in an arbitrary value pointed at by val(2nd value). Because Unmarshal uses the reflect package, the structs being written to must use upper case field names.

see the rest of the description at http://golang.org/pkg/encoding/asn1/#Unmarshal

Golang encoding/asn1.Unmarshal function usage example

 var raw []byte // pem block

 block, _ := pem.Decode(raw)
 raw := block.Bytes

 key := struct {
 Version int // remember... must use upper case field names
 P *big.Int
 Q *big.Int
 G *big.Int
 Priv *big.Int
 Pub *big.Int
  }{}

 var rest []byte
 rest, err = asn1.Unmarshal(raw, &key)

Reference :

http://golang.org/pkg/encoding/asn1/#Unmarshal

Advertisement