]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - Crypto/Demo/secimp/sign.py
afc901668dc51ae9f61011e328071805f963660f
[tahoe-lafs/tahoe-lafs.git] / Crypto / Demo / secimp / sign.py
1 #!/usr/bin/env python
2
3 # Using the public key defined in testkey.py, sign all *.pyc files in
4 # the listed directories.
5
6 from testkey import *
7 from allmydata.Crypto.Hash import MD5
8 import os, glob, sys
9 import marshal, compileall
10
11 filelist = []
12 if (len(sys.argv)>1):
13     for dir in sys.argv[1:]:
14         dir=os.path.join(dir, '')
15         compileall.compile_dir(dir)
16         filelist=filelist + glob.glob(dir + '*.pyc')
17 else:
18     print "Usage: sign.py dir1 dir2 dir3 ..."
19     print "  All *.pyc files in the listed directories will be signed,"
20     print "leaving the signatures in *.pys files."
21     sys.exit(0)
22
23 if len(filelist)==0:
24     print "No *.pyc files found"
25     sys.exit(0)
26
27 for file in filelist:
28     input=open(file, 'rb')
29     try:
30         os.unlink(file[:-4]+'.pys')     # Delete any existing signed file
31     except os.error, tuple:
32         if (tuple[0]==2): pass          # Ignore 'file not found' error
33         else: raise os.error, tuple
34     output=open(file[:-4]+'.pys', 'wb')
35     data=input.read()
36     hash=MD5.new(data).digest()         # Compute hash of the code object
37     K = "random bytes"
38     signature=key.sign(hash, K)         # Sign the hash value
39     marshal.dump(signature, output)     # Save signature to the file
40     output.write(data)                  # Copy code object to signed file
41     input.close()
42     output.close()
43     print os.path.basename(file)+ ' processed.'
44
45
46