Example #1
0
def encrypt(fp, s, output=None, fn=None):
    r"""
    >>> encrypt(shash('randomid'), "Goodbye, cruel world!")[:75]
    '-----BEGIN PGP MESSAGE-----\nVersion: GnuPG/MacGPG2 v2.0.17 (Darwin)\n\nhQIMA3'
    """
    if output:
        store.verify(output)
    fp = fp.replace(' ', '')
    if isinstance(s, unicode):
        s = s.encode('utf8')
    if isinstance(s, str):
        out = gpg.encrypt(s, [fp], output=output, always_trust=True)
    else:
        if fn:
            with _gpghacklock:
                oldname = gpg.gpgbinary
                gpg.gpgbinary += ' --set-filename ' + _shquote(fn)
                out = gpg.encrypt_file(s, [fp], output=output, always_trust=True)
                gpg.gpgbinary = oldname
        else:
            out = gpg.encrypt_file(s, [fp], output=output, always_trust=True)
    if out.ok:
        return out.data
    else:
        raise CryptoException(out.stderr)
Example #2
0
def encrypt(plaintext, fingerprints, output=None):
    # Verify the output path
    if output:
        store.verify(output)

    # Remove any spaces from provided fingerprints
    # GPG outputs fingerprints with spaces for readability, but requires the
    # spaces to be removed when using fingerprints to specify recipients.
    if not isinstance(fingerprints, (list, tuple)):
        fingerprints = [fingerprints, ]
    fingerprints = [fpr.replace(' ', '') for fpr in fingerprints]

    if isinstance(plaintext, unicode):
        plaintext = plaintext.encode('utf8')

    encrypt_fn = gpg.encrypt if isinstance(plaintext, str) else gpg.encrypt_file
    out = encrypt_fn(plaintext,
                     *fingerprints,
                     output=output,
                     always_trust=True,
                     armor=False)
    if out.ok:
        return out.data
    else:
        raise CryptoException(out.stderr)
Example #3
0
    def test_verify_invalid_file_extension_in_sourcedir_raises_exception(self):
        source_directory, file_path = self.create_file_in_source_dir(
            'example-filesystem-id', 'not_valid.txt'
        )

        with self.assertRaisesRegexp(
                store.PathException,
                'Invalid file extension .txt'):
            store.verify(file_path)

        shutil.rmtree(source_directory)  # Clean up created files
Example #4
0
    def test_verify_flagged_file_in_sourcedir_returns_true(self):
        source_directory, file_path = self.create_file_in_source_dir(
            'example-filesystem-id', '_FLAG'
        )

        self.assertTrue(store.verify(file_path))

        shutil.rmtree(source_directory)  # Clean up created files
Example #5
0
def encrypt(fp, s, output=None):
    r"""
    >>> key = genkeypair('randomid', 'randomid')
    >>> encrypt('randomid', "Goodbye, cruel world!")[:45]
    '-----BEGIN PGP MESSAGE-----\nVersion: GnuPG v2'
    """
    if output:
        store.verify(output)
    fp = fp.replace(' ', '')
    if isinstance(s, unicode):
        s = s.encode('utf8')
    if isinstance(s, str):
        out = gpg.encrypt(s, [fp], output=output, always_trust=True)
    else:
        out = gpg.encrypt_file(s, [fp], output=output, always_trust=True)
    if out.ok:
        return out.data
    else:
        raise CryptoException(out.stderr)
Example #6
0
def encrypt(fp, s, output=None):
    r"""
    >>> key = genkeypair('randomid', 'randomid')
    >>> encrypt('randomid', "Goodbye, cruel world!")[:45]
    '-----BEGIN PGP MESSAGE-----\nVersion: GnuPG v2'
    """
    if output:
        store.verify(output)
    fp = fp.replace(' ', '')
    if isinstance(s, unicode):
        s = s.encode('utf8')
    if isinstance(s, str):
        out = gpg.encrypt(s, [fp], output=output, always_trust=True)
    else:
        out = gpg.encrypt_file(s, [fp], output=output, always_trust=True)
    if out.ok:
        return out.data
    else:
        raise CryptoException(out.stderr)
Example #7
0
def encrypt(plaintext, fingerprints, output=None):
    # Verify the output path
    if output:
        store.verify(output)

    if not isinstance(fingerprints, (list, tuple)):
        fingerprints = [fingerprints, ]
    # Remove any spaces from provided fingerprints GPG outputs fingerprints
    # with spaces for readability, but requires the spaces to be removed when
    # using fingerprints to specify recipients.
    fingerprints = [fpr.replace(' ', '') for fpr in fingerprints]

    if not _is_stream(plaintext):
        plaintext = _make_binary_stream(plaintext, "utf_8")

    out = gpg.encrypt(plaintext,
                      *fingerprints,
                      output=output,
                      always_trust=True,
                      armor=False)
    if out.ok:
        return out.data
    else:
        raise CryptoException(out.stderr)
Example #8
0
def encrypt(plaintext, fingerprints, output=None):
    # Verify the output path
    if output:
        store.verify(output)

    if not isinstance(fingerprints, (list, tuple)):
        fingerprints = [fingerprints, ]
    # Remove any spaces from provided fingerprints GPG outputs fingerprints
    # with spaces for readability, but requires the spaces to be removed when
    # using fingerprints to specify recipients.
    fingerprints = [fpr.replace(' ', '') for fpr in fingerprints]

    if not _is_stream(plaintext):
        plaintext = _make_binary_stream(plaintext, "utf_8")

    out = gpg.encrypt(plaintext,
                      *fingerprints,
                      output=output,
                      always_trust=True,
                      armor=False)
    if out.ok:
        return out.data
    else:
        raise CryptoException(out.stderr)
Example #9
0
def secureunlink(fn):
    store.verify(fn)
    return subprocess.check_call(['srm', fn])
Example #10
0
def secureunlink(fn):
    store.verify(fn)
    return subprocess.check_call(['srm', fn])
Example #11
0
 def test_verify(self):
     with self.assertRaises(store.PathException):
         store.verify(os.path.join(config.STORE_DIR, '..', 'etc', 'passwd'))
Example #12
0
 def test_verify_in_store_dir(self):
     with self.assertRaisesRegexp(store.PathException, 'Invalid directory'):
         store.verify(config.STORE_DIR + "_backup")
Example #13
0
 def test_verify_path_not_absolute(self):
     with self.assertRaises(store.PathException):
         store.verify(os.path.join(config.STORE_DIR, '..', 'etc', 'passwd'))
Example #14
0
 def test_verify(self):
     with self.assertRaises(store.PathException):
         store.verify(os.path.join(config.STORE_DIR, '..', 'etc', 'passwd'))
     with self.assertRaises(store.PathException):
         store.verify(config.STORE_DIR + "_backup")