Data
extension Data
extension Data: XDRCodable
Extension providing base32 encoding and decoding for Data.
Supports both standard base32 (RFC 4648) and base32hex variants. Used extensively in Stellar’s StrKey encoding for account addresses and keys.
-
Encodes data to a base32 string.
Uses the standard base32 alphabet (A-Z, 2-7).
Example:
let data = Data([72, 101, 108, 108, 111]) let encoded = data.base32EncodedString // "JBSWY3DP"Declaration
Swift
public var base32EncodedString: String { get } -
Encodes data to base32 format as Data.
Returns the base32-encoded string as UTF-8 data.
Example:
let data = Data([72, 101, 108, 108, 111]) let encodedData = data.base32EncodedDataDeclaration
Swift
public var base32EncodedData: Data { get } -
Decodes base32-encoded data.
Assumes the data contains a UTF-8 encoded base32 string.
Example:
let encoded = "JBSWY3DP".data(using: .utf8)! if let decoded = encoded.base32DecodedData { // Use decoded data }Declaration
Swift
public var base32DecodedData: Data? { get }Return Value
Decoded data, or nil if decoding fails
-
Encodes data to a base32hex string.
Uses the extended hex alphabet (0-9, A-V).
Example:
let data = Data([72, 101, 108, 108, 111]) let encoded = data.base32HexEncodedStringDeclaration
Swift
public var base32HexEncodedString: String { get } -
Encodes data to base32hex format as Data.
Returns the base32hex-encoded string as UTF-8 data.
Declaration
Swift
public var base32HexEncodedData: Data { get } -
Decodes base32hex-encoded data.
Assumes the data contains a UTF-8 encoded base32hex string.
Declaration
Swift
public var base32HexDecodedData: Data? { get }Return Value
Decoded data, or nil if decoding fails
-
Options for base16 encoding.
See moreDeclaration
Swift
enum Base16EncodingOptions -
Encodes data to a base16 (hexadecimal) string.
Declaration
Swift
func base16EncodedString(options: [Base16EncodingOptions] = []) -> StringParameters
optionsEncoding options
Return Value
Hexadecimal string representation
-
Encodes data to base16 format as Data (UTF-8 bytes of hex string).
Declaration
Swift
func base16EncodedData(options: [Base16EncodingOptions] = []) -> DataParameters
optionsEncoding options
Return Value
UTF-8 encoded hexadecimal string as Data
-
Creates data from a base16 (hexadecimal) encoded string.
Throws
Base16EncodingError if the string is invalidDeclaration
Swift
init(base16Encoded string: String) throwsParameters
stringHexadecimal string (even length, case insensitive)
-
Creates data from base16 (hexadecimal) encoded UTF-8 Data.
Throws
Base16EncodingError if the data is invalidDeclaration
Swift
init(base16Encoded data: Data) throwsParameters
dataUTF-8 encoded hexadecimal string as Data
-
Returns a Base64URL-encoded string (RFC 4648 §5, no padding).
Declaration
Swift
func base64URLEncodedString() -> String -
Creates data from a Base64URL-encoded string.
Accepts input with or without trailing
=padding. Re-pads internally and delegates to the platform Base64 decoder.Throws
Base64URLEncodingError.invalidInputwhen the string is not valid Base64URL.Declaration
Swift
init(base64URLEncoded string: String) throwsParameters
stringBase64URL-encoded string (with or without
=padding). -
Compares two byte sequences in constant time.
Always inspects every byte of both sequences regardless of where the first mismatch occurs, preventing an attacker from inferring partial match length by measuring execution time. The length-difference indicator is stored as a Boolean flag (0 or 1) rather than a narrowed XOR of the lengths, keeping the implementation trap-free for any input sizes and avoiding the edge case where two different-length inputs could produce a zero-difference accumulator through integer overflow truncation.
Declaration
Swift
func constantTimeEquals(_ other: Data) -> BoolParameters
otherThe byte sequence to compare against.
Return Value
truewhen both sequences have identical length and byte contents. -
Computes the SHA-256 hash of the data.
Declaration
Swift
var sha256Hash: Data { get } -
Encodes data to strkey ed25519 public key (“G…”).
Declaration
Swift
public func encodeEd25519PublicKey() throws -> String -
Encodes data to strkey ed25519 seed (“S…”).
Declaration
Swift
public func encodeEd25519SecretSeed() throws -> String -
Encodes data to strkey med25519 public key. (“M…”)
Declaration
Swift
public func encodeMEd25519AccountId() throws -> String -
Encodes data to strkey preAuthTx. (“T…”)
Declaration
Swift
public func encodePreAuthTx() throws -> String -
Encodes data to strkey sha256 hash. (“X…”)
Declaration
Swift
public func encodeSha256Hash() throws -> String -
Encodes raw data to strkey signed payload (“P…”).
Declaration
Swift
public func encodeSignedPayload() throws -> String -
Encodes raw data to strkey contract id (“C…”).
Declaration
Swift
public func encodeContractId() throws -> String -
Encodes raw data to strkey claimable balance (“B…”).
Declaration
Swift
public func encodeClaimableBalanceId() throws -> String -
Encodes raw data to strkey liquidity pool id (“L…”).
Declaration
Swift
public func encodeLiquidityPoolId() throws -> String -
Encodes raw data representing a MuxedAccountXDR to strkey muxed account id (“M…”).
Declaration
Swift
public func encodeMuxedAccount() throws -> String -
Encodes data to XDR format with automatic padding.
Encodes the byte count followed by the bytes, padded to a 4-byte boundary.
Declaration
Swift
public func xdrEncode(to encoder: XDREncoder) throws -
Encodes data to XDR format without length prefix (fixed-size encoding).
Used for fixed-size byte arrays where the length is known in advance.
Declaration
Swift
public func xdrEncodeFixed(to encoder: XDREncoder) throws -
Decodes variable-length data from XDR format.
Reads the byte count, then the bytes, consuming any padding bytes.
Declaration
Swift
public init(fromBinary decoder: XDRDecoder) throws -
Decodes fixed-size data from XDR format.
Reads exactly the specified number of bytes without a length prefix.
Declaration
Swift
public init(fromBinary xdrDecoder: XDRDecoder, count: Int) throwsParameters
xdrDecoderDecoder to read from
countNumber of bytes to read
View on GitHub
Install in Dash