summaryrefslogtreecommitdiff
path: root/passlib/_unix_crypt.py
blob: 9520bd25ac0c7ce1a0fbd47e832b99e52a677c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
"""passlib._unix_crypt -- unix crypt(3) implementation

History
=======
This is a pure-python implementation of the unix "crypt" algorithm,
based on the Java implementation found at http://www.dynamic.net.au/christos/crypt/UnixCrypt2.txt

The copyright & license for that source is as follows::

    UnixCrypt.java	0.9 96/11/25
    Copyright (c) 1996 Aki Yoshida. All rights reserved.
    Permission to use, copy, modify and distribute this software
    for non-commercial or commercial purposes and without fee is
    hereby granted provided that this copyright notice appears in
    all copies.

    ---

    Unix crypt(3C) utility
    @version 	0.9, 11/25/96
    @author 	Aki Yoshida

    ---

    modified April 2001
    by Iris Van den Broeke, Daniel Deville

    ---
    Unix Crypt.
    Implements the one way cryptography used by Unix systems for
    simple password protection.
    @version $Id: UnixCrypt2.txt,v 1.1.1.1 2005/09/13 22:20:13 christos Exp $
    @author Greg Wilkins (gregw)

This python implementation was adapted from this source by Eli Collins <elic@astllc.org>,
and released under the BSD license as part of PassLib.

This module is mainly meant as a fallback when stdlib does not supply a ``crypt`` implementation,
such as on windows systems. As such, it attempts to have a public interface
which is compatible with stdlib, so it can be used as a drop-in replacement.
"""

#=========================================================
#simple constants
#=========================================================

#base64 char sequence
CHARS = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
b64_encode = CHARS.__getitem__ # int -> char

#inverse map (char->value)
CHARIDX = dict( (c,i) for i,c in enumerate(CHARS))
b64_decode = CHARIDX.__getitem__ # char -> int

#precalculated iteration ranges
R8 = range(8)
RR8 = range(7, -1, -1)
RR4 = range(3, -1, -1)
RR12_1 = range(11, 1, -1)

#=========================================================
# static tables
#=========================================================
PCXROT = IE3264 = SPE = CF6464 = None #placeholders filled in by load_tables

def load_tables():
    "delay loading tables until they are actually needed"
    global PCXROT, IE3264, SPE, CF6464

    #---------------------------------------------------
    # Initial key schedule permutation
    # PC1ROT - bit reverse, then PC1, then Rotate, then PC2
    #---------------------------------------------------
    PC1ROT=(
    ( 0x0000000000000000, 0x0000000000000000, 0x0000010000000000, 0x0000010000000000,
      0x0000000100000000, 0x0000000100000000, 0x0000010100000000, 0x0000010100000000,
      0x0000000000100000, 0x0000000000100000, 0x0000010000100000, 0x0000010000100000,
      0x0000000100100000, 0x0000000100100000, 0x0000010100100000, 0x0000010100100000, ),
    ( 0x0000000000000000, 0x0000000080000000, 0x0000040000000000, 0x0000040080000000,
      0x0010000000000000, 0x0010000080000000, 0x0010040000000000, 0x0010040080000000,
      0x0000000800000000, 0x0000000880000000, 0x0000040800000000, 0x0000040880000000,
      0x0010000800000000, 0x0010000880000000, 0x0010040800000000, 0x0010040880000000, ),
    ( 0x0000000000000000, 0x0000000000000000, 0x0000000000004000, 0x0000000000004000,
      0x0000000000000008, 0x0000000000000008, 0x0000000000004008, 0x0000000000004008,
      0x0000000000000010, 0x0000000000000010, 0x0000000000004010, 0x0000000000004010,
      0x0000000000000018, 0x0000000000000018, 0x0000000000004018, 0x0000000000004018, ),
    ( 0x0000000000000000, 0x0000000200000000, 0x0001000000000000, 0x0001000200000000,
      0x0400000000000000, 0x0400000200000000, 0x0401000000000000, 0x0401000200000000,
      0x0020000000000000, 0x0020000200000000, 0x0021000000000000, 0x0021000200000000,
      0x0420000000000000, 0x0420000200000000, 0x0421000000000000, 0x0421000200000000, ),
    ( 0x0000000000000000, 0x0000000000000000, 0x0000000000400000, 0x0000000000400000,
      0x0000000004000000, 0x0000000004000000, 0x0000000004400000, 0x0000000004400000,
      0x0000000000000800, 0x0000000000000800, 0x0000000000400800, 0x0000000000400800,
      0x0000000004000800, 0x0000000004000800, 0x0000000004400800, 0x0000000004400800, ),
    ( 0x0000000000000000, 0x0000000000008000, 0x0040000000000000, 0x0040000000008000,
      0x0000004000000000, 0x0000004000008000, 0x0040004000000000, 0x0040004000008000,
      0x8000000000000000, 0x8000000000008000, 0x8040000000000000, 0x8040000000008000,
      0x8000004000000000, 0x8000004000008000, 0x8040004000000000, 0x8040004000008000, ),
    ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000080, 0x0000000000000080,
      0x0000000000080000, 0x0000000000080000, 0x0000000000080080, 0x0000000000080080,
      0x0000000000800000, 0x0000000000800000, 0x0000000000800080, 0x0000000000800080,
      0x0000000000880000, 0x0000000000880000, 0x0000000000880080, 0x0000000000880080, ),
    ( 0x0000000000000000, 0x0000000008000000, 0x0000002000000000, 0x0000002008000000,
      0x0000100000000000, 0x0000100008000000, 0x0000102000000000, 0x0000102008000000,
      0x0000200000000000, 0x0000200008000000, 0x0000202000000000, 0x0000202008000000,
      0x0000300000000000, 0x0000300008000000, 0x0000302000000000, 0x0000302008000000, ),
    ( 0x0000000000000000, 0x0000000000000000, 0x0000000010000000, 0x0000000010000000,
      0x0000000000001000, 0x0000000000001000, 0x0000000010001000, 0x0000000010001000,
      0x0000000040000000, 0x0000000040000000, 0x0000000050000000, 0x0000000050000000,
      0x0000000040001000, 0x0000000040001000, 0x0000000050001000, 0x0000000050001000, ),
    ( 0x0000000000000000, 0x0000001000000000, 0x0000080000000000, 0x0000081000000000,
      0x1000000000000000, 0x1000001000000000, 0x1000080000000000, 0x1000081000000000,
      0x0004000000000000, 0x0004001000000000, 0x0004080000000000, 0x0004081000000000,
      0x1004000000000000, 0x1004001000000000, 0x1004080000000000, 0x1004081000000000, ),
    ( 0x0000000000000000, 0x0000000000000000, 0x0000000000040000, 0x0000000000040000,
      0x0000020000000000, 0x0000020000000000, 0x0000020000040000, 0x0000020000040000,
      0x0000000000000004, 0x0000000000000004, 0x0000000000040004, 0x0000000000040004,
      0x0000020000000004, 0x0000020000000004, 0x0000020000040004, 0x0000020000040004, ),
    ( 0x0000000000000000, 0x0000400000000000, 0x0200000000000000, 0x0200400000000000,
      0x0080000000000000, 0x0080400000000000, 0x0280000000000000, 0x0280400000000000,
      0x0000008000000000, 0x0000408000000000, 0x0200008000000000, 0x0200408000000000,
      0x0080008000000000, 0x0080408000000000, 0x0280008000000000, 0x0280408000000000, ),
    ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000040, 0x0000000000000040,
      0x0000000020000000, 0x0000000020000000, 0x0000000020000040, 0x0000000020000040,
      0x0000000000200000, 0x0000000000200000, 0x0000000000200040, 0x0000000000200040,
      0x0000000020200000, 0x0000000020200000, 0x0000000020200040, 0x0000000020200040, ),
    ( 0x0000000000000000, 0x0002000000000000, 0x0800000000000000, 0x0802000000000000,
      0x0100000000000000, 0x0102000000000000, 0x0900000000000000, 0x0902000000000000,
      0x4000000000000000, 0x4002000000000000, 0x4800000000000000, 0x4802000000000000,
      0x4100000000000000, 0x4102000000000000, 0x4900000000000000, 0x4902000000000000, ),
    ( 0x0000000000000000, 0x0000000000000000, 0x0000000000002000, 0x0000000000002000,
      0x0000000000000020, 0x0000000000000020, 0x0000000000002020, 0x0000000000002020,
      0x0000000000000400, 0x0000000000000400, 0x0000000000002400, 0x0000000000002400,
      0x0000000000000420, 0x0000000000000420, 0x0000000000002420, 0x0000000000002420, ),
    ( 0x0000000000000000, 0x2000000000000000, 0x0000000400000000, 0x2000000400000000,
      0x0000800000000000, 0x2000800000000000, 0x0000800400000000, 0x2000800400000000,
      0x0008000000000000, 0x2008000000000000, 0x0008000400000000, 0x2008000400000000,
      0x0008800000000000, 0x2008800000000000, 0x0008800400000000, 0x2008800400000000, ),
        )
    #---------------------------------------------------
    # Subsequent key schedule rotation permutations
    # PC2ROT - PC2 inverse, then Rotate, then PC2
    #---------------------------------------------------
    PC2ROTA=(
    ( 0x0000000000000000, 0x0000800000000000, 0x0800000000000000, 0x0800800000000000,
      0x0000004000000000, 0x0000804000000000, 0x0800004000000000, 0x0800804000000000,
      0x0000000400000000, 0x0000800400000000, 0x0800000400000000, 0x0800800400000000,
      0x0000004400000000, 0x0000804400000000, 0x0800004400000000, 0x0800804400000000, ),
    ( 0x0000000000000000, 0x0080000000000000, 0x0000040000000000, 0x0080040000000000,
      0x0008000000000000, 0x0088000000000000, 0x0008040000000000, 0x0088040000000000,
      0x0000200000000000, 0x0080200000000000, 0x0000240000000000, 0x0080240000000000,
      0x0008200000000000, 0x0088200000000000, 0x0008240000000000, 0x0088240000000000, ),
    ( 0x0000000000000000, 0x0040000000000000, 0x2000000000000000, 0x2040000000000000,
      0x0000008000000000, 0x0040008000000000, 0x2000008000000000, 0x2040008000000000,
      0x0000001000000000, 0x0040001000000000, 0x2000001000000000, 0x2040001000000000,
      0x0000009000000000, 0x0040009000000000, 0x2000009000000000, 0x2040009000000000, ),
    ( 0x0000000000000000, 0x0400000000000000, 0x8000000000000000, 0x8400000000000000,
      0x0000002000000000, 0x0400002000000000, 0x8000002000000000, 0x8400002000000000,
      0x0100000000000000, 0x0500000000000000, 0x8100000000000000, 0x8500000000000000,
      0x0100002000000000, 0x0500002000000000, 0x8100002000000000, 0x8500002000000000, ),
    ( 0x0000000000000000, 0x0000000000004000, 0x0000000020000000, 0x0000000020004000,
      0x0001000000000000, 0x0001000000004000, 0x0001000020000000, 0x0001000020004000,
      0x0200000000000000, 0x0200000000004000, 0x0200000020000000, 0x0200000020004000,
      0x0201000000000000, 0x0201000000004000, 0x0201000020000000, 0x0201000020004000, ),
    ( 0x0000000000000000, 0x1000000000000000, 0x0004000000000000, 0x1004000000000000,
      0x0002000000000000, 0x1002000000000000, 0x0006000000000000, 0x1006000000000000,
      0x0000000800000000, 0x1000000800000000, 0x0004000800000000, 0x1004000800000000,
      0x0002000800000000, 0x1002000800000000, 0x0006000800000000, 0x1006000800000000, ),
    ( 0x0000000000000000, 0x0000000000000008, 0x0000000000008000, 0x0000000000008008,
      0x0010000000000000, 0x0010000000000008, 0x0010000000008000, 0x0010000000008008,
      0x0020000000000000, 0x0020000000000008, 0x0020000000008000, 0x0020000000008008,
      0x0030000000000000, 0x0030000000000008, 0x0030000000008000, 0x0030000000008008, ),
    ( 0x0000000000000000, 0x0000400000000000, 0x0000080000000000, 0x0000480000000000,
      0x0000100000000000, 0x0000500000000000, 0x0000180000000000, 0x0000580000000000,
      0x4000000000000000, 0x4000400000000000, 0x4000080000000000, 0x4000480000000000,
      0x4000100000000000, 0x4000500000000000, 0x4000180000000000, 0x4000580000000000, ),
    ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
      0x0000000000080000, 0x0000000000080000, 0x0000000000080000, 0x0000000000080000,
      0x0000000000100000, 0x0000000000100000, 0x0000000000100000, 0x0000000000100000,
      0x0000000000180000, 0x0000000000180000, 0x0000000000180000, 0x0000000000180000, ),
    ( 0x0000000000000000, 0x0000000000040000, 0x0000000000000020, 0x0000000000040020,
      0x0000000000000004, 0x0000000000040004, 0x0000000000000024, 0x0000000000040024,
      0x0000000200000000, 0x0000000200040000, 0x0000000200000020, 0x0000000200040020,
      0x0000000200000004, 0x0000000200040004, 0x0000000200000024, 0x0000000200040024, ),
    ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
      0x0000000000000040, 0x0000000000000040, 0x0000000000000040, 0x0000000000000040,
      0x0000000000001000, 0x0000000000001000, 0x0000000000001000, 0x0000000000001000,
      0x0000000000001040, 0x0000000000001040, 0x0000000000001040, 0x0000000000001040, ),
    ( 0x0000000000000000, 0x0000000000000010, 0x0000000000000400, 0x0000000000000410,
      0x0000000000000080, 0x0000000000000090, 0x0000000000000480, 0x0000000000000490,
      0x0000000040000000, 0x0000000040000010, 0x0000000040000400, 0x0000000040000410,
      0x0000000040000080, 0x0000000040000090, 0x0000000040000480, 0x0000000040000490, ),
    ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
      0x0000000100000000, 0x0000000100000000, 0x0000000100000000, 0x0000000100000000,
      0x0000000000800000, 0x0000000000800000, 0x0000000000800000, 0x0000000000800000,
      0x0000000100800000, 0x0000000100800000, 0x0000000100800000, 0x0000000100800000, ),
    ( 0x0000000000000000, 0x0000020000000000, 0x0000000080000000, 0x0000020080000000,
      0x0000000000400000, 0x0000020000400000, 0x0000000080400000, 0x0000020080400000,
      0x0000000008000000, 0x0000020008000000, 0x0000000088000000, 0x0000020088000000,
      0x0000000008400000, 0x0000020008400000, 0x0000000088400000, 0x0000020088400000, ),
    ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
      0x0000000000200000, 0x0000000000200000, 0x0000000000200000, 0x0000000000200000,
      0x0000000004000000, 0x0000000004000000, 0x0000000004000000, 0x0000000004000000,
      0x0000000004200000, 0x0000000004200000, 0x0000000004200000, 0x0000000004200000, ),
    ( 0x0000000000000000, 0x0000000000000800, 0x0000010000000000, 0x0000010000000800,
      0x0000000000002000, 0x0000000000002800, 0x0000010000002000, 0x0000010000002800,
      0x0000000010000000, 0x0000000010000800, 0x0000010010000000, 0x0000010010000800,
      0x0000000010002000, 0x0000000010002800, 0x0000010010002000, 0x0000010010002800, ),
        )
    PC2ROTB=(
    ( 0x0000000000000000, 0x0000000800000000, 0x0000000400000000, 0x0000000c00000000,
      0x0000100000000000, 0x0000100800000000, 0x0000100400000000, 0x0000100c00000000,
      0x0010000000000000, 0x0010000800000000, 0x0010000400000000, 0x0010000c00000000,
      0x0010100000000000, 0x0010100800000000, 0x0010100400000000, 0x0010100c00000000, ),
    ( 0x0000000000000000, 0x0100000000000000, 0x0001000000000000, 0x0101000000000000,
      0x0000001000000000, 0x0100001000000000, 0x0001001000000000, 0x0101001000000000,
      0x0004000000000000, 0x0104000000000000, 0x0005000000000000, 0x0105000000000000,
      0x0004001000000000, 0x0104001000000000, 0x0005001000000000, 0x0105001000000000, ),
    ( 0x0000000000000000, 0x0000002000000000, 0x0000040000000000, 0x0000042000000000,
      0x4000000000000000, 0x4000002000000000, 0x4000040000000000, 0x4000042000000000,
      0x0000400000000000, 0x0000402000000000, 0x0000440000000000, 0x0000442000000000,
      0x4000400000000000, 0x4000402000000000, 0x4000440000000000, 0x4000442000000000, ),
    ( 0x0000000000000000, 0x0000004000000000, 0x0000200000000000, 0x0000204000000000,
      0x0000080000000000, 0x0000084000000000, 0x0000280000000000, 0x0000284000000000,
      0x0000800000000000, 0x0000804000000000, 0x0000a00000000000, 0x0000a04000000000,
      0x0000880000000000, 0x0000884000000000, 0x0000a80000000000, 0x0000a84000000000, ),
    ( 0x0000000000000000, 0x0000000000400000, 0x0000000000000020, 0x0000000000400020,
      0x0040000000000000, 0x0040000000400000, 0x0040000000000020, 0x0040000000400020,
      0x0800000000000000, 0x0800000000400000, 0x0800000000000020, 0x0800000000400020,
      0x0840000000000000, 0x0840000000400000, 0x0840000000000020, 0x0840000000400020, ),
    ( 0x0000000000000000, 0x0080000000000000, 0x0000008000000000, 0x0080008000000000,
      0x2000000000000000, 0x2080000000000000, 0x2000008000000000, 0x2080008000000000,
      0x0020000000000000, 0x00a0000000000000, 0x0020008000000000, 0x00a0008000000000,
      0x2020000000000000, 0x20a0000000000000, 0x2020008000000000, 0x20a0008000000000, ),
    ( 0x0000000000000000, 0x0000000004000000, 0x0000000008000000, 0x000000000c000000,
      0x0400000000000000, 0x0400000004000000, 0x0400000008000000, 0x040000000c000000,
      0x8000000000000000, 0x8000000004000000, 0x8000000008000000, 0x800000000c000000,
      0x8400000000000000, 0x8400000004000000, 0x8400000008000000, 0x840000000c000000, ),
    ( 0x0000000000000000, 0x0002000000000000, 0x0200000000000000, 0x0202000000000000,
      0x1000000000000000, 0x1002000000000000, 0x1200000000000000, 0x1202000000000000,
      0x0008000000000000, 0x000a000000000000, 0x0208000000000000, 0x020a000000000000,
      0x1008000000000000, 0x100a000000000000, 0x1208000000000000, 0x120a000000000000, ),
    ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
      0x0000000000001000, 0x0000000000001000, 0x0000000000001000, 0x0000000000001000,
      0x0000000000000010, 0x0000000000000010, 0x0000000000000010, 0x0000000000000010,
      0x0000000000001010, 0x0000000000001010, 0x0000000000001010, 0x0000000000001010, ),
    ( 0x0000000000000000, 0x0000000000000040, 0x0000010000000000, 0x0000010000000040,
      0x0000000000200000, 0x0000000000200040, 0x0000010000200000, 0x0000010000200040,
      0x0000000000008000, 0x0000000000008040, 0x0000010000008000, 0x0000010000008040,
      0x0000000000208000, 0x0000000000208040, 0x0000010000208000, 0x0000010000208040, ),
    ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
      0x0000000000002000, 0x0000000000002000, 0x0000000000002000, 0x0000000000002000,
      0x0000020000000000, 0x0000020000000000, 0x0000020000000000, 0x0000020000000000,
      0x0000020000002000, 0x0000020000002000, 0x0000020000002000, 0x0000020000002000, ),
    ( 0x0000000000000000, 0x0000000000000800, 0x0000000100000000, 0x0000000100000800,
      0x0000000010000000, 0x0000000010000800, 0x0000000110000000, 0x0000000110000800,
      0x0000000000000004, 0x0000000000000804, 0x0000000100000004, 0x0000000100000804,
      0x0000000010000004, 0x0000000010000804, 0x0000000110000004, 0x0000000110000804, ),
    ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
      0x0000000000000008, 0x0000000000000008, 0x0000000000000008, 0x0000000000000008,
      0x0000000040000000, 0x0000000040000000, 0x0000000040000000, 0x0000000040000000,
      0x0000000040000008, 0x0000000040000008, 0x0000000040000008, 0x0000000040000008, ),
    ( 0x0000000000000000, 0x0000000020000000, 0x0000000200000000, 0x0000000220000000,
      0x0000000000000080, 0x0000000020000080, 0x0000000200000080, 0x0000000220000080,
      0x0000000000100000, 0x0000000020100000, 0x0000000200100000, 0x0000000220100000,
      0x0000000000100080, 0x0000000020100080, 0x0000000200100080, 0x0000000220100080, ),
    ( 0x0000000000000000, 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
      0x0000000000000400, 0x0000000000000400, 0x0000000000000400, 0x0000000000000400,
      0x0000000000080000, 0x0000000000080000, 0x0000000000080000, 0x0000000000080000,
      0x0000000000080400, 0x0000000000080400, 0x0000000000080400, 0x0000000000080400, ),
    ( 0x0000000000000000, 0x0000000000800000, 0x0000000000004000, 0x0000000000804000,
      0x0000000080000000, 0x0000000080800000, 0x0000000080004000, 0x0000000080804000,
      0x0000000000040000, 0x0000000000840000, 0x0000000000044000, 0x0000000000844000,
      0x0000000080040000, 0x0000000080840000, 0x0000000080044000, 0x0000000080844000, ),
        )
    #---------------------------------------------------
    #PCXROT - PC1ROT, PC2ROTA, PC2ROTB listed in order
    # of the PC1 rotation schedule, as used by des_setkey
    #---------------------------------------------------
    ##ROTATES = (1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1)
    PCXROT = (
            PC1ROT,  PC2ROTA, PC2ROTB, PC2ROTB,
            PC2ROTB, PC2ROTB, PC2ROTB, PC2ROTB,
            PC2ROTA, PC2ROTB, PC2ROTB, PC2ROTB,
            PC2ROTB, PC2ROTB, PC2ROTB, PC2ROTA,
            )

    #---------------------------------------------------
    # Bit reverse, intial permupation, expantion
    # Initial permutation/expansion table
    #---------------------------------------------------
    IE3264=(
    ( 0x0000000000000000, 0x0000000000004004, 0x0400000000000040, 0x0400000000004044,
      0x0000000040040000, 0x0000000040044004, 0x0400000040040040, 0x0400000040044044,
      0x0000000000400400, 0x0000000000404404, 0x0400000000400440, 0x0400000000404444,
      0x0000000040440400, 0x0000000040444404, 0x0400000040440440, 0x0400000040444444, ),
    ( 0x0000000000000000, 0x0000400400000000, 0x0000004004000000, 0x0000404404000000,
      0x4004000000000000, 0x4004400400000000, 0x4004004004000000, 0x4004404404000000,
      0x0040040000000000, 0x0040440400000000, 0x0040044004000000, 0x0040444404000000,
      0x4044040000000000, 0x4044440400000000, 0x4044044004000000, 0x4044444404000000, ),
    ( 0x0000000000000000, 0x0000000000002000, 0x0000000000000020, 0x0000000000002020,
      0x0000000020000000, 0x0000000020002000, 0x0000000020000020, 0x0000000020002020,
      0x0000000000200000, 0x0000000000202000, 0x0000000000200020, 0x0000000000202020,
      0x0000000020200000, 0x0000000020202000, 0x0000000020200020, 0x0000000020202020, ),
    ( 0x0000000000000000, 0x0000200000000000, 0x0000002000000000, 0x0000202000000000,
      0x2000000000000000, 0x2000200000000000, 0x2000002000000000, 0x2000202000000000,
      0x0020000000000000, 0x0020200000000000, 0x0020002000000000, 0x0020202000000000,
      0x2020000000000000, 0x2020200000000000, 0x2020002000000000, 0x2020202000000000, ),
    ( 0x0000000000000000, 0x0000000000001000, 0x0000000000000010, 0x0000000000001010,
      0x0000000010000000, 0x0000000010001000, 0x0000000010000010, 0x0000000010001010,
      0x0000000000100000, 0x0000000000101000, 0x0000000000100010, 0x0000000000101010,
      0x0000000010100000, 0x0000000010101000, 0x0000000010100010, 0x0000000010101010, ),
    ( 0x0000000000000000, 0x0000100000000000, 0x0000001000000000, 0x0000101000000000,
      0x1000000000000000, 0x1000100000000000, 0x1000001000000000, 0x1000101000000000,
      0x0010000000000000, 0x0010100000000000, 0x0010001000000000, 0x0010101000000000,
      0x1010000000000000, 0x1010100000000000, 0x1010001000000000, 0x1010101000000000, ),
    ( 0x0000000000000000, 0x0000000000800800, 0x0000000000008008, 0x0000000000808808,
      0x0000008008000000, 0x0000008008800800, 0x0000008008008008, 0x0000008008808808,
      0x0000000080080000, 0x0000000080880800, 0x0000000080088008, 0x0000000080888808,
      0x0000008088080000, 0x0000008088880800, 0x0000008088088008, 0x0000008088888808, ),
    ( 0x0000000000000000, 0x0080080000000000, 0x0000800800000000, 0x0080880800000000,
      0x0800000000000080, 0x0880080000000080, 0x0800800800000080, 0x0880880800000080,
      0x8008000000000000, 0x8088080000000000, 0x8008800800000000, 0x8088880800000000,
      0x8808000000000080, 0x8888080000000080, 0x8808800800000080, 0x8888880800000080, ),
        )
    #---------------------------------------------------
    # Table that combines the S, P, and E operations.
    #---------------------------------------------------
    SPE=(
    ( 0x0080088008200000, 0x0000008008000000, 0x0000000000200020, 0x0080088008200020,
      0x0000000000200000, 0x0080088008000020, 0x0000008008000020, 0x0000000000200020,
      0x0080088008000020, 0x0080088008200000, 0x0000008008200000, 0x0080080000000020,
      0x0080080000200020, 0x0000000000200000, 0x0000000000000000, 0x0000008008000020,
      0x0000008008000000, 0x0000000000000020, 0x0080080000200000, 0x0080088008000000,
      0x0080088008200020, 0x0000008008200000, 0x0080080000000020, 0x0080080000200000,
      0x0000000000000020, 0x0080080000000000, 0x0080088008000000, 0x0000008008200020,
      0x0080080000000000, 0x0080080000200020, 0x0000008008200020, 0x0000000000000000,
      0x0000000000000000, 0x0080088008200020, 0x0080080000200000, 0x0000008008000020,
      0x0080088008200000, 0x0000008008000000, 0x0080080000000020, 0x0080080000200000,
      0x0000008008200020, 0x0080080000000000, 0x0080088008000000, 0x0000000000200020,
      0x0080088008000020, 0x0000000000000020, 0x0000000000200020, 0x0000008008200000,
      0x0080088008200020, 0x0080088008000000, 0x0000008008200000, 0x0080080000200020,
      0x0000000000200000, 0x0080080000000020, 0x0000008008000020, 0x0000000000000000,
      0x0000008008000000, 0x0000000000200000, 0x0080080000200020, 0x0080088008200000,
      0x0000000000000020, 0x0000008008200020, 0x0080080000000000, 0x0080088008000020, ),
    ( 0x1000800810004004, 0x0000000000000000, 0x0000800810000000, 0x0000000010004004,
      0x1000000000004004, 0x1000800800000000, 0x0000800800004004, 0x0000800810000000,
      0x0000800800000000, 0x1000000010004004, 0x1000000000000000, 0x0000800800004004,
      0x1000000010000000, 0x0000800810004004, 0x0000000010004004, 0x1000000000000000,
      0x0000000010000000, 0x1000800800004004, 0x1000000010004004, 0x0000800800000000,
      0x1000800810000000, 0x0000000000004004, 0x0000000000000000, 0x1000000010000000,
      0x1000800800004004, 0x1000800810000000, 0x0000800810004004, 0x1000000000004004,
      0x0000000000004004, 0x0000000010000000, 0x1000800800000000, 0x1000800810004004,
      0x1000000010000000, 0x0000800810004004, 0x0000800800004004, 0x1000800810000000,
      0x1000800810004004, 0x1000000010000000, 0x1000000000004004, 0x0000000000000000,
      0x0000000000004004, 0x1000800800000000, 0x0000000010000000, 0x1000000010004004,
      0x0000800800000000, 0x0000000000004004, 0x1000800810000000, 0x1000800800004004,
      0x0000800810004004, 0x0000800800000000, 0x0000000000000000, 0x1000000000004004,
      0x1000000000000000, 0x1000800810004004, 0x0000800810000000, 0x0000000010004004,
      0x1000000010004004, 0x0000000010000000, 0x1000800800000000, 0x0000800800004004,
      0x1000800800004004, 0x1000000000000000, 0x0000000010004004, 0x0000800810000000, ),
    ( 0x0000000000400410, 0x0010004004400400, 0x0010000000000000, 0x0010000000400410,
      0x0000004004000010, 0x0000000000400400, 0x0010000000400410, 0x0010004004000000,
      0x0010000000400400, 0x0000004004000000, 0x0000004004400400, 0x0000000000000010,
      0x0010004004400410, 0x0010000000000010, 0x0000000000000010, 0x0000004004400410,
      0x0000000000000000, 0x0000004004000010, 0x0010004004400400, 0x0010000000000000,
      0x0010000000000010, 0x0010004004400410, 0x0000004004000000, 0x0000000000400410,
      0x0000004004400410, 0x0010000000400400, 0x0010004004000010, 0x0000004004400400,
      0x0010004004000000, 0x0000000000000000, 0x0000000000400400, 0x0010004004000010,
      0x0010004004400400, 0x0010000000000000, 0x0000000000000010, 0x0000004004000000,
      0x0010000000000010, 0x0000004004000010, 0x0000004004400400, 0x0010000000400410,
      0x0000000000000000, 0x0010004004400400, 0x0010004004000000, 0x0000004004400410,
      0x0000004004000010, 0x0000000000400400, 0x0010004004400410, 0x0000000000000010,
      0x0010004004000010, 0x0000000000400410, 0x0000000000400400, 0x0010004004400410,
      0x0000004004000000, 0x0010000000400400, 0x0010000000400410, 0x0010004004000000,
      0x0010000000400400, 0x0000000000000000, 0x0000004004400410, 0x0010000000000010,
      0x0000000000400410, 0x0010004004000010, 0x0010000000000000, 0x0000004004400400, ),
    ( 0x0800100040040080, 0x0000100000001000, 0x0800000000000080, 0x0800100040041080,
      0x0000000000000000, 0x0000000040041000, 0x0800100000001080, 0x0800000040040080,
      0x0000100040041000, 0x0800000000001080, 0x0000000000001000, 0x0800100000000080,
      0x0800000000001080, 0x0800100040040080, 0x0000000040040000, 0x0000000000001000,
      0x0800000040041080, 0x0000100040040000, 0x0000100000000000, 0x0800000000000080,
      0x0000100040040000, 0x0800100000001080, 0x0000000040041000, 0x0000100000000000,
      0x0800100000000080, 0x0000000000000000, 0x0800000040040080, 0x0000100040041000,
      0x0000100000001000, 0x0800000040041080, 0x0800100040041080, 0x0000000040040000,
      0x0800000040041080, 0x0800100000000080, 0x0000000040040000, 0x0800000000001080,
      0x0000100040040000, 0x0000100000001000, 0x0800000000000080, 0x0000000040041000,
      0x0800100000001080, 0x0000000000000000, 0x0000100000000000, 0x0800000040040080,
      0x0000000000000000, 0x0800000040041080, 0x0000100040041000, 0x0000100000000000,
      0x0000000000001000, 0x0800100040041080, 0x0800100040040080, 0x0000000040040000,
      0x0800100040041080, 0x0800000000000080, 0x0000100000001000, 0x0800100040040080,
      0x0800000040040080, 0x0000100040040000, 0x0000000040041000, 0x0800100000001080,
      0x0800100000000080, 0x0000000000001000, 0x0800000000001080, 0x0000100040041000, ),
    ( 0x0000000000800800, 0x0000001000000000, 0x0040040000000000, 0x2040041000800800,
      0x2000001000800800, 0x0040040000800800, 0x2040041000000000, 0x0000001000800800,
      0x0000001000000000, 0x2000000000000000, 0x2000000000800800, 0x0040041000000000,
      0x2040040000800800, 0x2000001000800800, 0x0040041000800800, 0x0000000000000000,
      0x0040041000000000, 0x0000000000800800, 0x2000001000000000, 0x2040040000000000,
      0x0040040000800800, 0x2040041000000000, 0x0000000000000000, 0x2000000000800800,
      0x2000000000000000, 0x2040040000800800, 0x2040041000800800, 0x2000001000000000,
      0x0000001000800800, 0x0040040000000000, 0x2040040000000000, 0x0040041000800800,
      0x0040041000800800, 0x2040040000800800, 0x2000001000000000, 0x0000001000800800,
      0x0000001000000000, 0x2000000000000000, 0x2000000000800800, 0x0040040000800800,
      0x0000000000800800, 0x0040041000000000, 0x2040041000800800, 0x0000000000000000,
      0x2040041000000000, 0x0000000000800800, 0x0040040000000000, 0x2000001000000000,
      0x2040040000800800, 0x0040040000000000, 0x0000000000000000, 0x2040041000800800,
      0x2000001000800800, 0x0040041000800800, 0x2040040000000000, 0x0000001000000000,
      0x0040041000000000, 0x2000001000800800, 0x0040040000800800, 0x2040040000000000,
      0x2000000000000000, 0x2040041000000000, 0x0000001000800800, 0x2000000000800800, ),
    ( 0x4004000000008008, 0x4004000020000000, 0x0000000000000000, 0x0000200020008008,
      0x4004000020000000, 0x0000200000000000, 0x4004200000008008, 0x0000000020000000,
      0x4004200000000000, 0x4004200020008008, 0x0000200020000000, 0x0000000000008008,
      0x0000200000008008, 0x4004000000008008, 0x0000000020008008, 0x4004200020000000,
      0x0000000020000000, 0x4004200000008008, 0x4004000020008008, 0x0000000000000000,
      0x0000200000000000, 0x4004000000000000, 0x0000200020008008, 0x4004000020008008,
      0x4004200020008008, 0x0000000020008008, 0x0000000000008008, 0x4004200000000000,
      0x4004000000000000, 0x0000200020000000, 0x4004200020000000, 0x0000200000008008,
      0x4004200000000000, 0x0000000000008008, 0x0000200000008008, 0x4004200020000000,
      0x0000200020008008, 0x4004000020000000, 0x0000000000000000, 0x0000200000008008,
      0x0000000000008008, 0x0000200000000000, 0x4004000020008008, 0x0000000020000000,
      0x4004000020000000, 0x4004200020008008, 0x0000200020000000, 0x4004000000000000,
      0x4004200020008008, 0x0000200020000000, 0x0000000020000000, 0x4004200000008008,
      0x4004000000008008, 0x0000000020008008, 0x4004200020000000, 0x0000000000000000,
      0x0000200000000000, 0x4004000000008008, 0x4004200000008008, 0x0000200020008008,
      0x0000000020008008, 0x4004200000000000, 0x4004000000000000, 0x4004000020008008, ),
    ( 0x0000400400000000, 0x0020000000000000, 0x0020000000100000, 0x0400000000100040,
      0x0420400400100040, 0x0400400400000040, 0x0020400400000000, 0x0000000000000000,
      0x0000000000100000, 0x0420000000100040, 0x0420000000000040, 0x0000400400100000,
      0x0400000000000040, 0x0020400400100000, 0x0000400400100000, 0x0420000000000040,
      0x0420000000100040, 0x0000400400000000, 0x0400400400000040, 0x0420400400100040,
      0x0000000000000000, 0x0020000000100000, 0x0400000000100040, 0x0020400400000000,
      0x0400400400100040, 0x0420400400000040, 0x0020400400100000, 0x0400000000000040,
      0x0420400400000040, 0x0400400400100040, 0x0020000000000000, 0x0000000000100000,
      0x0420400400000040, 0x0000400400100000, 0x0400400400100040, 0x0420000000000040,
      0x0000400400000000, 0x0020000000000000, 0x0000000000100000, 0x0400400400100040,
      0x0420000000100040, 0x0420400400000040, 0x0020400400000000, 0x0000000000000000,
      0x0020000000000000, 0x0400000000100040, 0x0400000000000040, 0x0020000000100000,
      0x0000000000000000, 0x0420000000100040, 0x0020000000100000, 0x0020400400000000,
      0x0420000000000040, 0x0000400400000000, 0x0420400400100040, 0x0000000000100000,
      0x0020400400100000, 0x0400000000000040, 0x0400400400000040, 0x0420400400100040,
      0x0400000000100040, 0x0020400400100000, 0x0000400400100000, 0x0400400400000040, ),
    ( 0x8008000080082000, 0x0000002080082000, 0x8008002000000000, 0x0000000000000000,
      0x0000002000002000, 0x8008000080080000, 0x0000000080082000, 0x8008002080082000,
      0x8008000000000000, 0x0000000000002000, 0x0000002080080000, 0x8008002000000000,
      0x8008002080080000, 0x8008002000002000, 0x8008000000002000, 0x0000000080082000,
      0x0000002000000000, 0x8008002080080000, 0x8008000080080000, 0x0000002000002000,
      0x8008002080082000, 0x8008000000002000, 0x0000000000000000, 0x0000002080080000,
      0x0000000000002000, 0x0000000080080000, 0x8008002000002000, 0x8008000080082000,
      0x0000000080080000, 0x0000002000000000, 0x0000002080082000, 0x8008000000000000,
      0x0000000080080000, 0x0000002000000000, 0x8008000000002000, 0x8008002080082000,
      0x8008002000000000, 0x0000000000002000, 0x0000000000000000, 0x0000002080080000,
      0x8008000080082000, 0x8008002000002000, 0x0000002000002000, 0x8008000080080000,
      0x0000002080082000, 0x8008000000000000, 0x8008000080080000, 0x0000002000002000,
      0x8008002080082000, 0x0000000080080000, 0x0000000080082000, 0x8008000000002000,
      0x0000002080080000, 0x8008002000000000, 0x8008002000002000, 0x0000000080082000,
      0x8008000000000000, 0x0000002080082000, 0x8008002080080000, 0x0000000000000000,
      0x0000000000002000, 0x8008000080082000, 0x0000002000000000, 0x8008002080080000, ),
        )
    #---------------------------------------------------
    # compressed/interleaved => final permutation table
    # Compression, final permutation, bit reverse
    #---------------------------------------------------
    CF6464=(
    ( 0x0000000000000000, 0x0000000000000040, 0x0000000000004000, 0x0000000000004040,
      0x0000000000400000, 0x0000000000400040, 0x0000000000404000, 0x0000000000404040,
      0x0000000040000000, 0x0000000040000040, 0x0000000040004000, 0x0000000040004040,
      0x0000000040400000, 0x0000000040400040, 0x0000000040404000, 0x0000000040404040, ),
    ( 0x0000000000000000, 0x0000000000000004, 0x0000000000000400, 0x0000000000000404,
      0x0000000000040000, 0x0000000000040004, 0x0000000000040400, 0x0000000000040404,
      0x0000000004000000, 0x0000000004000004, 0x0000000004000400, 0x0000000004000404,
      0x0000000004040000, 0x0000000004040004, 0x0000000004040400, 0x0000000004040404, ),
    ( 0x0000000000000000, 0x0000004000000000, 0x0000400000000000, 0x0000404000000000,
      0x0040000000000000, 0x0040004000000000, 0x0040400000000000, 0x0040404000000000,
      0x4000000000000000, 0x4000004000000000, 0x4000400000000000, 0x4000404000000000,
      0x4040000000000000, 0x4040004000000000, 0x4040400000000000, 0x4040404000000000, ),
    ( 0x0000000000000000, 0x0000000400000000, 0x0000040000000000, 0x0000040400000000,
      0x0004000000000000, 0x0004000400000000, 0x0004040000000000, 0x0004040400000000,
      0x0400000000000000, 0x0400000400000000, 0x0400040000000000, 0x0400040400000000,
      0x0404000000000000, 0x0404000400000000, 0x0404040000000000, 0x0404040400000000, ),
    ( 0x0000000000000000, 0x0000000000000010, 0x0000000000001000, 0x0000000000001010,
      0x0000000000100000, 0x0000000000100010, 0x0000000000101000, 0x0000000000101010,
      0x0000000010000000, 0x0000000010000010, 0x0000000010001000, 0x0000000010001010,
      0x0000000010100000, 0x0000000010100010, 0x0000000010101000, 0x0000000010101010, ),
    ( 0x0000000000000000, 0x0000000000000001, 0x0000000000000100, 0x0000000000000101,
      0x0000000000010000, 0x0000000000010001, 0x0000000000010100, 0x0000000000010101,
      0x0000000001000000, 0x0000000001000001, 0x0000000001000100, 0x0000000001000101,
      0x0000000001010000, 0x0000000001010001, 0x0000000001010100, 0x0000000001010101, ),
    ( 0x0000000000000000, 0x0000001000000000, 0x0000100000000000, 0x0000101000000000,
      0x0010000000000000, 0x0010001000000000, 0x0010100000000000, 0x0010101000000000,
      0x1000000000000000, 0x1000001000000000, 0x1000100000000000, 0x1000101000000000,
      0x1010000000000000, 0x1010001000000000, 0x1010100000000000, 0x1010101000000000, ),
    ( 0x0000000000000000, 0x0000000100000000, 0x0000010000000000, 0x0000010100000000,
      0x0001000000000000, 0x0001000100000000, 0x0001010000000000, 0x0001010100000000,
      0x0100000000000000, 0x0100000100000000, 0x0100010000000000, 0x0100010100000000,
      0x0101000000000000, 0x0101000100000000, 0x0101010000000000, 0x0101010100000000, ),
    ( 0x0000000000000000, 0x0000000000000080, 0x0000000000008000, 0x0000000000008080,
      0x0000000000800000, 0x0000000000800080, 0x0000000000808000, 0x0000000000808080,
      0x0000000080000000, 0x0000000080000080, 0x0000000080008000, 0x0000000080008080,
      0x0000000080800000, 0x0000000080800080, 0x0000000080808000, 0x0000000080808080, ),
    ( 0x0000000000000000, 0x0000000000000008, 0x0000000000000800, 0x0000000000000808,
      0x0000000000080000, 0x0000000000080008, 0x0000000000080800, 0x0000000000080808,
      0x0000000008000000, 0x0000000008000008, 0x0000000008000800, 0x0000000008000808,
      0x0000000008080000, 0x0000000008080008, 0x0000000008080800, 0x0000000008080808, ),
    ( 0x0000000000000000, 0x0000008000000000, 0x0000800000000000, 0x0000808000000000,
      0x0080000000000000, 0x0080008000000000, 0x0080800000000000, 0x0080808000000000,
      0x8000000000000000, 0x8000008000000000, 0x8000800000000000, 0x8000808000000000,
      0x8080000000000000, 0x8080008000000000, 0x8080800000000000, 0x8080808000000000, ),
    ( 0x0000000000000000, 0x0000000800000000, 0x0000080000000000, 0x0000080800000000,
      0x0008000000000000, 0x0008000800000000, 0x0008080000000000, 0x0008080800000000,
      0x0800000000000000, 0x0800000800000000, 0x0800080000000000, 0x0800080800000000,
      0x0808000000000000, 0x0808000800000000, 0x0808080000000000, 0x0808080800000000, ),
    ( 0x0000000000000000, 0x0000000000000020, 0x0000000000002000, 0x0000000000002020,
      0x0000000000200000, 0x0000000000200020, 0x0000000000202000, 0x0000000000202020,
      0x0000000020000000, 0x0000000020000020, 0x0000000020002000, 0x0000000020002020,
      0x0000000020200000, 0x0000000020200020, 0x0000000020202000, 0x0000000020202020, ),
    ( 0x0000000000000000, 0x0000000000000002, 0x0000000000000200, 0x0000000000000202,
      0x0000000000020000, 0x0000000000020002, 0x0000000000020200, 0x0000000000020202,
      0x0000000002000000, 0x0000000002000002, 0x0000000002000200, 0x0000000002000202,
      0x0000000002020000, 0x0000000002020002, 0x0000000002020200, 0x0000000002020202, ),
    ( 0x0000000000000000, 0x0000002000000000, 0x0000200000000000, 0x0000202000000000,
      0x0020000000000000, 0x0020002000000000, 0x0020200000000000, 0x0020202000000000,
      0x2000000000000000, 0x2000002000000000, 0x2000200000000000, 0x2000202000000000,
      0x2020000000000000, 0x2020002000000000, 0x2020200000000000, 0x2020202000000000, ),
    ( 0x0000000000000000, 0x0000000200000000, 0x0000020000000000, 0x0000020200000000,
      0x0002000000000000, 0x0002000200000000, 0x0002020000000000, 0x0002020200000000,
      0x0200000000000000, 0x0200000200000000, 0x0200020000000000, 0x0200020200000000,
      0x0202000000000000, 0x0202000200000000, 0x0202020000000000, 0x0202020200000000, ),
    )
    #=========================================================
    #eof load_data
    #=========================================================

#=========================================================
#helpers
#=========================================================


def perm6464(c, p):
    """Returns the permutation of the given 64-bit code with
    the specified permutataion table."""
    out = 0
    for i in RR8:
        out |= p[i<<1][c&0x0f] | p[(i<<1)+1][(c>>4) & 0x0f]
        c >>= 8
    return out

def perm3264(c, p):
    """Returns the permutation of the given 32-bit code with
    the specified permutataion table."""
    out = 0
    for i in RR4:
        out |= p[i<<1][c&0x0f] | p[(i<<1)+1][(c>>4)&0x0f]
        c >>= 8
    return out

def des_setkey(keyword):
    "Returns the key schedule for the given key."
    def _gen(K):
        for p in PCXROT:
            K = perm6464(K, p)
            yield K & ~0x0303030300000000
    return list(_gen(keyword))

def to_six_bit_int(num):
        return (((num << 26) & 0xfc000000) | ((num << 12) & 0xfc0000) |
                ((num >> 2) & 0xfc00) | ((num >> 16) & 0xfc))

def des_cipher(input, salt, num_iter, KS):
    """Returns the DES encrypted code of the given word with the specified environment."""
    salt = to_six_bit_int(salt)
    R = L = input
    L &= 0x5555555555555555
    R = (R & 0xaaaaaaaa00000000L) | ((R >> 1) & 0x0000000055555555L)
    L = ((((L << 1) | (L << 32)) & 0xffffffff00000000L) |
         ((R | (R >> 32)) & 0x00000000ffffffffL))

    L = perm3264((L>>32), IE3264)
    R = perm3264((L&0xffffffff), IE3264)

    #run specified number of passed
    while num_iter >= 1:
        num_iter -= 1
        #run over each part of the schedule
        for loop_count in R8:
            kp = KS[(loop_count<<1)]
            k = ((R>>32) ^ R) & salt & 0xffffffff
            k |= (k<<32)
            B = (k ^ R ^ kp)

            L ^= (SPE[0][(B>>58)&0x3f] ^ SPE[1][(B>>50)&0x3f] ^
                  SPE[2][(B>>42)&0x3f] ^ SPE[3][(B>>34)&0x3f] ^
                  SPE[4][(B>>26)&0x3f] ^ SPE[5][(B>>18)&0x3f] ^
                  SPE[6][(B>>10)&0x3f] ^ SPE[7][(B>>2)&0x3f])

            kp = KS[(loop_count<<1)+1]
            k = ((L>>32) ^ L) & salt & 0xffffffff
            k |= (k<<32)
            B = (k ^ L ^ kp)

            R ^= (SPE[0][(B>>58)&0x3f] ^ SPE[1][(B>>50)&0x3f] ^
                  SPE[2][(B>>42)&0x3f] ^ SPE[3][(B>>34)&0x3f] ^
                  SPE[4][(B>>26)&0x3f] ^ SPE[5][(B>>18)&0x3f] ^
                  SPE[6][(B>>10)&0x3f] ^ SPE[7][(B>>2)&0x3f])

        # swap L and R
        L, R = R, L

    L = ((((L>>35) & 0x0f0f0f0fL) | (((L&0xffffffff)<<1) & 0xf0f0f0f0L))<<32 |
         (((R>>35) & 0x0f0f0f0fL) | (((R&0xffffffff)<<1) & 0xf0f0f0f0L)))

    L = perm6464(L, CF6464)

    return L

#=========================================================
#frontend
#=========================================================

def crypt(key, salt):
    "encrypt string using unix-crypt (des) algorithm"
    if PCXROT is None:
        load_tables()

    if '\x00' in key:
        #builtin linux crypt doesn't like this,
        #so we don't either
        raise ValueError, "key must not contain null characters"

    #convert key string into an integer
    if len(key) < 8:
        key = key + '\x00' * 8
    key_value = 0
    for c in key[:8]:
        key_value <<= 8
        key_value |= 2 * ord(c)

    #convert key int -> key schedule
    key_sched = des_setkey(key_value)

    #parse salt into int
    if not salt:
        sa = sb = '.' #no std behavior in this case
    elif len(salt) < 2:
        sa = sb = salt #linux's behavior, probably no real standard
    else:
        sa, sb = salt[0:2]
    salt_value = (b64_decode(sb) << 6) + b64_decode(sa)
        #FIXME: ^ this will throw a KeyError if bad salt chars are used
        # whereas linux crypt does something with it

    #run data through des using input of 0
    result = des_cipher(0, salt_value, 25, key_sched)

    #run b64 encode on result
    out = [sa, sb] + [None] * 10 + [ b64_encode((result<<2)&0x3f) ]
    result >>= 4
    for i in RR12_1:
        out[i] = b64_encode(result&0x3f)
        result >>= 6
    return "".join(out)

#=========================================================
#eof
#=========================================================