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 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 // https://gist.github.com/NikolaiRuhe/eeb135d20c84a7097516

View file

@ -76,6 +76,7 @@ class Password {
private func getAdditionValue(withKey key: String) -> String? { private func getAdditionValue(withKey key: String) -> String? {
return self.additions[key] return self.additions[key]
} }
} }
extension PasswordEntity { extension PasswordEntity {

View file

@ -23,6 +23,6 @@ class FillPasswordTableViewCell: UITableViewCell {
} }
@IBAction func generatePassword(_ sender: UIButton) { @IBAction func generatePassword(_ sender: UIButton) {
contentTextField.text = "4-r4d0m-p455w0rd" contentTextField.text = Utils.randomString(length: 16)
} }
} }