Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to be able to change the password of a disk image, but fully automated. Is there a way to enter a string as a password instead of plain text? Or can I have a .txt file with nothing but the password, and somehow import that into the password field? Thanks very much.

share|improve this question
up vote 2 down vote accepted

You did not mention what tool you are using to effect the change of passphrase, but from a shell command line, hdiutil probably makes the most sense.

Its chpass subcommand will let you change the passphrase of an encrypted disk image. It can be told to read the old and new passphrases from stdin. However, it requires that the passphrases be presented in a “NUL terminated” format, which is difficult to accomplish when typing them directly. You can use the printf command to create the required format though:

printf '%s\0' '01d Pa55w0rD' 'n3w p4ssWoRd' | 
  hdiutil chpass /path/to/encrypted.dmg -oldstdinpass -newstdinpass

The passphrases should be given in the same order as the -oldstdinpass and -newstdinpass parameters (in the example above, the -oldstdinpass parameter and the old passphrase itself come first in their respective contexts).

Note: If you are in a multi-user environment there is a chance that another user could spot your passphrases by watching the list of processes and their associated command lines (e.g. the printf command and its argument (thus your passphrases) might show up in the output of ps -ef). This is less likely if you are using a shell where printf is a built into the shell itself (e.g. ksh, bash, zsh do not require using a separate process for the external printf command).

share|improve this answer

The expect command can watch and type for you. Whatever you want is possible, storing ssh hashes, scripting the keychain, variables. You can be as tricky or transparent as you are creative.

share|improve this answer
where's a good place to learn to do this? – JShoe Jul 30 '11 at 1:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.