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
30 mp.actions['set_password'] = sub(d) {
32 { 'type' => 'password', 'label' => L("Password:") },
33 { 'type' => 'password', 'label' => L("Password (again):") }
40 mp.drv.alert(L("Passwords don't match."));
50 /* action descriptions */
52 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 */
80 if(key == NULL) return;
82 /* split as an array of characters */
83 local k = split(NULL, key);
87 mp.arcfour.S = [ 0 .. 255 ];
95 foreach(i, [ 0 .. 255 ])
97 local t = mp.arcfour.S[i];
99 j = (j + t + ord(k[i % size(k)])) & 0xff;
101 mp.arcfour.S[i] = mp.arcfour.S[j];
105 /* discard 256 bytes (as recommended in many sources) */
106 foreach(i, [ 0 .. 255 ])
111 sub mp.crypt1_load(fd, password)
112 /* loads a crypt1 encrypted file into lines */
118 /* the mpcrypt1\n\0 signature has already been read */
121 mp.arcfour_init(password);
123 while((c = getchar(fd)) != NULL)
125 /* decrypt byte and concat */
126 c = chr(ord(c) ^ mp.arcfour_byte());
131 /* end of line; chomp l and push it */
132 push(lines, mp.chomp(l));
137 push(lines, mp.chomp(l));
143 sub mp.crypt1_save(fd, lines, password)
144 /* saves the lines as a crypt1 encrypted file */
148 /* save first the signature */
149 write(fd, "mpcrypt1\n");
155 mp.arcfour_init(password);
158 foreach(local l, lines)
160 /* write a line separator if it's not the first line */
161 if(nl) l = mp.config.eol ~ l;
164 local lc = split(NULL, l);
166 /* write each char xoring with next crypto-byte */
168 putchar(fd, chr(ord(c) ^ mp.arcfour_byte()));
177 sub mp.crypt1_detect(fd)
178 /* detects if fd is an mpcrypt1-type file */
180 /* is it mpcrypt1\n followed by a 0? */
181 if(read(fd) eq "mpcrypt1\n" && ord(getchar(fd)) == 0)
184 /* no; rewind file and say it */