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.3k Golang : Generate Interleaved 2 inch by 5 inch barcode
+11.2k Golang : Fix - does not implement sort.Interface (missing Len method)
+18.5k Golang : Send email with attachment
+11.7k Golang : Calculations using complex numbers example
+7.6k Javascript : Push notifications to browser with Push.js
+11.7k Golang : Find age or leap age from date of birth example
+3.7k Golang : Switch Redis database redis.NewClient
+16.7k Golang : Gzip file example
+12.4k Golang : Extract part of string with regular expression
+15.6k Golang : How to convert(cast) IP address to string?
+21.6k Golang : GORM create record or insert new record into database example