4 A Programmer's Text Editor
8 Copyright (C) 1991-2007 Angel Ortega <angel@triptico.com>
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 http://www.triptico.com
28 /** editor actions **/
30 mp.actions['set_password'] = sub(d) {
32 { 'type' => 'password', 'label' => L("Password:") },
33 { 'type' => 'password', 'label' => L("Password (again):") }
39 mp.drv.alert(L("Passwords don't match."));
48 /** action descriptions **/
50 mp.actdesc['set_password'] = LL("Password protect...");
55 /* gets next ARCFOUR byte */
56 /* next char is chr(ord(getchar(l)) ^ mp.arcfour_byte()) */
65 j = (j + S[i]) & 0xff;
67 mp.arcfour.i = i; mp.arcfour.j = j;
70 local t = S[i]; S[i] = S[j]; S[j] = t;
72 return S[(S[i] + S[j]) & 0xff];
76 sub mp.arcfour_init(key)
77 /* initializes an ARCFOUR cypher */
79 /* no key? nothing to do */
83 /* split as an array of characters */
84 local k = split(NULL, key);
88 mp.arcfour.S = [ 0 .. 255 ];
95 foreach (i, [ 0 .. 255 ]) {
96 local t = mp.arcfour.S[i];
98 j = (j + t + ord(k[i % size(k)])) & 0xff;
100 mp.arcfour.S[i] = mp.arcfour.S[j];
104 /* discard 256 bytes (as recommended in many sources) */
105 foreach (i, [ 0 .. 255 ])
110 sub mp.crypt1_load(fd, password)
111 /* loads a crypt1 encrypted file into lines */
117 /* the mpcrypt1\n\0 signature has already been read */
120 mp.arcfour_init(password);
122 while ((c = getchar(fd)) != NULL) {
123 /* decrypt byte and concat */
124 c = chr(ord(c) ^ mp.arcfour_byte());
128 /* end of line; chomp l and push it */
129 push(lines, mp.chomp(l));
134 push(lines, mp.chomp(l));
140 sub mp.crypt1_save(fd, lines, password)
141 /* saves the lines as a crypt1 encrypted file */
145 /* save first the signature */
146 write(fd, "mpcrypt1\n");
152 mp.arcfour_init(password);
156 /* write a line separator if it's not the first line */
158 l = mp.config.eol ~ l;
161 local lc = split(NULL, l);
163 /* write each char xoring with next crypto-byte */
165 putchar(fd, chr(ord(c) ^ mp.arcfour_byte()));
174 sub mp.crypt1_detect(fd)
175 /* detects if fd is an mpcrypt1-type file */
177 /* is it mpcrypt1\n followed by a 0? */
178 if (read(fd) eq "mpcrypt1\n" && ord(getchar(fd)) == 0)
181 /* no; file must be reopen */