Attachment 'MoinCrypt.py'
Download 1 # -*- coding: utf-8 -*-
2 """
3 MoinMoin - MoinCrypt Action
4
5 PURPOSE:
6 This action is used to display an encrypted page
7
8 MODIFICATION HISTORY:
9 @copyright: 2008 by Frank Maloschytzki (Frank.Maloschytzki@gansnett.de)
10 @license: GNU GPL, see COPYING for details.
11 Version: 0.1
12
13 Part of code is copied from Whois.py and test.py
14
15
16
17 """
18
19 from MoinMoin.PageEditor import PageEditor
20 from MoinMoin.action import ActionBase
21 from base64 import b64decode
22 from Crypto.Cipher import AES
23
24
25 PAGENAME = u'ThisPageDoesNotExistsAndWillNeverBeReally'
26
27 CRYPTHEADER = 'Encrypted Page:'
28
29
30
31
32 class MoinCrypt(ActionBase):
33 """ Show Decrypted page action
34
35 """
36
37
38 def __init__(self, pagename, request):
39 ActionBase.__init__(self, pagename, request)
40 self.use_ticket = False
41 _ = self._
42 self.form_trigger = 'passphrase'
43 self.form_trigger_label = _('Crypt / Decrypt')
44 self.thispage = PageEditor(request, pagename)
45
46
47 def expandto16(self, text, fillchar=' '):
48 """ Expand length of string to multiple of 16 by adding spaces
49 """
50 i = len(text) % 16
51 if i != 0:
52 return text + fillchar*(16-i)
53 else:
54 return text
55
56
57 def do_action(self):
58
59 _ = self._
60 form = self.form
61 passphrase = self.expandto16(form.get('pass', [u''])[0])
62 text = self.thispage.get_body().encode('latin-1')
63 if text.find(CRYPTHEADER) == -1:
64 # Encrypt Page
65 obj = AES.new(passphrase)
66 ctext = obj.encrypt(self.expandto16(text))
67
68 self.thispage.saveText(CRYPTHEADER+ctext.encode("base64"), 0, comment=u"Encrypt")
69 self.render_msg(_('Successfully encrypted'))
70 return
71 else:
72 # Page ist encrypted, so decrypt
73 text = text[len(CRYPTHEADER):]
74 text = b64decode(text)
75
76 obj = AES.new(passphrase, AES.MODE_ECB)
77
78 ctext = obj.decrypt(text)
79 self.thispage.saveText(unicode(ctext,'latin-1'), 0, comment=u"Decrypt")
80 self.render_msg(_('Successfully decrypted'))
81 return
82
83
84 def get_form_html(self, buttons_html):
85 _ = self._
86 d = {
87 'pagename': self.pagename,
88 'pass_label': _("Passphrase"),
89 'buttons_html': buttons_html,
90 }
91 return '''
92 <table>
93 <tr>
94 <td class="label"><label>%(pass_label)s</label></td>
95 <td class="content">
96 <input type="text" name="pass" maxlength="128">
97 </td>
98 </tr>
99 <tr>
100 <td></td>
101 <td class="buttons">
102 %(buttons_html)s
103 </td>
104 </tr>
105 </table>
106 ''' % d
107
108 def work(self):
109 """ Render action - this is the main function called by action's
110 execute() function.
111
112 We usually render a form here, check for posted forms, etc.
113 """
114 _ = self._
115 form = self.form
116
117 if form.has_key(self.form_cancel):
118 self.render_cancel()
119 return
120
121 # Validate allowance, user rights and other conditions.
122 error = None
123 if self.is_excluded():
124 error = _('Action %(actionname)s is excluded in this wiki!') % {'actionname': self.actionname }
125 elif not self.is_allowed():
126 error = _('You are not allowed to use action %(actionname)s on thispage!') % {'actionname': self.actionname }
127 if error is None:
128 self.check_condition()
129 if error:
130 self.render_msg(error)
131 elif form.has_key(self.form_trigger) or form.has_key('pass'): # user hit the trigger button
132 if self.ticket_ok():
133 self.do_action()
134 return
135 else:
136 self.render_msg(_('Please use the interactive user interface to use action %(actionname)s!') % {'actionname': self.actionname })
137 return
138 else:
139 # Return a new form
140 self.render_msg(self.make_form())
141
142 def execute(pagename, request):
143 """ Do your work
144 """
145 _ = request.getText
146
147 # Don't check if personal wiki
148
149 # if request.user.valid:
150 # username = request.user.name
151 # else:
152 # username = ''
153 #
154 # if not username:
155 # return thispage.send_page(
156 # msg = _('Please log in first.'))
157
158
159 MoinCrypt(pagename, request).work()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.