RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
IsaacCipher.java
1/* Copyright (c) 2009 Graham Edgecombe, Blake Beaupain and Brett Russell
2*
3* More information about Hyperion may be found on this website:
4* http://hyperion.grahamedgecombe.com/
5*
6* Permission is hereby granted, free of charge, to any person obtaining a copy
7* of this software and associated documentation files (the "Software"), to deal
8* in the Software without restriction, including without limitation the rights
9* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10* copies of the Software, and to permit persons to whom the Software is
11* furnished to do so, subject to the following conditions:
12*
13* The above copyright notice and this permission notice shall be included in
14* all copies or substantial portions of the Software.
15*
16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22* THE SOFTWARE.
23*/
24package com.osroyale.net.codec;
25
64
65public final class IsaacCipher {
66
70 private static final int RATIO = 0x9e3779b9;
71
75 private static final int SIZE_LOG = 8;
76
80 private static final int SIZE = 1 << SIZE_LOG;
81
85 private static final int MASK = (SIZE - 1) << 2;
86
90 private int count = 0;
91
95 private int results[] = new int[SIZE];
96
100 private int memory[] = new int[SIZE];
101
105 private int a;
106
110 private int b;
111
115 private int c;
116
122 public IsaacCipher(int[] seed) {
123 for (int i = 0; i < seed.length; i++) {
124 results[i] = seed[i];
125 }
126 init(true);
127 }
128
134 public int getKey() {
135 if (count-- == 0) {
136 isaac();
137 count = SIZE - 1;
138 }
139 return results[count];
140 }
141
145 public void isaac() {
146 int i, j, x, y;
147 b += ++c;
148 for (i = 0, j = SIZE / 2; i < SIZE / 2; ) {
149 x = memory[i];
150 a ^= a << 13;
151 a += memory[j++];
152 memory[i] = y = memory[(x & MASK) >> 2] + a + b;
153 results[i++] = b = memory[((y >> SIZE_LOG) & MASK) >> 2] + x;
154
155 x = memory[i];
156 a ^= a >>> 6;
157 a += memory[j++];
158 memory[i] = y = memory[(x & MASK) >> 2] + a + b;
159 results[i++] = b = memory[((y >> SIZE_LOG) & MASK) >> 2] + x;
160
161 x = memory[i];
162 a ^= a << 2;
163 a += memory[j++];
164 memory[i] = y = memory[(x & MASK) >> 2] + a + b;
165 results[i++] = b = memory[((y >> SIZE_LOG) & MASK) >> 2] + x;
166
167 x = memory[i];
168 a ^= a >>> 16;
169 a += memory[j++];
170 memory[i] = y = memory[(x & MASK) >> 2] + a + b;
171 results[i++] = b = memory[((y >> SIZE_LOG) & MASK) >> 2] + x;
172 }
173 for (j = 0; j < SIZE / 2; ) {
174 x = memory[i];
175 a ^= a << 13;
176 a += memory[j++];
177 memory[i] = y = memory[(x & MASK) >> 2] + a + b;
178 results[i++] = b = memory[((y >> SIZE_LOG) & MASK) >> 2] + x;
179
180 x = memory[i];
181 a ^= a >>> 6;
182 a += memory[j++];
183 memory[i] = y = memory[(x & MASK) >> 2] + a + b;
184 results[i++] = b = memory[((y >> SIZE_LOG) & MASK) >> 2] + x;
185
186 x = memory[i];
187 a ^= a << 2;
188 a += memory[j++];
189 memory[i] = y = memory[(x & MASK) >> 2] + a + b;
190 results[i++] = b = memory[((y >> SIZE_LOG) & MASK) >> 2] + x;
191
192 x = memory[i];
193 a ^= a >>> 16;
194 a += memory[j++];
195 memory[i] = y = memory[(x & MASK) >> 2] + a + b;
196 results[i++] = b = memory[((y >> SIZE_LOG) & MASK) >> 2] + x;
197 }
198 }
199
205 public void init(boolean flag) {
206 int i;
207 int a, b, c, d, e, f, g, h;
208 a = b = c = d = e = f = g = h = RATIO;
209 for (i = 0; i < 4; ++i) {
210 a ^= b << 11;
211 d += a;
212 b += c;
213 b ^= c >>> 2;
214 e += b;
215 c += d;
216 c ^= d << 8;
217 f += c;
218 d += e;
219 d ^= e >>> 16;
220 g += d;
221 e += f;
222 e ^= f << 10;
223 h += e;
224 f += g;
225 f ^= g >>> 4;
226 a += f;
227 g += h;
228 g ^= h << 8;
229 b += g;
230 h += a;
231 h ^= a >>> 9;
232 c += h;
233 a += b;
234 }
235 for (i = 0; i < SIZE; i += 8) {
236 if (flag) {
237 a += results[i];
238 b += results[i + 1];
239 c += results[i + 2];
240 d += results[i + 3];
241 e += results[i + 4];
242 f += results[i + 5];
243 g += results[i + 6];
244 h += results[i + 7];
245 }
246 a ^= b << 11;
247 d += a;
248 b += c;
249 b ^= c >>> 2;
250 e += b;
251 c += d;
252 c ^= d << 8;
253 f += c;
254 d += e;
255 d ^= e >>> 16;
256 g += d;
257 e += f;
258 e ^= f << 10;
259 h += e;
260 f += g;
261 f ^= g >>> 4;
262 a += f;
263 g += h;
264 g ^= h << 8;
265 b += g;
266 h += a;
267 h ^= a >>> 9;
268 c += h;
269 a += b;
270 memory[i] = a;
271 memory[i + 1] = b;
272 memory[i + 2] = c;
273 memory[i + 3] = d;
274 memory[i + 4] = e;
275 memory[i + 5] = f;
276 memory[i + 6] = g;
277 memory[i + 7] = h;
278 }
279 if (flag) {
280 for (i = 0; i < SIZE; i += 8) {
281 a += memory[i];
282 b += memory[i + 1];
283 c += memory[i + 2];
284 d += memory[i + 3];
285 e += memory[i + 4];
286 f += memory[i + 5];
287 g += memory[i + 6];
288 h += memory[i + 7];
289 a ^= b << 11;
290 d += a;
291 b += c;
292 b ^= c >>> 2;
293 e += b;
294 c += d;
295 c ^= d << 8;
296 f += c;
297 d += e;
298 d ^= e >>> 16;
299 g += d;
300 e += f;
301 e ^= f << 10;
302 h += e;
303 f += g;
304 f ^= g >>> 4;
305 a += f;
306 g += h;
307 g ^= h << 8;
308 b += g;
309 h += a;
310 h ^= a >>> 9;
311 c += h;
312 a += b;
313 memory[i] = a;
314 memory[i + 1] = b;
315 memory[i + 2] = c;
316 memory[i + 3] = d;
317 memory[i + 4] = e;
318 memory[i + 5] = f;
319 memory[i + 6] = g;
320 memory[i + 7] = h;
321 }
322 }
323 isaac();
324 count = SIZE;
325 }
326}