Golang crypto/x509.ParseCertificateRequest() function example
package crypto/x509
ParseCertificateRequest parses a single certificate request from the given ASN.1 DER data.
Golang crypto/x509.ParseCertificateRequest() function usage example
package main
import (
"net"
"crypto/x509"
"crypto/x509/pkix"
"fmt"
"crypto/rsa"
"crypto/rand"
)
func main() {
// populate some dummy data.
// see CertificateRequest full structure at
// http://golang.org/pkg/crypto/x509/#CertificateRequest
template := &x509.CertificateRequest {
Signature : []byte("Hello World Signature"),
SignatureAlgorithm : 2, // http://golang.org/pkg/crypto/x509/#SignatureAlgorithm
Subject : pkix.Name{
Country : []string{"Earth"},
Organization: []string{"Mother Nature"},
},
DNSNames : []string{"example.abc123.com"},
EmailAddresses : []string{"admin@abc123.com"},
IPAddresses : []net.IP{net.IPv4(127, 0, 0, 1).To4(), net.ParseIP("2001:1234:0:2001::56")},
}
// generate private key
privatekey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
fmt.Println(err)
}
derBytes, err := x509.CreateCertificateRequest(rand.Reader, template, privatekey)
if err != nil {
fmt.Println(err)
}
parsedcertreq, err := x509.ParseCertificateRequest(derBytes)
if err != nil {
fmt.Println(err)
}
fmt.Println(parsedcertreq.Version)
fmt.Println(parsedcertreq.Signature)
fmt.Println(parsedcertreq.EmailAddresses)
fmt.Println(parsedcertreq.DNSNames)
}
Output :
0
[154 199... 196 211 86]
[admin@abc123.com]
[example.abc123.com]
Reference :
Advertisement
Something interesting
Tutorials
+6.6k Golang : Warp text string by number of characters or runes example
+13.2k Golang : Convert(cast) int to int64
+6k Javascript : Get operating system and browser information
+16.1k Golang : How to check if input from os.Args is integer?
+16.5k Golang : Check if a string contains multiple sub-strings in []string?
+17k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+13.2k Golang : How to calculate the distance between two coordinates using Haversine formula
+9.5k Golang : Accessing content anonymously with Tor
+6.8k Android Studio : Hello World example
+12.8k Golang : Listen and Serve on sub domain example
+14.4k Golang : Find network of an IP address
+6.9k Golang : How to solve "too many .rsrc sections" error?