genpw, in Swift
75d16d1d — Mark Cornick 3 months ago
Note that universal builds require Swift 5.3
8c0690ac — Mark Cornick 3 months ago
Add universal build instructions
40ec7351 — Mark Cornick 3 months ago
Add short options


browse  log 



You can also use your local clone with git send-email.

#genpw (in Swift)

This is a port of my genpw program from Go to Swift. I'm doing this to learn some Swift, not to provide a 1:1 port. Nevertheless, aside from a few differences in command line flags, it now has feature parity with the Go version.

Passwords are, by default, chosen from the union of three character classes: upper-case letters, lower-case letters, and digits.

(Previous versions did not use 0, O, I, l, or 1; this was also the behavior of the Go version. This was to avoid confusion when passwords are displayed in sans-serif fonts. I no longer consider that important, so this version uses all letters and digits.)

Options can be given to omit any one or any two of these character classes. For instance, you can omit uppercase letters and digits by passing --no-upper --no-digit to genpw. This will return a password composed only of lowercase letters.

Passwords are guaranteed to contain at least one of each selected character class. The default length is 16. genpw will create a password of any length greater than or equal to the number of character classes selected.

If the password length is less than or equal to the total number of characters selected, genpw will not repeat characters within the generated password.


This package targets Swift 5.2/Xcode 11 and higher. To build universal macOS binaries, Swift 5.3/Xcode 12 is required.

On Linux, make sure you've installed Swift 5.2 or higher from the Swift download site.

#Single-architecture build

Run swift build -c release in the top level directory. Copy the generated .build/release/genpw binary to wherever you like.

#Universal build

Run swift build -c release --arch arm64 --arch x86_64 in the top level directory. Copy the generated .build/apple/Products/Release/genpw binary to wherever you like.


$ genpw --help
USAGE: genpw [--length <length>] [--no-upper] [--no-lower] [--no-digit]

  -l, --length <length>   Length to generate. (default: 16)
  -U, --no-upper          Exclude uppercase letters.
  -u, --no-lower          Exclude lowercase letters.
  -d, --no-digit          Exclude digits.
  -h, --help              Show help information.

$ genpw
$ genpw --length 8
$ genpw --length 64
$ genpw --length 0
Error: Length must be at least 3.
$ genpw --no-lower
$ genpw --no-upper --length 8
$ genpw --no-lower --no-upper --length 32
$ genpw --no-lower --no-upper --no-digit
Error: Cannot exclude all three character classes.


genpw is available as open source under the terms of the MIT License.