#!/usr/bin/python
#
# Author: Runa Sandvik, <runa.sandvik@gmail.com>
#
# See LICENSE for licensing information.
#
# This script will create a file with a random string, sign the file
# with 'gpg --clearsign' and then save the output in the original file.
#

# The with statement is not available in Python 2.5, so import it
from __future__ import with_statement

# Import the rest of the necessary modules
import GnuPGInterface
import random

# Define GnuPGInterface
gnupg = GnuPGInterface.GnuPG()

### Start config ###

# Define the file that will contain the random string and later be
# signed with GPG. You may want to give the file the ending .asc, but
# this is not required.
randstr_file = ""

# Tell gnupg where to find the GPG keyring. That is, the .gnupg
# directory.
gnupg.options.homedir = ""

# The secret passphrase of the GPG key.
passphrase = ""

### End config ###

# Where to find the passphrase to use when signing the file.
gnupg.passphrase = passphrase

# Create the random string. The string will consist of small letters,
# big letters and digits.
letters = "abcdefghijklmnopqrstuvwxyz"
letters += letters.upper () + "0123456789"

# The random starts out empty, then 40 random possible characters are
# appended.
randstr = "Random string: "
for i in range (40):
	randstr += random.choice(letters)

# Write random string to file
with open(randstr_file, 'w') as write_randstr:
    write_randstr.write(randstr)

# Open the file to read the contents
sign_file = open(randstr_file, 'r')

# Sign the file
clearsign = gnupg.run(['--no-tty'], ['--clearsign'], create_fhs=['stdout'],
		attach_fhs={'stdin': sign_file})

# Read the output, i.e. the signature, so that it can be stored later.
signature = clearsign.handles['stdout'].read()

# Write the output to the original file.
with open(randstr_file, 'w') as write_signature:
    write_signature.write(signature)

@@ -518,188 +518,8 @@
 ##%0A%0A
-# Define the file that will contain the random string and later be%0A# signed with GPG. You may want to give the file the ending .asc, but%0A# this is not required.%0Arandstr_file = %22%22%0A%0A
 # Te
@@ -1091,17 +1091,16 @@
 in range
- 
 (40):%0A%09r
@@ -1133,16 +1133,233 @@
 tters)%0A%0A
+# Write the random string to a file so that it can be signed with GPG%0A# later. The file, with a random name, will be written to /tmp.%0Arandstr_file =%22/tmp/%22%0Afor i in range(10):%0A%09randstr_file += random.choice(letters)%0A%0A
 # Write 

write the file to /tmp