38 lines
800 B
Go
38 lines
800 B
Go
package main
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/x509"
|
|
|
|
"software.sslmate.com/src/go-pkcs12"
|
|
)
|
|
|
|
const passChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*?"
|
|
|
|
func generatePassword(n int) (string, error) {
|
|
bytes := make([]byte, n)
|
|
_, err := rand.Read(bytes)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
for i, b := range bytes {
|
|
bytes[i] = passChars[int(b)%len(passChars)]
|
|
}
|
|
return string(bytes), nil
|
|
}
|
|
|
|
func generatePKCS12(pKey any, cert *x509.Certificate, caCerts []*x509.Certificate) (string, []byte, error) {
|
|
pfxPass, err := generatePassword(50)
|
|
if err != nil {
|
|
return "Failed to create password", nil, err
|
|
}
|
|
|
|
pfxData, err := pkcs12.Modern.Encode(pKey, cert, caCerts, pfxPass)
|
|
if err != nil {
|
|
return "", nil, err
|
|
} else {
|
|
return pfxPass, pfxData, nil
|
|
}
|
|
}
|