Skip to content

Can Random.generate() be non-throwing? #17

Description

@twostraws

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.")

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions