1 /* Invisible Vector Library
2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 module iv
.arc4simple
/*is aliced*/;
21 // ///////////////////////////////////////////////////////////////////////// //
23 ubyte[256] m
; // permutation table
24 ubyte x
, y
; // permutation indicies
26 nothrow @trusted @nogc:
27 this (const(void)[] key
, usize skipBytes
=4096) { reinit(key
, skipBytes
); }
29 void reinit (const(void)[] key
, usize skipBytes
=4096) {
30 assert(key
.length
> 0);
31 auto keybytes
= cast(const ubyte*)key
.ptr
;
33 foreach (immutable f
; 0..256) m
[f
] = cast(ubyte)f
;
34 // create permutation table
37 foreach (immutable f
; 0..256) {
38 auto kc
= keybytes
[kidx
];
43 if (++kidx
== key
.length
) kidx
= 0;
45 // discard first bytes
55 void processBuffer(T
) (T
[] buf
) {
56 usize len
= T
.sizeof
*buf
.length
;
57 auto data
= cast(ubyte*)buf
.ptr
;
58 foreach (immutable _
; 0..len
) {
62 auto bt = (m
[x
] = m
[y
]);
64 *data
++ ^
= m
[(a
+bt)&0xff];
70 // ///////////////////////////////////////////////////////////////////////// //
71 version(arc4_tests
) unittest {
74 writeln("unittest: arc4");
75 enum ubyte[] sourceData
= [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
76 enum ubyte[] encodedData
= [51,27,44,79,153,231,133,220,143,156,178,63,31,238,28,138];
77 auto data
= sourceData
.dup
;
79 auto a4
= ARC4Codec("asspole");
80 a4
.processBuffer(data
);
81 assert(equal(encodedData
, data
));
84 auto a4
= ARC4Codec("asspole");
85 a4
.processBuffer(data
);
86 assert(equal(sourceData
, data
));