<!--
{
  "availability" : [

  ],
  "documentType" : "symbol",
  "framework" : "OpenSSL",
  "identifier" : "/documentation/OpenSSL/Base64URL/encode(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "OpenSSL"
    ],
    "preciseIdentifier" : "s:7OpenSSL9Base64URLO6encodeySS10Foundation4DataVFZ"
  },
  "title" : "encode(_:)"
}
-->

# encode(_:)

Encodes raw bytes using the URL-safe unpadded Base64 alphabet.

```
static func encode(_ data: Data) -> String
```

## Parameters

`data`

The bytes to encode. May be empty; the empty
input produces the empty string.

## Return Value

A Base64URL string consisting solely of characters
from the 64-symbol URL-safe alphabet plus no padding. Round-
tripped via [`decode(_:)`](/documentation/OpenSSL/Base64URL/decode(_:)) to recover the original bytes.

## Discussion

Emits the encoding defined in
[RFC 4648 §5](https://datatracker.ietf.org/doc/html/rfc4648#section-5):
the standard Base64 alphabet with `+` replaced by `-`, `/`
replaced by `_`, and the trailing `=` padding stripped. The
output is safe to drop into URL path segments, query parameter
values, HTTP header fields, JSON string values, JWS/JWT segments,
and filename components without further escaping.

Implementation detail: this is a thin layer over Foundation’s
`Data.base64EncodedString()` followed by alphabet translation and
padding removal. It does not call into `libcrypto`.

> Note: Not constant-time with respect to the input length or
> content. Do not use on side-channel-sensitive material.