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):") }
39 mp.drv.alert(L("Passwords don't match."));
48 /* action descriptions */
50 mp.actdesc['set_password'] = LL("Password protect...");
53 /* gets next ARCFOUR byte */
54 /* next char is chr(ord(getchar(l)) ^ mp.arcfour_byte()) */
63 j = (j + S[i]) & 0xff;
65 mp.arcfour.i = i; mp.arcfour.j = j;
68 local t = S[i]; S[i] = S[j]; S[j] = t;
70 return S[(S[i] + S[j]) & 0xff];
74 sub mp.arcfour_init(key)
75 /* initializes an ARCFOUR cypher */
77 /* no key? nothing to do */
81 /* split as an array of characters */
82 local k = split(NULL, key);
86 mp.arcfour.S = [ 0 .. 255 ];
93 foreach (i, [ 0 .. 255 ]) {
94 local t = mp.arcfour.S[i];
96 j = (j + t + ord(k[i % size(k)])) & 0xff;
98 mp.arcfour.S[i] = mp.arcfour.S[j];
102 /* discard 256 bytes (as recommended in many sources) */
103 foreach (i, [ 0 .. 255 ])
108 sub mp.crypt1_load(fd, password)
109 /* loads a crypt1 encrypted file into lines */
115 /* the mpcrypt1\n\0 signature has already been read */
118 mp.arcfour_init(password);
120 while ((c = getchar(fd)) != NULL) {
121 /* decrypt byte and concat */
122 c = chr(ord(c) ^ mp.arcfour_byte());
126 /* end of line; chomp l and push it */
127 push(lines, mp.chomp(l));
132 push(lines, mp.chomp(l));
138 sub mp.crypt1_save(fd, lines, password)
139 /* saves the lines as a crypt1 encrypted file */
143 /* save first the signature */
144 write(fd, "mpcrypt1\n");
150 mp.arcfour_init(password);
154 /* write a line separator if it's not the first line */
156 l = mp.config.eol ~ l;
159 local lc = split(NULL, l);
161 /* write each char xoring with next crypto-byte */
163 putchar(fd, chr(ord(c) ^ mp.arcfour_byte()));
172 sub mp.crypt1_detect(fd)
173 /* detects if fd is an mpcrypt1-type file */
175 /* is it mpcrypt1\n followed by a 0? */
176 if (read(fd) eq "mpcrypt1\n" && ord(getchar(fd)) == 0)
179 /* no; file must be reopen */