Sodium

struct Sodium

A wrapper for libsodium that on one hand offers convenient access to the libsodium bindings and on the other hand assures that libsodium was initialized before actually invoking methods.

  • Add padding to protect message lengths.

    Warning

    Padding has to be applied to plaintext before encryption.

    Precondition

    0 < blockSize

    Postcondition

    result.count % blockSize = 0

    Declaration

    Swift

    func pad(unpadded: Bytes, blockSize: Int) -> Bytes

    Parameters

    unpadded

    The unpadded plaintext bytes.

    blockSize

    The block size in bytes.

    Return Value

    The padded plaintext bytes.

  • Add padding to protect message lengths.

    Warning

    Padding has to be applied to plaintext before encryption.

    Precondition

    0 < blockSize

    Postcondition

    result.count % blockSize = 0

    Declaration

    Swift

    func unpad(padded: Bytes, blockSize: Int) -> Bytes?

    Parameters

    unpadded

    The unpadded plaintext bytes.

    blockSize

    The block size in bytes.

    Return Value

    The padded plaintext bytes.

  • Converts hex-characters to a byte array.

    Declaration

    Swift

    func hex2bin(_ hex: String, ignore: String? = nil) -> Bytes?

    Parameters

    hex

    The string.

    ignore

    A string containing characters that should be ignored, e.g., this is useful to ignore the colon if hex is 00:11.

    Return Value

    The byte array.

  • Converts a byte array to a hex-encoded string.

    Declaration

    Swift

    func bin2hex(_ bin: Bytes) -> String

    Parameters

    bin

    The byte array.

    Return Value

    The hex-encoded string.

  • Converts hex-characters to a byte array.

    Declaration

    Swift

    func b64decode(_ b64: String, ignore: String? = nil) -> Bytes?

    Parameters

    b64

    The string.

    ignore

    A string containing characters that should be ignored.

    Return Value

    The byte array.

  • Converts a byte array to a Base64-encoded string.

    Declaration

    Swift

    func b64encode(bytes: Bytes) -> String

    Parameters

    bin

    The byte array.

    Return Value

    The Base64-encoded string.