Сертификаты в Go

Содержание
Введение
Генерация pem
Похожие статьи

Введение

Обычно сертификаты генерируются с помощью инструментов вроде OpenSSL.

У этого способа есть ограничения, связанные с возможностью инструментов. Обойти их можно создав сертификат программно, например на Go.

Создать сертификат

Если нужно сгенерировать сертификат с определёнными значениям NotBefore и NotAfter

package main import ( "bytes" "crypto/rand" "crypto/rsa" "crypto/x509" "crypto/x509/pkix" "encoding/pem" "io/ioutil" "math/big" "time" ) func main() { template := x509.Certificate{ SerialNumber: big.NewInt(time.Now().Unix()), Subject: pkix.Name{Organization: []string{"localhost"}}, NotBefore: time.Now().AddDate(1, 0, 1), NotAfter: time.Now().AddDate(2, 1, 0), KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, DNSNames: []string{"localhost"}, } privatekey, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { panic(err) } crt, err := x509.CreateCertificate(rand.Reader, &template, &template, &privatekey.PublicKey, privatekey) if err != nil { panic(err) } var certOut, keyOut bytes.Buffer pem.Encode(&certOut, &pem.Block{Type: "CERTIFICATE", Bytes: crt}) pem.Encode(&keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privatekey)}) ioutil.WriteFile("/tmp/key", keyOut.Bytes(), 0644) ioutil.WriteFile("/tmp/pem", certOut.Bytes(), 0644) }

Article author: Andrei Olegovich

Похожие статьи
Программирование на Go
Объявление переменных
Определить тип переменной
Указатели
Константы
Сертификаты
Запросы к REST API на Go
Установка в Linux

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: