File: SignedFile.py

package info (click to toggle)
mini-dinstall 0.7.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 300 kB
  • sloc: python: 2,027; sh: 33; makefile: 8
file content (107 lines) | stat: -rwxr-xr-x 3,228 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/python3
# SignedFile -*- mode: python; coding: utf-8 -*-

# SignedFile offers a subset of file object operations, and is
# designed to transparently handle files with PGP signatures.

# Copyright (c) 2002 Colin Walters <walters@gnu.org>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

class SignedFile:
    _stream = None
    _eof = False
    _signed = False
    _signature = None
    _signatureversion = None
    _initline = None

    def __init__(self, stream):
        self._stream = stream
        line = stream.readline()
        if line == "-----BEGIN PGP SIGNED MESSAGE-----\n":
            self._signed = True
            while True:
                line = stream.readline()
                if not line or line == '\n':
                    break
        else:
            self._initline = line

    def readline(self):
        if self._eof:
            return ''
        if self._initline:
            line = self._initline
            self._initline = None
        else:
            line = self._stream.readline()
        if not self._signed:
            return line
        elif line == "-----BEGIN PGP SIGNATURE-----\n":
            self._eof = True
            self._signature = []
            self._signatureversion = self._stream.readline()
            self._stream.readline()  # skip blank line
            while True:
                line = self._stream.readline()
                if not line or line == "-----END PGP SIGNATURE-----\n":
                    break
                self._signature.append(line)
            self._signature = ''.join(self._signature)
            return ''
        return line

    def readlines(self):
        ret = []
        while True:
            line = self.readline()
            if line:
                ret.append(line)
            else:
                break
        return ret

    def close(self):
        self._stream.close()

    def getSigned(self):
        return self._signed

    def getSignature(self):
        return self._signature

    def getSignatureVersion(self):
        return self._signatureversion

if __name__ == "__main__":
    import sys
    if not sys.argv:
        print("Need one file as an argument")
        sys.exit(1)
    filename = sys.argv[1]
    f = SignedFile(open(filename))
    if f.getSigned():
        print("**** SIGNED ****")
    else:
        print("**** NOT SIGNED ****")
    lines = f.readlines()
    print(lines)
    if not f.getSigned():
        assert(len(lines) == len(actuallines))
    else:
        print("Signature: %s" % f.getSignature())

# vim:ts=4:sw=4:et: