BlueCryptor icon indicating copy to clipboard operation
BlueCryptor copied to clipboard

Can Random.generate() be non-throwing?

Open twostraws opened this issue 8 years ago • 0 comments

When I need to generate some random data for a string, I'd run code like this:

if let randomBytes = try? Random.generate(byteCount: 64) {
    randomString = CryptoUtils.hexString(from: randomBytes)
} else {
    randomString = generateMyOwnRandomStringSomehow()
}

If I'm trying to generate something important, e.g. a salt, the generateMyOwnRandomStringSomehow() is a weak point because it relies on users to have some idea of PRNG techniques. Is it possible to make Random.generate() always return a value, perhaps by implementing a second generation algorithm that can be used if the preferred algorithm fails?

(Note: the answer may well be "No", but I'm following the "if you don't ask, you don't get" approach.")

twostraws avatar Jan 23 '17 19:01 twostraws