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
+13k Android Studio : Highlight ImageButton when pressed on example
+11.8k Use systeminfo to find out installed Windows Hotfix(s) or updates
+8.3k Golang : Handle Palindrome string with case sensitivity and unicode
+15.5k Golang : package is not in GOROOT during compilation
+17.3k Golang : Get number of CPU cores
+8.2k Javascript : How to check a browser's Do Not Track status?
+12.4k Golang : md5 hash of a string
+7.7k Golang : Rename part of filename
+21.4k Golang : Convert(cast) string to rune and back to string example
+6.3k Golang : Convert Chinese UTF8 characters to Pin Yin
+12.7k Elastic Search : Return all records (higher than default 10)
+14.3k Golang : Compress and decompress file with compress/flate example