safe.gpg¶
Facilities for interacting with GPG encrypted files.
| author: | Joe Joyce <joe@decafjoe.com> |
|---|---|
| copyright: | Copyright (c) Joe Joyce and contributors, 2016-2019. |
| license: | BSD |
-
safe.gpg.PREFERRED_CIPHER= 'aes256'¶ Name of the cipher to use if unspecified in
GPGFile.save().Type: str
-
exception
safe.gpg.GPGError(message, stdout, stderr)[source]¶ Bases:
exceptions.ExceptionRaised for errors from this module.
-
class
safe.gpg.GPGFile(path)[source]¶ Bases:
objectManage decryption and encryption of a GPG file.
-
_homedir= None¶ Home directory to use for GnuPG calls (i.e. the
--homedirargument). Defaults to ~/.gnupg. This attribute exists to allow tests to tweak the GnuPG environment while running, and is not otherwise used.Type: str
-
_keyid= None¶ Keyid to which the file was encrypted. Populated when
decrypt_to()is called.Type: strorNone(if file is symmetrically encrypted)
-
_password= None¶ Password with which file was encryted. Populated when
decrypt_to()is called.Type: strorNone(if file is asymmetrically encrypted)
-
_symmetric= None¶ Boolean indicating whether the file is symmetrically encrypted. If false, the file is asymmetrically encrypted.
Type: bool
-
KEYID_RE= <_sre.SRE_Pattern object>¶ Regex matching the keyid output string from
gpg --list-packets.Type: re.compile()
-
__init__(path)[source]¶ Instantiate the file wrapper.
Parameters: path (str) – Path to the GPG encrypted file Raise: GPGErrorif file cannot be read
-
decrypt_to(path, password=None)[source]¶ Decrypt file to
pathusingpassword.If decryption is successful, this will cache the password/keyid for use in subsequent calls to
save().Parameters: Raise: GPGErrorif decryption failsReturn type: None
-
save(source, cipher='aes256')[source]¶ Save plaintext file
sourceback to the original path, encrypted.decrypt_to()must be called before calling this method. Certain values needed by this method are cached when a file is decrypted. (Namely, password for symmetrically encrypted files and keyid for asymmetrically encrypted files.)Parameters: - source (str) – Path to file to save
- cipher (str) – Cipher to use for encryption (defaults to
PREFERRED_CIPHER)
Raise: GPGErrorif encryption fails (original encrypted file is left untouched)Return type: None
-
symmetric¶ If true, file is encrypted symmetrically (i.e. with a password).
-
-
safe.gpg.get_gpg_executable()[source]¶ Return GPG executable, raising a
GPGErrorif not found.This will first look for an executable named
gpg2, returning it immediately if found. Ifgpg2does not exist butgpgdoes, this function runsgpg --versionto check the version. If version 2, the absolute path to the executable is returned.Failure to find a GPG2 executable results in a
GPGErrorbeing raised.Raise: GPGErrorif GPG executable is not foundReturns: Absolute path to the GPG executable Return type: str
-
class
safe.gpg.GPGSubprocess(command)[source]¶ Bases:
safe.util.SubprocessConvenience class for running GPG commands.