Skip to content

Instantly share code, notes, and snippets.

@RodolfoAntonici
Last active May 8, 2017 14:51
Show Gist options
  • Save RodolfoAntonici/c13dbb7168c7db193b27d38ad3680db1 to your computer and use it in GitHub Desktop.
Save RodolfoAntonici/c13dbb7168c7db193b27d38ad3680db1 to your computer and use it in GitHub Desktop.
//Based on: https://github.com/nortthon/CPF-CNPJ-Swift
func generateCPF() -> String {
var cpf = [0,0,0,0,0,0,0,0,0,0,0]
var firstVerification = 0
var secondVerification = 0
for i in 0...8 {
cpf[i] = (Int)(arc4random_uniform(9))
firstVerification += cpf[i] * (10 - i)
secondVerification += cpf[i] * (11 - i)
}
firstVerification %= 11
cpf[9] = firstVerification < 2 ? 0 : 11 - firstVerification
secondVerification += cpf[9] * 2
secondVerification %= 11
cpf[10] = secondVerification < 2 ? 0 : 11-secondVerification
return "\(cpf[0])\(cpf[1])\(cpf[2])\(cpf[3])\(cpf[4])\(cpf[5])\(cpf[6])\(cpf[7])\(cpf[8])\(cpf[9])\(cpf[10])"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment