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
+51.9k Golang : How to get time in milliseconds?
+4.7k Adding Skype actions such as call and chat into web page examples
+7.9k Golang : Get today's weekday name and calculate target day distance example
+22.2k Golang : How to run Golang application such as web server in the background or as daemon?
+17k Golang : How to save log messages to file?
+5.7k Linux/Unix/PHP : Restart PHP-FPM
+8.1k Golang : Randomize letters from a string example
+19.3k Golang : Get host name or domain name from IP address
+9.8k Golang : Qt get screen resolution and display on center example
+11.3k Golang : How to use if, eq and print properly in html template