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
+5.6k Golang : Frobnicate or tweaking a string example
+20.7k Android Studio : AlertDialog and EditText to get user string input example
+14.8k Golang : Get URI segments by number and assign as variable example
+7.7k Golang : Test if an input is an Armstrong number example
+16.6k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+4.7k Javascript : Access JSON data example
+9.5k Golang : Convert(cast) string to int64
+22.9k Golang : Calculate time different
+13.5k Golang : Read XML elements data with xml.CharData example
+8.1k Golang : Randomize letters from a string example
+12k Golang : Convert a rune to unicode style string \u
+26.9k Golang : Force your program to run with root permissions