gpg: Factor out code into a standalone function.