StreamCryptor

public class StreamCryptor

Encrypts or decrypts return results as they become available.

Note

The underlying cipher may be a block or a stream cipher.

Use for large files or network streams.

For small, in-memory buffers Cryptor may be easier to use.

  • Enumerates Cryptor operations

    See more

    Declaration

    Swift

    public enum Operation
  • Enumerates valid key sizes.

    See more

    Declaration

    Swift

    public enum ValidKeySize
  • Maps CommonCryptoOptions onto a Swift struct.

    See more

    Declaration

    Swift

    public struct Options : OptionSet
  • Enumerates available algorithms

    See more

    Declaration

    Swift

    public enum Algorithm
  • The status code resulting from the last method call to this Cryptor. Used to get additional information when optional chaining collapes.

    Declaration

    Swift

    public internal(set) var status: Status { get }

Lifecycle Methods

  • Default Initializer

    Declaration

    Swift

    public init(operation: Operation, algorithm: Algorithm, options: Options, keyBuffer: [UInt8], keyByteCount: Int, ivBuffer: UnsafePointer<UInt8>, ivLength: Int = 0) throws

    Parameters

    operation

    The operation to perform see Operation (Encrypt, Decrypt)

    algorithm

    The algorithm to use see Algorithm (AES, des, tripleDes, cast, rc2, blowfish)

    keyBuffer

    Pointer to key buffer

    keyByteCount

    Number of bytes in the key

    ivBuffer

    Initialization vector buffer

    ivLength

    Length of the ivBuffer

    Return Value

    New StreamCryptor instance.

  • Creates a new StreamCryptor

    Declaration

    Swift

    public convenience init(operation: Operation, algorithm: Algorithm, options: Options, key: [UInt8], iv: [UInt8]) throws

    Parameters

    algorithm

    The algorithm to use see Algorithm (AES, des, tripleDes, cast, rc2, blowfish)

    key

    A byte array containing key data

    iv

    A byte array containing initialization vector

    Return Value

    New StreamCryptor instance.

  • Creates a new StreamCryptor

    Declaration

    Swift

    public convenience init(operation: Operation, algorithm: Algorithm, options: Options, key: String, iv: String) throws

    Parameters

    algorithm

    The algorithm to use see Algorithm (AES, des, tripleDes, cast, rc2, blowfish)

    key

    A string containing key data (will be interpreted as UTF8)

    iv

    A string containing initialization vector data (will be interpreted as UTF8)

    Return Value

    New StreamCryptor instance.

Public Methods

  • Add the contents of an Data buffer to the current encryption/decryption operation.

    Declaration

    Swift

    public func update(dataIn: Data, byteArrayOut: inout [UInt8]) -> (Int, Status)

    Parameters

    dataIn

    The input data

    byteArrayOut

    Output data

    Return Value

    A tuple containing the number of output bytes produced and the status (see Status)

  • Add the contents of an NSData buffer to the current encryption/decryption operation.

    Declaration

    Swift

    public func update(dataIn: NSData, byteArrayOut: inout [UInt8]) -> (Int, Status)

    Parameters

    byteArrayOut

    Output data

    Return Value

    A tuple containing the number of output bytes produced and the status (see Status)

  • Add the contents of a byte array to the current encryption/decryption operation.

    Declaration

    Swift

    public func update(byteArrayIn: [UInt8], byteArrayOut: inout [UInt8]) -> (Int, Status)

    Parameters

    byteArrayOut

    Output data

    Return Value

    A tuple containing the number of output bytes produced and the status (see Status)

  • Add the contents of a string (interpreted as UTF8) to the current encryption/decryption operation.

    Declaration

    Swift

    public func update(stringIn: String, byteArrayOut: inout [UInt8]) -> (Int, Status)

    Parameters

    byteArrayOut

    Output data

    Return Value

    A tuple containing the number of output bytes produced and the status (see Status)

  • Retrieves all remaining encrypted or decrypted data from this cryptor.

    Note

    If the underlying algorithm is an block cipher and the padding option has not been specified and the cumulative input to the cryptor has not been an integral multiple of the block length this will fail with an alignment error.

    Note

    This method updates the status property

    Declaration

    Swift

    public func final(byteArrayOut: inout [UInt8]) -> (Int, Status)

    Parameters

    byteArrayOut

    The output bffer

    Return Value

    a tuple containing the number of output bytes produced and the status (see Status)

Low-level interface

  • Update the buffer

    Declaration

    Swift

    public func update(bufferIn: UnsafeRawPointer, byteCountIn: Int, bufferOut: UnsafeMutablePointer<UInt8>, byteCapacityOut: Int, byteCountOut: inout Int) -> Status

    Parameters

    bufferIn

    Pointer to input buffer

    inByteCount

    Number of bytes contained in input buffer

    bufferOut

    Pointer to output buffer

    outByteCapacity

    Capacity of the output buffer in bytes

    outByteCount

    On successful completion, the number of bytes written to the output buffer

    Return Value

    Status of the update

  • Retrieves all remaining encrypted or decrypted data from this cryptor.

    Note

    If the underlying algorithm is an block cipher and the padding option has not been specified and the cumulative input to the cryptor has not been an integral multiple of the block length this will fail with an alignment error.

    Note

    This method updates the status property

    Declaration

    Swift

    public func final(bufferOut: UnsafeMutablePointer<UInt8>, byteCapacityOut: Int, byteCountOut: inout Int) -> Status

    Parameters

    outByteCapacity

    Capacity of the output buffer in bytes

    outByteCount

    On successful completion, the number of bytes written to the output buffer

    Return Value

    Status of the update

  • Determines the number of bytes that will be output by this Cryptor if inputBytes of additional data is input.

    Declaration

    Swift

    public func getOutputLength(inputByteCount: Int, isFinal: Bool = false) -> Int

    Parameters

    isFinal

    True if buffer to be input will be the last input buffer, false otherwise.

    Return Value

    The final output length