-
Notifications
You must be signed in to change notification settings - Fork 0
/
ignode.py
executable file
·2821 lines (2597 loc) · 144 KB
/
ignode.py
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Welcome to ⊔net!
#-----------------------------------------------------------------------------
# © Copyright 2013 ⊔Foundation
# This file is part of ⊔net.
#
# ⊔net is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ⊔net is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ⊔net. If not, see <http://www.gnu.org/licenses/>.
#
# Acknowledgements:
# * ECDSA has been adapted to Python3 and simplified for 512P curve only
# code inspired from:
# Brian Warner
# * The PyCrypt library is far too complex for our needs so we used a code
# for AES inspired from:
# Josh Davis ( http://www.josh-davis.org )
# Laurent Haan (http://www.progressive-coding.com)
# * QRcode is extented to PDF and SVG from the inspired code of:
# Sam Curren (porting from Javascript)
# * Encryption with ECC use an idea of jackjack-jj on github
#-----------------------------------------------------------------------------
# Short todo list #
# - date presentation as Twitter
# - shift sha1 to sha256 in for signing
# - Frontend for 'Maires' (add change password, user reset)
# - Fix mairies list (10% errors)
# - Finish eurofranc/2015 pages
# - Prepare letter (both text and PDF) for Maires
# - use cookie for name when available
# - encapsulate for standalone Windows exe
import re, os, sys, urllib.parse, hashlib, http.client, base64, dbm.ndbm, datetime, functools, subprocess, time, smtplib, operator, random, getpass
import PyQt4.QtGui # only for GUI
__digest__ = base64.urlsafe_b64encode(hashlib.sha1(open(__file__, 'r', encoding='utf8').read().encode('utf8')).digest())[:10]
__app__ = os.path.basename(__file__)[:-3]
__dfprt__ = 80
__base__ = '/%s/%s_%s/' % (__app__, __app__, __dfprt__)
__ppc__ = 'eurofranc'
__email__ = 'contact@%s.fr' % __ppc__
_SVGNS = 'xmlns="http://www.w3.org/2000/svg"'
_b58char = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
_root_id = 'AdminJqjFdcY'
_root_pkey = 'AdMctT3bXbwrTBGkB5eKAG74qIqShRRy1nHa_NWCHsxmKhmZeE_aWgo_S251td8d6C5uti6TymQSSZvhmO1b19pI/AYYPFxkKL_13dnhBGXdFdmDQhQEZZbc1P7GDDrZZwU0FSGuwc53_AxHk1vVRte7bdmhzIcOUMUvO'
_cur = {'€':b'A', '£':b'P', '$':b'D', '⊔':b'U', 'C':b'C'}
##### ENCODING #####
PAD = lambda s:(len(s)%2)*'0'+s[2:]
def i2b(x, n=1):
"int to bytes with n padding"
z = bytes.fromhex(PAD(hex(x)))
return ((n-len(z))%n)*bytes.fromhex('00') + z
def b2i(x):
"bytes to int"
return int.from_bytes(x, 'big')
def s2b(x, n=1):
"signed int to bytes with n padding"
z = bytes.fromhex(PAD(hex(x + (1<<(8*n-1)))))
return ((n-len(z))%n)*bytes.fromhex('00') + z
def b2s(x, n=1):
"signed bytes to int"
return int.from_bytes(x, 'big') - (1<<(8*n-1))
def itob64(n):
"transform int to base64"
return re.sub(b'=*$', b'', base64.urlsafe_b64encode(bytes.fromhex(PAD(hex(n)))))
def b64toi(c):
"transform base64 to int"
if c == '': return 0
return int.from_bytes(base64.urlsafe_b64decode(c + b'='*((4-(len(c)%4))%4)), 'big')
def btob64(c):
"bytes to base64"
return base64.urlsafe_b64encode(c).decode('ascii').strip('=')
def b64tob(c):
"base64 to bytes"
return base64.urlsafe_b64decode(c + b'='*((4-(len(c)%4))%4))
def H(*tab):
"hash"
return int(hashlib.sha256(b''.join(tab)).hexdigest(), 16)
def datencode(n=0):
"4 chars (minute precision)"
return i2b(int(time.mktime(time.gmtime())/60 + 60*24*n), 4)
def datdecode(tt):
"4 chars (minute precision)"
return time.strftime('%d/%m/%y %H:%M', time.localtime(float(b2i(tt)*60)))
def is_after(d1, d2):
"_"
return datdecode(d1) > datdecode(d2)
def random_b64():
"20 chars url safe"
return base64.urlsafe_b64encode(bytes.fromhex(hashlib.sha1(os.urandom(32)).hexdigest()[:30]))
def hcode(m, s=10):
"_"
return (hashlib.sha1(m.encode('utf8')).digest())[:s]
def valencode(xi, p1, pf):
"xi:7/p1:17/pf:32"
assert (2*p1 < pf or xi==0) and xi<=100 and p1<(1<<17) and pf<(1<<32)
return i2b((xi<<49) + (p1<<32) + pf, 7)
def valdecode(code):
"xi:7/p1:17/pf:32"
e = int.from_bytes(code, 'big')
return ((e>>49) & 0x7F), ((e>>32) & 0x1FFFF), (e & 0xFFFFFFFF)
### OLD
def valencode1(xi, p1, pf):
"xi:7/p1:15/pf:26"
assert (2*p1 < pf or xi==0) and xi<=100 and p1<(1<<15) and pf<(1<<26)
return i2b((xi<<41) + (p1<<26) + pf, 6)
def valdecode1(code):
"xi:7/p1:15/pf:26"
e = int.from_bytes(code, 'big')
return ((e>>41) & 0x7F), ((e>>26) & 0x7FFF), (e & 0x3FFFFFF)
###
##### ECDSA NIST CURVE P-521 #####
_B = b'UZU-uWGOHJofkpohoLaFQO6i2nJbmbMV87i0iZGO8QnhVhk5Uex-k3sWUsC9O7G_BzVz34g9LDTx70Uf1GtQPwA'
_GX = b'xoWOBrcEBOnNnj7LZiOVtEKcZIE5BT-1Ifgor2BrTT26oUted-_nWSj-HcEnov-o3jNIs8GFakKb-X5-McLlvWY'
_GY = b'ARg5KWp4mjvABFyKX7QsfRvZmPVESVebRGgXr70XJz5mLJfucple9CZAxVC5AT-tB2E1PHCGonLCQIi-lHaf0WZQ'
_R = b'Af' + b'_'*42 + b'-lGGh4O_L5Zrf8wBSPcJpdA7tcm4iZxHrrtvtx6ROGQJ'
class Curve():
"The curve of points satisfying y^2 = x^3 + a*x + b (mod p)"
def __init__(self, p, a, b): self.p, self.a, self.b = p, a, b
def has_pt(self, x, y): return (y*y - (x*x*x + self.a*x + self.b)) % self.p == 0
c521 = Curve(b64toi(b'Af' + b'_'*86), -3, b64toi(_B))
class Point():
def __init__(self, curve, x, y, order = None):
self.curve, self.x, self.y, self.order = curve, x, y, order
def __add__(self, other):
if other == INFINITY: return self
if self == INFINITY: return other
if self.x == other.x:
if (self.y + other.y) % self.curve.p == 0: return INFINITY
else: return self.double()
p = self.curve.p
l = ((other.y - self.y) * inverse_mod(other.x - self.x, p)) % p
x3 = (l*l - self.x - other.x) % p
y3 = (l*(self.x - x3) - self.y) % p
return Point(self.curve, x3, y3)
def __mul__(self, e):
if self.order: e = e % self.order
if e == 0 or self == INFINITY: return INFINITY
e3, neg_self = 3*e, Point(self.curve, self.x, -self.y, self.order)
i = 1 << (len(bin(e3))-4)
result = self
while i > 1:
result = result.double()
if (e3 & i) != 0 and (e & i) == 0: result = result + self
if (e3 & i) == 0 and (e & i) != 0: result = result + neg_self
i //= 2
return result
def __rmul__(self, other): return self * other
def double(self):
if self == INFINITY: return INFINITY
p, a = self.curve.p, self.curve.a
l = ((3 * self.x * self.x + a) * inverse_mod(2 * self.y, p)) % p
x3 = (l*l - 2 * self.x) % p
y3 = (l*(self.x - x3) - self.y) % p
return Point(self.curve, x3, y3)
INFINITY = Point(None, None, None)
class ecdsa:
def __init__(self):
self.gen = Point(c521, b64toi(_GX), b64toi(_GY), b64toi(_R))
self.pkgenerator, self.pkorder = self.gen, self.gen.order
def generate(self):
secexp = randrange(self.gen.order)
pp = self.gen*secexp
self.pt, n = pp, self.gen.order
if not n: raise 'Generator point must have order!'
if not n * pp == INFINITY: raise 'Bad Generator point order!'
if pp.x < 0 or n <= pp.x or pp.y < 0 or n <= pp.y: raise 'Out of range!'
self.privkey = secexp
def verify(self, sig, data):
r, s, G, n = b2i(sig[:66]), b2i(sig[66:]), self.pkgenerator, self.pkorder
if r < 1 or r > n-1 or s < 1 or s > n-1: return False
c = inverse_mod(s, n)
u1, u2 = (H(data) * c) % n, (r * c) % n
z = u1 * G + u2 * self.pt
return z.x % n == r
def sign(self, data):
rk, G, n = randrange(self.pkorder), self.pkgenerator, self.pkorder
k = rk % n
p1 = k * G
r = p1.x
s = (inverse_mod(k, n) * (H(data) + (self.privkey * r) % n)) % n
assert s != 0 and r != 0
return i2b(r, 66) + i2b(s, 66)
def find_offset(self, x):
p, a, b = b64toi(b'Af' + b'_'*86), -3, b64toi(_B)
for offset in range(64):
Mx = x + offset
My2 = pow(Mx, 3, p) + a * Mx + b % p
My = pow(My2, (p+1)//4, p)
if c521.has_pt(Mx, My): return offset, My
raise Exception('Y Not found')
def encrypt(self, data):
p, a, b, G, x = b64toi(b'Af' + b'_'*86), -3, b64toi(_B), self.pkgenerator, int.from_bytes(data, 'big')
offset, y = self.find_offset(x)
M, k = Point(c521, x + offset, y), randrange(self.pkorder)
p1, p2 = k*G, M + k*self.pt
o1, o2 = p1.y&1, p2.y&1
return bytes('%02X' % ((o1<<7) + (o2<<6) + offset), 'ascii') + i2b(p1.x, 66) + i2b(p2.x, 66)
def decrypt(self, enctxt):
oo, x1, x2 = int(enctxt[:2], 16), b2i(enctxt[2:68]), b2i(enctxt[68:])
o1, o2, offset, p, a, b = (oo & 0x80)>>7,(oo & 0x40)>>6, oo & 0x3F, b64toi(b'Af' + b'_'*86), -3, b64toi(_B)
z1, z2 = pow(x1, 3, p) + a * x1 + b % p, pow(x2, 3, p) + a * x2 + b % p
t1, t2 = pow(z1, (p+1)//4, p), pow(z2, (p+1)//4, p)
y1, y2 = t1 if int(o1) == t1&1 else (-t1)%p, t2 if int(o2) == t2&1 else (-t2)%p
p1, p2 = Point(c521, x1, - y1), Point(c521, x2, y2)
u = p2 + self.privkey * p1
return i2b(u.x-offset)
def inverse_mod(a, m):
"_"
if a < 0 or m <= a: a = a % m
c, d = a, m
uc, vc, ud, vd = 1, 0, 0, 1
while c != 0:
q, c, d = divmod(d, c) + (c,)
uc, vc, ud, vd = ud - q*uc, vd - q*vc, uc, vc
if ud > 0: return ud
else: return ud + m
def randrange(order):
"_"
byts = (1+len('%x' % order))//2
cand = b2i(os.urandom(byts))
return cand//2 if cand >= order else cand
##### LOCAL AES-256 ##### (replace PyCrypto AES)
_IV = b'ABCDEFGHIJKLMNOP'
class AES:
sbox = [
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0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb ]
def rotate(self, word):
c = word[0]
for i in range(3): word[i] = word[i+1]
word[3] = c
return word
def core(self, word, iteration):
word = self.rotate(word)
for i in range(4): word[i] = self.sbox[word[i]]
word[0] = word[0]^self.Rcon[iteration]
return word
def expandKey(self, key, expandedKeySize):
currentSize, rconIteration, t, expandedKey = 32, 1, [0]*4, [0]*expandedKeySize
for j in range(32): expandedKey[j] = key[j]
while currentSize < expandedKeySize:
for k in range(4): t[k] = expandedKey[(currentSize - 4) + k]
if currentSize % 32 == 0:
t = self.core(t, rconIteration)
rconIteration += 1;
if (currentSize % 32) == 16:
for l in range(4): t[l] = self.sbox[t[l]]
for m in range(4):
expandedKey[currentSize] = expandedKey[currentSize - 32] ^ t[m]
currentSize += 1
return expandedKey
def addRoundKey(self, state, roundKey):
for i in range(16): state[i] ^= roundKey[i]
return state
def createRoundKey(self, expandedKey, roundKeyPointer):
return [expandedKey[roundKeyPointer+i*4+j] for j in range(4) for i in range(4)]
def g_mult(self, a, b):
p = 0
for counter in range(8):
if (b & 1) == 1: p ^= a
if p > 0x100: p ^= 0x100
hi_bit_set = (a & 0x80)
a <<= 1
if a > 0x100: a ^= 0x100
if hi_bit_set == 0x80: a ^= 0x1b
if a > 0x100: a ^= 0x100
b >>= 1
if b > 0x100: b ^= 0x100
return p
def subBytes(self, state):
for i in range(16): state[i] = self.sbox[state[i]]
return state
def shiftRows(self, state):
for i in range(4): state = self.shiftRow(state, i*4, i)
return state
def shiftRow(self, state, pointer, nbr):
for i in range(nbr):
tmp = state[pointer]
for j in range(3): state[pointer + j] = state[pointer + j+1]
state[pointer + 3] = tmp
return state
def mixColumns(self, state):
column = [0]*4
for i in range(4):
for j in range(4): column[j] = state[(j*4)+i]
column = self.mixColumn(column)
for k in range(4): state[(k*4)+i] = column[k]
return state;
def mixColumn(self, col):
mult, cpy = [2, 1, 1, 3], [0]*4
for i in range(4): cpy[i] = col[i]
col[0] = self.g_mult(cpy[0], mult[0]) ^ self.g_mult(cpy[3], mult[1]) ^ self.g_mult(cpy[2], mult[2]) ^ self.g_mult(cpy[1], mult[3])
col[1] = self.g_mult(cpy[1], mult[0]) ^ self.g_mult(cpy[0], mult[1]) ^ self.g_mult(cpy[3], mult[2]) ^ self.g_mult(cpy[2], mult[3])
col[2] = self.g_mult(cpy[2], mult[0]) ^ self.g_mult(cpy[1], mult[1]) ^ self.g_mult(cpy[0], mult[2]) ^ self.g_mult(cpy[3], mult[3])
col[3] = self.g_mult(cpy[3], mult[0]) ^ self.g_mult(cpy[2], mult[1]) ^ self.g_mult(cpy[1], mult[2]) ^ self.g_mult(cpy[0], mult[3])
return col
def aes_round(self, state, roundKey):
state = self.subBytes(state)
state = self.shiftRows(state)
state = self.mixColumns(state)
state = self.addRoundKey(state, roundKey)
return state
def aes_main(self, state, expandedKey, nbrRounds):
state, i = self.addRoundKey(state, self.createRoundKey(expandedKey, 0)), 1
for i in range(1, nbrRounds): state = self.aes_round(state, self.createRoundKey(expandedKey, 16*i))
state = self.subBytes(state)
state = self.shiftRows(state)
state = self.addRoundKey(state, self.createRoundKey(expandedKey, 16*nbrRounds))
return state
def enc(self, iput, key):
output, block, nbrRounds = [0]*16, [0]*16, 14
expandedKeySize = (16*(nbrRounds+1))
for i in range(4):
for j in range(4): block[(i+(j*4))] = iput[(i*4)+j]
expandedKey = self.expandKey(key, expandedKeySize)
block = self.aes_main(block, expandedKey, nbrRounds)
for k in range(4):
for l in range(4): output[(k*4)+l] = block[(k+(l*4))]
return output
def encrypt(self, stringIn, key):
plain, iput, output, cipher = [], [], [], [0]*16
cipherOut, firstRound = [], True
if stringIn != None:
for j in range(1+(len(stringIn)-1)//16):
start, end = j*16, j*16+16
if j*16+16 > len(stringIn): end = len(stringIn)
plain = (stringIn.encode('utf8'))[start:end]
if firstRound: output, firstRound = self.enc(_IV, key), False
else: output = self.enc(iput, key)
for i in range(16):
if len(plain)-1 < i: cipher[i] = 0 ^ output[i]
elif len(output)-1 < i: cipher[i] = plain[i] ^ 0
elif len(plain)-1 < i and len(output) < i: cipher[i] = 0 ^ 0
else: cipher[i] = plain[i] ^ output[i]
for k in range(end-start): cipherOut.append(cipher[k])
iput = output
return base64.urlsafe_b64encode(bytes(cipherOut))
def decrypt(self, cIn, key):
cipher, iput, output, plain, stringOut, fround, cipherIn = [], [], [], [0]*16, '', True, [i for i in base64.urlsafe_b64decode(cIn)]
if cipherIn != None:
for j in range(1+(len(cipherIn)-1)//16):
start, end = j*16, j*16+16
if j*16+16 > len(cipherIn): end = len(cipherIn)
cipher = cipherIn[start:end]
if fround: output, fround = self.enc(_IV, key), False
else: output = self.enc(iput, key)
for i in range(16):
if len(output)-1 < i: plain[i] = 0 ^ cipher[i]
elif len(cipher)-1 < i: plain[i] = output[i] ^ 0
elif len(output)-1 < i and len(cipher) < i: plain[i] = 0 ^ 0
else: plain[i] = output[i] ^ cipher[i]
for k in range(end-start): stringOut += chr(plain[k])
iput = output
return stringOut.rstrip('@');
##### QRCODE #####
ERR_COR_L, ERR_COR_M, ERR_COR_Q, ERR_COR_H = 1, 0, 3, 2
MODE_NUMBER, MODE_ALPHA_NUM, MODE_8BIT_BYTE, MODE_KANJI = 1, 2, 4, 8
MODE_SIZE_SMALL = { MODE_NUMBER: 10, MODE_ALPHA_NUM: 9, MODE_8BIT_BYTE: 8, MODE_KANJI: 8,}
MODE_SIZE_MEDIUM = { MODE_NUMBER: 12, MODE_ALPHA_NUM: 11, MODE_8BIT_BYTE: 16, MODE_KANJI: 10,}
MODE_SIZE_LARGE = { MODE_NUMBER: 14, MODE_ALPHA_NUM: 13, MODE_8BIT_BYTE: 16, MODE_KANJI: 12,}
ALPHA_NUM = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:'
TEXP = list(range(256))
TLOG = list(range(256))
for i in range(8): TEXP[i] = 1 << i
for i in range(8, 256): TEXP[i] = (TEXP[i - 4] ^ TEXP[i - 5] ^ TEXP[i - 6] ^ TEXP[i - 8])
for i in range(255): TLOG[TEXP[i]] = i
NUMBER_LENGTH = {3: 10, 2: 7, 1: 4}
RS_BLOCK_OFFSET = { ERR_COR_L: 0, ERR_COR_M: 1, ERR_COR_Q: 2, ERR_COR_H: 3 }
RS_BLOCK_TABLE = [ # L M Q H
[1, 26, 19], [1, 26, 16], [1, 26, 13], [1, 26, 9],
[1, 44, 34], [1, 44, 28], [1, 44, 22], [1, 44, 16],
[1, 70, 55], [1, 70, 44], [2, 35, 17], [2, 35, 13],
[1, 100, 80],[2, 50, 32], [2, 50, 24], [4, 25, 9],
[1, 134, 108], [2, 67, 43], [2, 33, 15, 2, 34, 16], [2, 33, 11, 2, 34, 12],
[2, 86, 68], [4, 43, 27], [4, 43, 19], [4, 43, 15],
[2, 98, 78], [4, 49, 31], [2, 32, 14, 4, 33, 15], [4, 39, 13, 1, 40, 14],
[2, 121, 97], [2, 60, 38, 2, 61, 39], [4, 40, 18, 2, 41, 19], [4, 40, 14, 2, 41, 15],
[2, 146, 116], [3, 58, 36, 2, 59, 37], [4, 36, 16, 4, 37, 17], [4, 36, 12, 4, 37, 13],
[2, 86, 68, 2, 87, 69], [4, 69, 43, 1, 70, 44], [6, 43, 19, 2, 44, 20], [6, 43, 15, 2, 44, 16],
[4, 101, 81], [1, 80, 50, 4, 81, 51], [4, 50, 22, 4, 51, 23], [3, 36, 12, 8, 37, 13],
[2, 116, 92, 2, 117, 93], [6, 58, 36, 2, 59, 37], [4, 46, 20, 6, 47, 21], [7, 42, 14, 4, 43, 15],
[4, 133, 107], [8, 59, 37, 1, 60, 38], [8, 44, 20, 4, 45, 21], [12, 33, 11, 4, 34, 12],
[3, 145, 115, 1, 146, 116], [4, 64, 40, 5, 65, 41], [11, 36, 16, 5, 37, 17], [11, 36, 12, 5, 37, 13],
[5, 109, 87, 1, 110, 88], [5, 65, 41, 5, 66, 42], [5, 54, 24, 7, 55, 25], [11, 36, 12],
[5, 122, 98, 1, 123, 99], [7, 73, 45, 3, 74, 46], [15, 43, 19, 2, 44, 20], [3, 45, 15, 13, 46, 16],
[1, 135, 107, 5, 136, 108], [10, 74, 46, 1, 75, 47], [1, 50, 22, 15, 51, 23], [2, 42, 14, 17, 43, 15],
[5, 150, 120, 1, 151, 121], [9, 69, 43, 4, 70, 44], [17, 50, 22, 1, 51, 23], [2, 42, 14, 19, 43, 15],
[3, 141, 113, 4, 142, 114], [3, 70, 44, 11, 71, 45], [17, 47, 21, 4, 48, 22], [9, 39, 13, 16, 40, 14],
[3, 135, 107, 5, 136, 108], [3, 67, 41, 13, 68, 42], [15, 54, 24, 5, 55, 25], [15, 43, 15, 10, 44, 16],
[4, 144, 116, 4, 145, 117], [17, 68, 42], [17, 50, 22, 6, 51, 23], [19, 46, 16, 6, 47, 17],
[2, 139, 111, 7, 140, 112], [17, 74, 46], [7, 54, 24, 16, 55, 25], [34, 37, 13],
[4, 151, 121, 5, 152, 122], [4, 75, 47, 14, 76, 48], [11, 54, 24, 14, 55, 25], [16, 45, 15, 14, 46, 16],
[6, 147, 117, 4, 148, 118], [6, 73, 45, 14, 74, 46], [11, 54, 24, 16, 55, 25], [30, 46, 16, 2, 47, 17],
[8, 132, 106, 4, 133, 107], [8, 75, 47, 13, 76, 48], [7, 54, 24, 22, 55, 25], [22, 45, 15, 13, 46, 16],
[10, 142, 114, 2, 143, 115], [19, 74, 46, 4, 75, 47], [28, 50, 22, 6, 51, 23], [33, 46, 16, 4, 47, 17],
[8, 152, 122, 4, 153, 123], [22, 73, 45, 3, 74, 46], [8, 53, 23, 26, 54, 24], [12, 45, 15, 28, 46, 16],
[3, 147, 117, 10, 148, 118], [3, 73, 45, 23, 74, 46], [4, 54, 24, 31, 55, 25], [11, 45, 15, 31, 46, 16],
[7, 146, 116, 7, 147, 117], [21, 73, 45, 7, 74, 46], [1, 53, 23, 37, 54, 24], [19, 45, 15, 26, 46, 16],
[5, 145, 115, 10, 146, 116], [19, 75, 47, 10, 76, 48], [15, 54, 24, 25, 55, 25], [23, 45, 15, 25, 46, 16],
[13, 145, 115, 3, 146, 116], [2, 74, 46, 29, 75, 47], [42, 54, 24, 1, 55, 25], [23, 45, 15, 28, 46, 16],
[17, 145, 115], [10, 74, 46, 23, 75, 47], [10, 54, 24, 35, 55, 25], [19, 45, 15, 35, 46, 16],
[17, 145, 115, 1, 146, 116], [14, 74, 46, 21, 75, 47], [29, 54, 24, 19, 55, 25], [11, 45, 15, 46, 46, 16],
[13, 145, 115, 6, 146, 116], [14, 74, 46, 23, 75, 47], [44, 54, 24, 7, 55, 25], [59, 46, 16, 1, 47, 17],
[12, 151, 121, 7, 152, 122], [12, 75, 47, 26, 76, 48], [39, 54, 24, 14, 55, 25], [22, 45, 15, 41, 46, 16],
[6, 151, 121, 14, 152, 122], [6, 75, 47, 34, 76, 48], [46, 54, 24, 10, 55, 25], [2, 45, 15, 64, 46, 16],
[17, 152, 122, 4, 153, 123], [29, 74, 46, 14, 75, 47], [49, 54, 24, 10, 55, 25], [24, 45, 15, 46, 46, 16],
[4, 152, 122, 18, 153, 123], [13, 74, 46, 32, 75, 47], [48, 54, 24, 14, 55, 25], [42, 45, 15, 32, 46, 16],
[20, 147, 117, 4, 148, 118], [40, 75, 47, 7, 76, 48], [43, 54, 24, 22, 55, 25], [10, 45, 15, 67, 46, 16],
[19, 148, 118, 6, 149, 119], [18, 75, 47, 31, 76, 48], [34, 54, 24, 34, 55, 25], [20, 45, 15, 61, 46, 16]
]
PATTERN_POSITION_TABLE = [
[], [6, 18], [6, 22], [6, 26], [6, 30], [6, 34],
[6, 22, 38], [6, 24, 42], [6, 26, 46], [6, 28, 50], [6, 30, 54], [6, 32, 58], [6, 34, 62],
[6, 26, 46, 66], [6, 26, 48, 70], [6, 26, 50, 74], [6, 30, 54, 78], [6, 30, 56, 82], [6, 30, 58, 86], [6, 34, 62, 90],
[6, 28, 50, 72, 94], [6, 26, 50, 74, 98], [6, 30, 54, 78, 102], [6, 28, 54, 80, 106], [6, 32, 58, 84, 110], [6, 30, 58, 86, 114], [6, 34, 62, 90, 118],
[6, 26, 50, 74, 98, 122], [6, 30, 54, 78, 102, 126], [6, 26, 52, 78, 104, 130], [6, 30, 56, 82, 108, 134],
[6, 34, 60, 86, 112, 138], [6, 30, 58, 86, 114, 142], [6, 34, 62, 90, 118, 146],
[6, 30, 54, 78, 102, 126, 150], [6, 24, 50, 76, 102, 128, 154], [6, 28, 54, 80, 106, 132, 158], [6, 32, 58, 84, 110, 136, 162],
[6, 26, 54, 82, 110, 138, 166], [6, 30, 58, 86, 114, 142, 170]
]
G15 = ((1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0))
G18 = ((1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0))
G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1)
def BCH_type_info(data):
d = data << 10
while BCH_digit(d) - BCH_digit(G15) >= 0: d ^= (G15 << (BCH_digit(d) - BCH_digit(G15)))
return ((data << 10) | d) ^ G15_MASK
def BCH_type_number(data):
d = data << 12
while BCH_digit(d) - BCH_digit(G18) >= 0: d ^= (G18 << (BCH_digit(d) - BCH_digit(G18)))
return (data << 12) | d
def BCH_digit(data):
digit = 0
while data != 0:
digit += 1
data >>= 1
return digit
def pattern_position(version):
return PATTERN_POSITION_TABLE[version - 1]
def mask_func(pattern):
if pattern == 0: return lambda i, j: (i + j) % 2 == 0
if pattern == 1: return lambda i, j: i % 2 == 0
if pattern == 2: return lambda i, j: j % 3 == 0
if pattern == 3: return lambda i, j: (i + j) % 3 == 0
if pattern == 4: return lambda i, j: (int(i / 2) + int(j / 3)) % 2 == 0
if pattern == 5: return lambda i, j: (i * j) % 2 + (i * j) % 3 == 0
if pattern == 6: return lambda i, j: ((i * j) % 2 + (i * j) % 3) % 2 == 0
if pattern == 7: return lambda i, j: ((i * j) % 3 + (i + j) % 2) % 2 == 0
def length_in_bits(mode, version):
if version < 10: mode_size = MODE_SIZE_SMALL
elif version < 27: mode_size = MODE_SIZE_MEDIUM
else: mode_size = MODE_SIZE_LARGE
return mode_size[mode]
def lost_point1(modules):
modules_count, lost_point, darkCount = len(modules), 0, 0
for row in range(modules_count):
for col in range(modules_count):
sameCount, dark = 0, modules[row][col]
for r in range(-1, 2):
if row + r < 0 or modules_count <= row + r: continue
for c in range(-1, 2):
if col + c < 0 or modules_count <= col + c: continue
if r == 0 and c == 0: continue
if dark == modules[row + r][col + c]: sameCount += 1
if sameCount > 5: lost_point += (3 + sameCount - 5)
for row in range(modules_count - 1):
for col in range(modules_count - 1):
count = 0
if modules[row][col]: count += 1
if modules[row + 1][col]: count += 1
if modules[row][col + 1]: count += 1
if modules[row + 1][col + 1]: count += 1
if count == 0 or count == 4: lost_point += 3
for row in range(modules_count):
for col in range(modules_count - 6):
if (modules[row][col]
and not modules[row][col + 1]
and modules[row][col + 2]
and modules[row][col + 3]
and modules[row][col + 4]
and not modules[row][col + 5]
and modules[row][col + 6]):
lost_point += 40
for col in range(modules_count):
for row in range(modules_count - 6):
if (modules[row][col]
and not modules[row + 1][col]
and modules[row + 2][col]
and modules[row + 3][col]
and modules[row + 4][col]
and not modules[row + 5][col]
and modules[row + 6][col]):
lost_point += 40
for col in range(modules_count):
for row in range(modules_count):
if modules[row][col]: darkCount += 1
return lost_point + abs(100 * darkCount / modules_count / modules_count - 50) * 2
class QRData:
def __init__(self, data, mode=None):
if data.isdigit(): auto_mode = MODE_NUMBER
elif re.match('^[%s]*$' % re.escape(ALPHA_NUM), data): auto_mode = MODE_ALPHA_NUM
else: auto_mode = MODE_8BIT_BYTE
self.mode, self.data = auto_mode if mode is None else mode, data
def __len__(self):
return len(self.data)
def write(self, buffer):
if self.mode == MODE_NUMBER:
for i in range(0, len(self.data), 3):
chars = self.data[i:i + 3]
bit_length = NUMBER_LENGTH[len(chars)]
buffer.put(int(chars), bit_length)
elif self.mode == MODE_ALPHA_NUM:
for i in range(0, len(self.data), 2):
chars = self.data[i:i + 2]
if len(chars) > 1: buffer.put(ALPHA_NUM.find(chars[0]) * 45 + ALPHA_NUM.find(chars[1]), 11)
else: buffer.put(ALPHA_NUM.find(chars), 6)
else:
for c in self.data:
buffer.put(ord(c), 8)
class BitBuffer:
def __init__(self): self.buffer, self.length = [], 0
def get(self, index): return ((self.buffer[int(index/8)] >> (7 - index % 8)) & 1) == 1
def put(self, num, length):
for i in range(length): self.put_bit(((num >> (length - i - 1)) & 1) == 1)
def __len__(self): return self.length
def put_bit(self, bit):
buf_index = self.length // 8
if len(self.buffer) <= buf_index: self.buffer.append(0)
if bit: self.buffer[buf_index] |= (0x80 >> (self.length % 8))
self.length += 1
def create_bytes(buffer, rs_b):
offset, maxDcCount, maxEcCount, totalCodeCount, dcdata, ecdata = 0, 0, 0, 0, [0] * len(rs_b), [0] * len(rs_b)
for r in range(len(rs_b)):
dcCount = rs_b[r].data_count
ecCount = rs_b[r].total_count - dcCount
maxDcCount, maxEcCount, dcdata[r] = max(maxDcCount, dcCount), max(maxEcCount, ecCount), [0] * dcCount
for i in range(len(dcdata[r])): dcdata[r][i] = 0xff & buffer.buffer[i + offset]
offset += dcCount
rsPoly = Poly([1], 0)
for i in range(ecCount): rsPoly = rsPoly * Poly([1, TEXP[i]], 0)
modPoly = Poly(dcdata[r], len(rsPoly) - 1) % rsPoly
ecdata[r] = [0] * (len(rsPoly) - 1)
for i in range(len(ecdata[r])):
modIndex = i + len(modPoly) - len(ecdata[r])
ecdata[r][i] = modPoly[modIndex] if (modIndex >= 0) else 0
for b in rs_b: totalCodeCount += b.total_count
data, index = [None] * totalCodeCount, 0
for i in range(maxDcCount):
for r in range(len(rs_b)):
if i < len(dcdata[r]):
data[index] = dcdata[r][i]
index += 1
for i in range(maxEcCount):
for r in range(len(rs_b)):
if i < len(ecdata[r]):
data[index] = ecdata[r][i]
index += 1
return data
class DataOverflowError(Exception):
pass
def create_data(version, error_correction, data_list):
rs_b, buffer, total_data_count, PAD0, PAD1 = rs_blocks(version, error_correction), BitBuffer(), 0, 0xEC, 0x11
for data in data_list:
buffer.put(data.mode, 4)
buffer.put(len(data), length_in_bits(data.mode, version))
data.write(buffer)
for block in rs_b: total_data_count += block.data_count
if len(buffer) > total_data_count * 8: raise DataOverflowError()
if len(buffer) + 4 <= total_data_count * 8: buffer.put(0, 4)
while len(buffer) % 8: buffer.put_bit(False)
while True:
if len(buffer) >= total_data_count * 8: break
buffer.put(PAD0, 8)
if len(buffer) >= total_data_count * 8: break
buffer.put(PAD1, 8)
return create_bytes(buffer, rs_b)
class Poly:
def __init__(self, num, shift):
offset = 0
while offset < len(num) and num[offset] == 0: offset += 1
self.num = [0] * (len(num) - offset + shift)
for i in range(len(num) - offset): self.num[i] = num[i + offset]
def __getitem__(self, index):
return self.num[index]
def __len__(self): return len(self.num)
def __mul__(self, e):
num = [0] * (len(self) + len(e) - 1)
for i in range(len(self)):
for j in range(len(e)): num[i + j] ^= TEXP[(TLOG[self[i]] + TLOG[e[j]]) % 255]
return Poly(num, 0)
def __mod__(self, e):
if len(self) - len(e) < 0: return self
ratio, num = TLOG[self[0]] - TLOG[e[0]], [0] * len(self)
for i in range(len(self)): num[i] = self[i]
for i in range(len(e)): num[i] ^= TEXP[(TLOG[e[i]] + ratio) % 255]
return Poly(num, 0) % e
class RSBlock:
def __init__(self, total_c, data_c):
self.total_count, self.data_count = total_c, data_c
def rs_blocks(version, error_correction):
offset = RS_BLOCK_OFFSET[error_correction]
rs_b, blocks = RS_BLOCK_TABLE[(version - 1) * 4 + offset], []
for i in range(0, len(rs_b), 3):
count, total_count, data_count = rs_b[i:i + 3]
for j in range(count): blocks.append(RSBlock(total_count, data_count))
return blocks
class QRCode:
def __init__(self, data='hello', error_co=0):
self.version, self.error_co, size, self.m, self.m_count, self.data_cache, self.data_list = None, int(error_co), 1, None, 0, None, []
self.data_list.append(QRData(data))
while True:
try: self.data_cache = create_data(size, self.error_co, self.data_list)
except: size += 1
else:
self.version = size
break
self.makeImpl(False, self.best_mask_pattern())
def makeImpl(self, test, mask_pattern):
self.m_count = self.version * 4 + 17
self.m = [None] * self.m_count
for row in range(self.m_count):
self.m[row] = [None] * self.m_count
for col in range(self.m_count): self.m[row][col] = None
self.setup_position_probe_pattern(0, 0)
self.setup_position_probe_pattern(self.m_count - 7, 0)
self.setup_position_probe_pattern(0, self.m_count - 7)
self.setup_position_adjust_pattern()
self.setup_timing_pattern()
self.setup_type_info(test, mask_pattern)
if self.version >= 7: self.setup_type_number(test)
if self.data_cache is None: self.data_cache = create_data(self.version, self.error_co, self.data_list)
self.map_data(self.data_cache, mask_pattern)
def setup_position_probe_pattern(self, row, col):
for r in range(-1, 8):
if row + r <= -1 or self.m_count <= row + r: continue
for c in range(-1, 8):
if col + c <= -1 or self.m_count <= col + c: continue
self.m[row + r][col + c] = True if (0 <= r and r <= 6 and (c == 0 or c == 6) or (0 <= c and c <= 6 and (r == 0 or r == 6)) or (2 <= r and r <= 4 and 2 <= c and c <= 4)) else False
def best_mask_pattern(self):
min_lost_point, pattern = 0, 0
for i in range(8):
self.makeImpl(True, i)
lost_point = lost_point1(self.m)
if i == 0 or min_lost_point > lost_point: min_lost_point, pattern = lost_point, i
return pattern
def svg(self, ox=0, oy=0, d=2, txt=''):
o, mc = '<svg %s width="%s" height="%s">\n' % (_SVGNS, ox+d*self.m_count, oy+d*self.m_count), self.m_count
for r in range(mc):
k = 0
for c in range(mc):
if self.m[r][c]: k += 1
elif k > 0:
o += '<rect x="%d" y="%d" width="%d" height="%d"/>\n' % (ox+(c-k)*d, oy+r*d, k*d, d)
k = 0
if k > 0: o += '<rect x="%d" y="%d" width="%d" height="%d"/>\n' % (ox+(mc-k)*d, oy+r*d, k*d, d)
if txt:
o += '<text x="%s" y="%s" style="font-size:32;fill:gray" >%s</text>\n' % (ox, oy + 40 + d*mc, txt)
return o + '</svg>\n'
def pdf(self, ox=0, oy=0, d=10, size=False, color = b'0 0 0'):
o, mc = color + b' rg ', self.m_count
if size:
o += bytes('q .9 .9 .9 rg BT 1 0 0 1 2 2 Tm /F1 3 Tf (%d) Tj ET Q ' % self.m_count, 'ascii')
for r in range(mc):
k = 0
for c in range(mc):
if self.m[r][c]: k += 1
elif k > 0:
o += bytes('%d %d %d %d re ' % (ox+(c-k)*d, oy-r*d, k*d, d), 'ascii')
k = 0
if k > 0: o += bytes('%d %d %d %d re ' % (ox+(mc-k)*d, oy-r*d, k*d, d), 'ascii')
return o + b'f '
def setup_timing_pattern(self):
for r in range(8, self.m_count - 8):
if self.m[r][6] != None: continue
self.m[r][6] = (r % 2 == 0)
for c in range(8, self.m_count - 8):
if self.m[6][c] != None: continue
self.m[6][c] = (c % 2 == 0)
def setup_position_adjust_pattern(self):
pos = pattern_position(self.version)
for i in range(len(pos)):
for j in range(len(pos)):
row, col = pos[i], pos[j]
if self.m[row][col] != None: continue
for r in range(-2, 3):
for c in range(-2, 3):
self.m[row + r][col + c] = True if (r == -2 or r == 2 or c == -2 or c == 2 or (r == 0 and c == 0)) else False
def setup_type_number(self, test):
bits = BCH_type_number(self.version)
for i in range(18): self.m[i // 3][i % 3 + self.m_count - 8 - 3] = (not test and ((bits >> i) & 1) == 1)
for i in range(18): self.m[i % 3 + self.m_count - 8 - 3][i // 3] = (not test and ((bits >> i) & 1) == 1)
def setup_type_info(self, test, mask_pattern):
bits = BCH_type_info((self.error_co << 3) | mask_pattern)
for i in range(15):
mod = (not test and ((bits >> i) & 1) == 1)
if i < 6: self.m[i][8] = mod
elif i < 8: self.m[i + 1][8] = mod
else: self.m[self.m_count - 15 + i][8] = mod
for i in range(15):
mod = (not test and ((bits >> i) & 1) == 1)
if i < 8: self.m[8][self.m_count - i - 1] = mod
elif i < 9: self.m[8][15 - i - 1 + 1] = mod
else: self.m[8][15 - i - 1] = mod
self.m[self.m_count - 8][8] = (not test)
def map_data(self, data, mask_pattern):
inc, row, bitIndex, byteIndex, mask_func1 = -1, self.m_count - 1, 7, 0, mask_func(mask_pattern)
for col in range(self.m_count - 1, 0, -2):
if col == 6: col -= 1
while True:
for c in range(2):
if self.m[row][col - c] == None:
dark = False
if byteIndex < len(data): dark = (((data[byteIndex] >> bitIndex) & 1) == 1)
if mask_func1(row, col - c): dark = not dark
self.m[row][col - c] = dark
bitIndex -= 1
if bitIndex == -1:
byteIndex += 1
bitIndex = 7
row += inc
if row < 0 or self.m_count <= row:
row -= inc
inc = -inc
break
##### API #####
def send(host='localhost', data=''):
"_"
co, serv = http.client.HTTPConnection(host), '/'
co.request('POST', serv, urllib.parse.quote(data))
return co.getresponse().read().decode('utf8')
def send_get(host='localhost', data=''):
"_"
co = http.client.HTTPConnection(host)
co.request('GET', '/' + urllib.parse.quote(data))
return co.getresponse().read()
def add_local_id():
"_"
pp1, pp2, cm, k, db, ok, notdone = '', '', b'', ecdsa(), dbm.open('keys', 'c'), True, True
while pp1 != pp2 or len(pp1) < 4: pp1, pp2 = getpass.getpass('Select a passphrase? '), getpass.getpass('The passphrase again? ')
if os.path.isfile('found_id.txt'):
for l in open('found_id.txt').readlines():
cm, tab = b64tob(bytes(l[:12], 'ascii')), l[16:].split('/')
if cm not in db:
print ('Static Id: %s' % l[:12])
if input('Select this id or another ? [y/n]: ') == 'y':
#pp = getpass.getpass('Select root passphrase? ')
#intpriv = int(AES().decrypt(b64tob(bytes(tab[2].strip(), 'ascii')), hashlib.sha256(pp.encode('utf8')).digest()))
intpriv = b64toi(bytes(tab[2].strip(), 'ascii'))
#intpriv = int(tab[2].strip())
prv = AES().encrypt('%s' % intpriv, hashlib.sha256(pp1.encode('utf8')).digest())
ptx, pty = b64toi(bytes(tab[0].strip(), 'ascii')), b64toi(bytes(tab[1].strip(), 'ascii'))
#ptx, pty = int(tab[0].strip()), int(tab[1].strip())
db[cm] = i2b(ptx, 66) + i2b(pty, 66) + prv
#db[cm] = b''.join([b64tob(bytes(x, 'ascii')) for x in tab[:2]]) + prv
notdone = False
print ('%s added' % l[:12])
break
if notdone:
while (cm in db) or ok:
k.generate()
cm = i2b(k.pt.y)[-9:]
print ('Id: %s' % btob64(cm))
ok = not (input('Select this id or another ? [y/n]: ') == 'y')
db[cm] = i2b(k.pt.x, 66) + i2b(k.pt.y, 66) + AES().encrypt('%s' % k.privkey, hashlib.sha256(pp1.encode('utf8')).digest())
print ('%s added' % btob64(cm))
if b'user' not in db: db[b'user'] = cm
if b'host' not in db: db[b'host'] = b'cup'
db.close()
def add_local_id2(pp):
"for gui"
cm, k, db = b'', ecdsa(), dbm.open('cles', 'c')
k.generate()
cm = i2b(k.pt.y)[-9:]
if cm not in db:
db[cm] = i2b(k.pt.x, 66) + i2b(k.pt.y, 66) + AES().encrypt('%s' % k.privkey, hashlib.sha256(pp.encode('utf8')).digest())
print ('%s added' % btob64(cm))
db.close()
def list_local_ids(node):
"_"
if os.path.isfile('keys'):
db = dbm.open('keys', 'c')
print ('Autority: %s, Currency: %s, Host: \'%s\', You have %d user IDs:' % (_root_id, db['cry'].decode('utf8'), db['host'].decode('ascii'), len(db.keys())-3))
for x in filter(lambda i:len(i)==9, db.keys()):
bl = send(node, '=' + btob64(x))
print (btob64(x), '%20s' % bl if db['user'] != x else '%20s\t*' % bl )
db.close()
else:
print ('Use \'./%s add\' for adding an ID' % __app__)
def get_unique(dk, rid):
"_"
out = [u for u in filter(lambda i:re.match(rid, btob64(i)), dk.keys())]
return out[0] if len(out) == 1 else None
def set(k, h):
"_"
d = dbm.open('keys', 'c')
if k == 'user':
src = get_unique(d, h)
if src:
d[k] = src
print ('%s->%s' % (k, btob64(src)))
elif k == 'host':
d[k] = h
print ('%s->%s' % (k, h))
elif k == 'cry':
d[k] = h
print ('%s->%s' % (k, h))
d.close()
def readdb(arg, ascii=False):
"_"
d = dbm.open(arg)
if ascii:
#for x in d.keys(): print (x.decode('ascii'), '->', d[x].decode('ascii'))
for x in d.keys(): print (x.decode('utf8'), '->', d[x].decode('utf8'))
#for x in d.keys(): print (x, '->', d[x])
else:
for x in d.keys(): print ('%02d:%03d' % (len(x), len(d[x])), btob64(x), '->', btob64(d[x]))
def get_host():
"_"
db = dbm.open('keys')
host = db['host']
db.close()
return host.decode('utf8')
def certif(node, rid):
"_"
db, k, dat = dbm.open('keys'), ecdsa(), datencode()
src, res = db['user'], send(node, '?' + rid)
if res:
prv, pub, dst, pp = db[src][132:], db[src][:132], b64tob(bytes(res, 'ascii')), getpass.getpass('Passphrase for \'%s\'? ' % btob64(src))
print ('...please wait')
age = 1967
k.privkey, msg = int(AES().decrypt(prv, hashlib.sha256(pp.encode('utf8')).digest())), dst + datencode(365) + src + i2b(age, 2) + hcode('%s 167071927202809' % age)
print (send(node, '.' + btob64(msg + k.sign(msg))))
else:
print('unknown recipient')
db.close()
def buy(node, rid, prc):
"_"
db, k, dat = dbm.open('keys'), ecdsa(), datencode()
cry = _cur[db['cry'].decode('utf8')] if db['cry'].decode('utf8') in _cur else db['cry']
src, pri, res = db['user'], int(prc*100) if cry == b'A' else int(prc), send(node, '?' + rid)
if res:
prv, pub, dst, pp = db[src][132:], db[src][:132], b64tob(bytes(res, 'ascii')), getpass.getpass('Passphrase for \'%s\'? ' % btob64(src))
m = input('Message (140 chars maxi)? ')
print ('...please wait')