Skip to content

Commit

Permalink
Remove deprecated package 'ioutil'
Browse files Browse the repository at this point in the history
  • Loading branch information
deining committed Nov 17, 2024
1 parent 1c1dc4e commit cc105aa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
21 changes: 10 additions & 11 deletions cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"io/ioutil"
"log"
"math/big"
"net"
Expand Down Expand Up @@ -113,19 +112,19 @@ func (m *mkcert) makeCert(hosts []string) {
privPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privDER})

if certFile == keyFile {
err = ioutil.WriteFile(keyFile, append(certPEM, privPEM...), 0600)
err = os.WriteFile(keyFile, append(certPEM, privPEM...), 0600)
fatalIfErr(err, "failed to save certificate and key")
} else {
err = ioutil.WriteFile(certFile, certPEM, 0644)
err = os.WriteFile(certFile, certPEM, 0644)
fatalIfErr(err, "failed to save certificate")
err = ioutil.WriteFile(keyFile, privPEM, 0600)
err = os.WriteFile(keyFile, privPEM, 0600)
fatalIfErr(err, "failed to save certificate key")
}
} else {
domainCert, _ := x509.ParseCertificate(cert)
pfxData, err := pkcs12.Encode(rand.Reader, priv, domainCert, []*x509.Certificate{m.caCert}, "changeit")
fatalIfErr(err, "failed to generate PKCS#12")
err = ioutil.WriteFile(p12File, pfxData, 0644)
err = os.WriteFile(p12File, pfxData, 0644)
fatalIfErr(err, "failed to save PKCS#12")
}

Expand Down Expand Up @@ -211,7 +210,7 @@ func (m *mkcert) makeCertFromCSR() {
log.Fatalln("ERROR: can't create new certificates because the CA key (rootCA-key.pem) is missing")
}

csrPEMBytes, err := ioutil.ReadFile(m.csrPath)
csrPEMBytes, err := os.ReadFile(m.csrPath)
fatalIfErr(err, "failed to read the CSR")
csrPEM, _ := pem.Decode(csrPEMBytes)
if csrPEM == nil {
Expand Down Expand Up @@ -267,7 +266,7 @@ func (m *mkcert) makeCertFromCSR() {
}
certFile, _, _ := m.fileNames(hosts)

err = ioutil.WriteFile(certFile, pem.EncodeToMemory(
err = os.WriteFile(certFile, pem.EncodeToMemory(
&pem.Block{Type: "CERTIFICATE", Bytes: cert}), 0644)
fatalIfErr(err, "failed to save certificate")

Expand All @@ -284,7 +283,7 @@ func (m *mkcert) loadCA() {
m.newCA()
}

certPEMBlock, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootName))
certPEMBlock, err := os.ReadFile(filepath.Join(m.CAROOT, rootName))
fatalIfErr(err, "failed to read the CA certificate")
certDERBlock, _ := pem.Decode(certPEMBlock)
if certDERBlock == nil || certDERBlock.Type != "CERTIFICATE" {
Expand All @@ -297,7 +296,7 @@ func (m *mkcert) loadCA() {
return // keyless mode, where only -install works
}

keyPEMBlock, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootKeyName))
keyPEMBlock, err := os.ReadFile(filepath.Join(m.CAROOT, rootKeyName))
fatalIfErr(err, "failed to read the CA key")
keyDERBlock, _ := pem.Decode(keyPEMBlock)
if keyDERBlock == nil || keyDERBlock.Type != "PRIVATE KEY" {
Expand Down Expand Up @@ -352,11 +351,11 @@ func (m *mkcert) newCA() {

privDER, err := x509.MarshalPKCS8PrivateKey(priv)
fatalIfErr(err, "failed to encode CA key")
err = ioutil.WriteFile(filepath.Join(m.CAROOT, rootKeyName), pem.EncodeToMemory(
err = os.WriteFile(filepath.Join(m.CAROOT, rootKeyName), pem.EncodeToMemory(
&pem.Block{Type: "PRIVATE KEY", Bytes: privDER}), 0400)
fatalIfErr(err, "failed to save CA key")

err = ioutil.WriteFile(filepath.Join(m.CAROOT, rootName), pem.EncodeToMemory(
err = os.WriteFile(filepath.Join(m.CAROOT, rootName), pem.EncodeToMemory(
&pem.Block{Type: "CERTIFICATE", Bytes: cert}), 0644)
fatalIfErr(err, "failed to save CA certificate")

Expand Down
3 changes: 1 addition & 2 deletions truststore_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -59,7 +58,7 @@ func (m *mkcert) installPlatform() bool {
return false
}

cert, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootName))
cert, err := os.ReadFile(filepath.Join(m.CAROOT, rootName))
fatalIfErr(err, "failed to read root certificate")

cmd := commandWithSudo("tee", m.systemTrustFilename())
Expand Down

0 comments on commit cc105aa

Please sign in to comment.