I've written this class to generate a random password and was wondering if there's anything I could improve upon here and whether or not this a safe (most random) method of generating a password.
There's only one function in here which can be used to create an alphanumerical password of a specified length. It can also be overloaded to be more specific in which characters it will use.
The code works as it's supposed to.
import UIKit
class codeGen: NSObject {
private let base: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
private let lowerChars: String = "abcdefghijklmnopqrstuvwxyz"
private let numberChars: String = "1234567890"
private let specialChars: String = "!@#$%^&*"
func generate(length: Int) -> String
{
let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let len = UInt32(letters.length)
var result = ""
for _ in 0 ..< length {
let rand = arc4random_uniform(len)
var nextChar = letters.character(at: Int(rand))
result += NSString(characters: &nextChar, length: 1) as String
}
return result
}
func generate(length: Int, lowers: Bool, numbers: Bool, specials: Bool) -> String
{
var letters = base
if (lowers == true)
{
letters += lowerChars
}
if (numbers == true)
{
letters += numberChars
}
if (specials == true)
{
letters += specialChars
}
let NSletters : NSString = letters as NSString
let len = UInt32(NSletters.length)
var result = ""
for _ in 0 ..< length {
let rand = arc4random_uniform(len)
var nextChar = NSletters.character(at: Int(rand))
result += NSString(characters: &nextChar, length: 1) as String
}
return result
}
}