0
public func createSecureRandomKey(numberOfBits: Int) -> Any {
    let attributes: [String: Any] =
        [kSecAttrKeyType as String:CFString.self,
         kSecAttrKeySizeInBits as String:numberOfBits]

    var error: Unmanaged<CFError>?
    guard let privateKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {
        return ""
    }
    return privateKey
}

I am trying to create Secure random number like above way, but returning nothing, Could any one please help me. Thanks.

Satish
  • 133
  • 11

1 Answers1

2

It looks like you are using the wrong function. With your function you are generating a new key. But as your title says you want to generate secure random numbers. For this there is a function called: SecRandomCopyBytes(::_:)

Here is a code snippet taken from the official apple documentation how to use it:

var bytes = [Int8](repeating: 0, count: 10)
let status = SecRandomCopyBytes(kSecRandomDefault, bytes.count, &bytes)

if status == errSecSuccess { // Always test the status.
    print(bytes)
    // Prints something different every time you run.
}

Source: Apple doc

Biba
  • 1,595
  • 1
  • 12
  • 17