Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a random hash of length(x)
// HashHash: make some random numbers, randomly.
import UIKit
func newString(length: Int) -> String {
let allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
// uncomment this if you want all numbers
// let allowed = "1234567890"
let allowedCount = UInt32(allowed.characters.count)
var randomString = ""
for _ in (0..<length) {
let randomNumber = Int(arc4random_uniform(allowedCount))
let newCharacter = allowed[allowed.startIndex.advancedBy(randomNumber)]
randomString += String(newCharacter)
}
return randomString
}
// uncommment for two-factor SMS auth style of string (###-###)
// let twoFactorAuth = "\(newString(3))-\(newString(3))"
let hash = newString(6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.