generate random password

This commit is contained in:
Bob Sun 2017-02-11 22:00:04 +08:00
parent 95680138a4
commit 9f3b13f8b0
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
3 changed files with 18 additions and 1 deletions

View file

@ -34,6 +34,22 @@ class Utils {
}
return lastUpdatedTimeString
}
static func randomString(length: Int) -> String {
let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
let len = UInt32(letters.length)
var randomString = ""
for _ in 0 ..< length {
let rand = arc4random_uniform(len)
var nextChar = letters.character(at: Int(rand))
randomString += NSString(characters: &nextChar, length: 1) as String
}
return randomString
}
}
// https://gist.github.com/NikolaiRuhe/eeb135d20c84a7097516