Usage | ssl-certificate-go | Spatie       

 SPATIE  

ssl-certificate-go
==================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Ssl-certificate-go](https://spatie.be/docs/ssl-certificate-go/v1)  Usage

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/ssl-certificate-go/v1) 

  Usage    
- [ Introduction ](https://spatie.be/docs/ssl-certificate-go/v1/introduction)
- [ Installation and setup ](https://spatie.be/docs/ssl-certificate-go/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/ssl-certificate-go/v1/usage)
- [ Downloading certificates ](https://spatie.be/docs/ssl-certificate-go/v1/downloading-certificates)
- [ Testing ](https://spatie.be/docs/ssl-certificate-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/ssl-certificate-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/ssl-certificate-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/ssl-certificate-go/v1/support-us)

 Usage
=====

You can create a certificate with this constructor:

```
certificate, err := sslcertificate.ForHost(ctx, "spatie.be")
if err != nil {
    return err
}
```

You can pass the port with the same constructor:

```
certificate, err := sslcertificate.ForHost(ctx, "spatie.be:8443")
```

Configure a downloader to specify a port or check the certificate on a different IP address. IPv4 and IPv6 are supported. The host name still controls SNI and peer name verification.

```
downloader := sslcertificate.NewDownloader()
downloader.Port = 8443
downloader.IPAddress = "192.0.2.1"
certificate, err := downloader.ForHost(ctx, "spatie.be")
```

For files or in-memory data, use `FromFile(path)`, `FromPEM(data)` or `FromDER(data)`. PEM input uses the first certificate block. DER input must contain exactly one certificate. All constructors return `(*Certificate, error)` rather than a boolean.

### Getting the issuer name

```
certificate.Issuer() // "Let's Encrypt Authority X3"
```

### Getting the domain name

Returns the primary domain name for the certificate. If the subject has repeated common names, the first is used, matching PHP.

```
certificate.Domain() // "spatie.be"
```

### Getting the certificate's signing algorithm

Returns the algorithm used for signing the certificate. Names follow Go's `crypto/x509`, such as `SHA256-RSA`, rather than OpenSSL's `RSA-SHA256`.

```
certificate.SignatureAlgorithm()
```

### Getting the certificate's organization

Returns the organization belonging to the certificate's issuer.

```
certificate.Organization()
```

### Getting the subject's organization

Returns the organization of the certificate's subject.

```
certificate.SubjectOrganization()
```

Common name and organization getters return an empty string when absent. Organization getters use the first organization if more than one exists.

### Getting the additional domain names

A certificate can cover multiple (sub)domains. Here's how to get them.

```
certificate.AdditionalDomains() // []string{"spatie.be", "www.spatie.be"}
```

A name can start with `*.`, meaning it covers one subdomain label, not the root or deeper subdomains. Alternative names retain certificate order. IP addresses have an `IP Address:` prefix, emails have `email:` and URIs have `URI:`. Emails and URIs never match host names. `Domains()` combines these names with the primary domain and removes duplicates.

### Getting the fingerprint

```
certificate.Fingerprint()
```

Returns the lowercase SHA-1 digest of the original DER, without separators. Do not use SHA-1 for authentication.

### Getting the SHA256 fingerprint

```
certificate.FingerprintSHA256()
```

Returns the lowercase SHA-256 digest of the original DER, without separators.

### Getting the date when the certificate becomes valid

```
certificate.ValidFromDate() // time.Time in UTC
```

### Getting the expiration date

```
certificate.ExpirationDate() // time.Time in UTC
```

`LifespanInDays()` returns the whole 24-hour days between the validity dates. `DaysUntilExpirationDate()` returns signed whole days until expiration, truncating toward zero. For example, 49 hours in the past returns -2. Long-lived certificates are not limited by `time.Duration`'s roughly 292-year range.

### Determining if the certificate is still valid

Returns true if the current date and time is between `ValidFromDate` and `ExpirationDate`, including both boundaries.

```
certificate.IsValid("")
```

You can also determine if a given domain is covered by the certificate. It keeps checking the dates.

```
certificate.IsValid("spatie.be")
certificate.IsValid("laravel.com")
```

This does not verify the chain of trust. Downloads verify trust separately. `AppliesToURL(input)` checks coverage without dates. It accepts bare host names, URLs and IP addresses. It includes the common name even if SANs exist, matching PHP's metadata queries. The TLS handshake follows Go's stricter SAN-only rules. An IP address needs a literal IP match. A DNS wildcard never covers an IP address. Malformed inputs return false. International names are converted to ASCII and DNS comparisons are case-insensitive.

### Determining if the certificate is still valid until a given date

Returns true if the certificate is currently valid and expiration is strictly after the given date.

```
certificate.IsValidUntil(time.Now().Add(7*24*time.Hour), "")
```

For deterministic checks, use `IsValidAt(now, url)`, `IsValidUntilAt(now, date, url)`, `IsExpiredAt(now)` and `DaysUntilExpirationDateAt(now)`. These avoid a mutable global clock.

### Determining if the certificate is expired

```
certificate.IsExpired()
```

Expiration must be strictly before the current time.

### Convert the certificate to an array

In Go, use a typed `Properties` value instead of a PHP array:

```
properties := certificate.Properties()
restored, err := sslcertificate.FromProperties(properties)
```

Properties retain DER and the optional remote socket address. JSON encoding stores DER as base64. Both the properties and certificate support `encoding/json`. Decoding parses the DER again rather than trusting cached metadata.

```
data, err := json.Marshal(certificate)
if err != nil {
    return err
}
var restored sslcertificate.Certificate
if err := json.Unmarshal(data, &restored); err != nil {
    return err
}
```

This format is not compatible with PHP serialization or OpenSSL arrays. DER, properties and metadata accessors return independent copies.

### Other certificate properties

`SerialNumber()` returns a decimal string. `PublicKeyAlgorithm()` returns the key algorithm, such as RSA, EC or Ed25519. `PublicKeySize()` returns its size in bits. Algorithms unsupported by Go cannot be parsed merely by disabling verification.

`RawCertificateFields()` returns a `*x509.Certificate`, including the original extensions. `RawCertificateFieldsJSON()` returns that metadata as JSON with an error. `String()` returns the JSON as a string. This is Go-native metadata, not PHP's OpenSSL field map. `Hash()` is an MD5 identifier of this JSON, not a security digest or a stable identifier across Go versions. Prefer fingerprints for cross-application comparisons.

`ContainsDomain(domain)` checks literal, case-sensitive membership and subdomains of an entry. It treats `*` literally. Use `AppliesToURL` for wildcard coverage.

`IsSelfSigned()` compares issuer and subject common names, matching PHP's heuristic. It does not verify a signature. `UsesSHA1Hash()` detects SHA-1 signature algorithms. `IsPreCertificate()` checks for the CT poison extension.

`RemoteAddress()` returns the peer socket address, or an empty string for local input. `DER()` and `PEM()` return the original certificate. `DERToPEM(der)` wraps DER bytes in a PEM envelope without parsing them.

PHP's runtime macros and subclasses become ordinary Go functions, composition and consumer-defined interfaces. No model, job or profile classes exist in this package. Downloads expose `DialContext` and `TLSConfig` for transport customization.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
