Password Generator for Swift
Free online password generator with Swift code examples
Working with password generator in Swift? Our free online password generator helps Swift developers format, validate, and process data instantly. Below you will find Swift code examples using Security / CryptoKit (built-in) so you can achieve the same result programmatically in your own projects.
Try the Password Generator Online
Use our free Password Generator directly in your browser — no setup required.
Open Password GeneratorSwift Code Example
import Foundation
func generatePassword(length: Int = 20) -> String {
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*"
var password = ""
var randomBytes = [UInt8](repeating: 0, count: length)
_ = SecRandomCopyBytes(kSecRandomDefault, length, &randomBytes)
for byte in randomBytes {
password.append(chars[chars.index(chars.startIndex, offsetBy: Int(byte) % chars.count)])
}
return password
}
print(generatePassword(length: 20))Quick Setup
Library: Security / CryptoKit (built-in)
// Built-in frameworks — no installation neededSwift Tips & Best Practices
- SecRandomCopyBytes uses the OS cryptographic random generator
- Available on all Apple platforms
- Never use Int.random(in:) for security-sensitive randomness