iOS Developer Library

Developer

Security Framework Reference Randomization Services Reference

Options
Deployment Target:

On This Page
Language:

Randomization Services Reference

Randomization Services is an API that generates cryptographically secure random numbers.

Functions

  • Generates an array of cryptographically secure random bytes.

    Declaration

    Swift

    func SecRandomCopyBytes(_ rnd: SecRandomRef, _ count: Int, _ bytes: UnsafeMutablePointer<UInt8>) -> Int32

    Objective-C

    int SecRandomCopyBytes ( SecRandomRef rnd, size_t count, uint8_t *bytes );

    Parameters

    rnd

    The random number generator object to use. Specify kSecRandomDefault to use the default random number generator.

    count

    The number of random bytes to return in the array pointed to by the bytes parameter.

    bytes

    The random bytes generated by the function.

    Return Value

    Returns 0 if the function completed successfully and -1 if there was an error. Check the errno system variable for the error.

    Discussion

    This function reads from /dev/random to obtain an array of cryptographically-secure random bytes. For more information on the /dev/random random-number generator, see the manual page for random(4).

    Availability

    Available in iOS 2.0 and later.

Data Types

  • Abstract Core Foundation-type object containing information about a random number generator.

    Declaration

    Swift

    typealias SecRandomRef = COpaquePointer

    Objective-C

    typedef const struct __SecRandom * SecRandomRef;

    Import Statement

    Objective-C

    @import Security;

    Swift

    import Security

    Availability

    Available in iOS 2.0 and later.

Constants

  • Indicates the default random number generator.

    Declaration

    Swift

    let kSecRandomDefault: SecRandomRef

    Objective-C

    const SecRandomRef kSecRandomDefault;

    Constants

    • kSecRandomDefault

      kSecRandomDefault

      When passed to the SecRandomCopyBytes function as the random number generator reference, this constant indicates that the default number generator should be used.

      This constant is a synonym for NULL.

      Available in iOS 2.0 and later.