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