python-imprastorage/impra/crypt.py

320 lines
28 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# software : ImpraStorage <http://imprastorage.sourceforge.net/> #
# version : 0.4 #
# date : 2012 #
# licence : GPLv3.0 <http://www.gnu.org/licenses/> #
# author : a-Sansara <http://www.a-sansara.net/> #
# copyright : pluie.org <http://www.pluie.org/> #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# This file is part of ImpraStorage.
#
# ImpraStorage is free software (free as in speech) : 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.
#
# ImpraStorage 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 ImpraStorage. If not, see <http://www.gnu.org/licenses/>.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ package crypt ~~
from base64 import urlsafe_b64encode, b64decode
from binascii import b2a_base64, a2b_base64
2012-09-17 17:13:10 +00:00
from hashlib import sha256, md5
from math import log, floor, ceil
from random import choice
from os import urandom
from time import sleep
from impra.util import RuTime, __CALLER__, stack, DEBUG
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ methods ~~
def hash_sha256(data):
"""Get a sha256 hash of str `data`
:Returns: `str`
"""
return str(sha256(bytes(data,'utf-8')).hexdigest())
2012-09-17 17:13:10 +00:00
def hash_md5_file(path):
"""Get a md5 hash of file from path
:Returns: `str`
"""
return md5(open(path, mode='rb').read()).hexdigest()
def randomFrom(val, sval=0):
"""Get a random number from range `sval=0` to `val`
:Returns: `int`
"""
lst = list(range(sval,val))
return choice(lst)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ class KeyGen ~~
class KeyGen :
""""""
CHSET = [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, 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, 880, 881, 882, 883, 884, 885, 886, 887, 891, 892, 893, 894, 901, 902, 903, 904, 905, 906, 911, 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 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, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023, 1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031, 1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039, 1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047, 1048, 1049, 1050, 1051, 1052, 1053, 1054, 1055, 1056, 1057, 1058, 1059, 1060, 1061, 1062, 1063, 1
""""""
LEN_FOOTPRINT = 22
""""""
SALT = '-¤-ImpraStorage-¤-'
""""""
def __init__(self, length, salt=None):
""""""
self.new(length, salt)
def _build(self,l):
""""""
r = Randomiz(len(self.CHSET),self.CHSET)
self.key = ksin = kfoo = ''
dic = {}
for i in range(l):
self.key += chr(r.get(False))
if not self.key[i] in dic: dic[self.key[i]] = 1
for c in dic: ksin += c
for c in ksin[::-5]:
if len(kfoo)>=self.LEN_FOOTPRINT: break
kfoo += c
self.mark = hash_sha256(self.salt+kfoo)
def getMark(self,key):
""""""
dic = {}
ksin = kfoo = ''
for i in range(len(key)):
if not self.key[i] in dic: dic[self.key[i]] = 1
for c in dic: ksin += c
for c in ksin[::-5]:
if len(kfoo)>=self.LEN_FOOTPRINT: break
kfoo += c
return hash_sha256(self.salt+kfoo)
def new(self, length, salt=None):
""""""
if salt == None : self.salt = self.SALT
else : self.salt = salt
self._build(length)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ class ConfigKey ~~
class ConfigKey:
""""""
def __init__(self, key=None, salt=None, psize=19710000):
""""""
if key : self.key = bytes(key,'utf-8')
else : self.key = self._build()
if salt ==None : self.salt = str(self.key[::-10])
else : self.salt = salt
self.psize = psize
self.noiser = Noiser(self.key)
self.rdmz = Randomiz(1)
def getHashList(self,name,count,noSorted=False):
""""""
rt = RuTime(eval(__CALLER__('"%s",%s,%i' % (name,count,noSorted))))
self.rdmz.new(count)
dic, lst, hroot = {}, [], hash_sha256(self.salt+name)
for i in range(count) :
self.noiser.build(i)
d = str(i).rjust(2,'0')
# part n°, hash, lns, lne, pos
hpart = hash_sha256(self.salt+name+'.part'+d)[:-3]+str(ord(hroot[i])).rjust(3,'0')
lst.append((d, hpart, self.noiser.lns, self.noiser.lne, self.rdmz.get()))
dic['head'] = [name,count,hroot,self.getKey()]
if not noSorted :
lst = sorted(lst, key=lambda lst: lst[4])
dic['data'] = lst
rt.stop()
return dic
def _build(self,l=48):
""""""
kg = KeyGen(l)
return urlsafe_b64encode(bytes(kg.key,'utf-8'))
def getKey(self):
""""""
return str(self.key,'utf-8')
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ class Kirmah ~~
class Kirmah:
""""""
def __init__(self, key, mark):
""""""
self.key = bytes(key,'utf-8')
self.mark = mark
self.ck = ConfigKey(mark)
def enchr(self,o,cs,ce):
""""""
if not self.key[cs] > self.key[ce] or not o - 2*self.key[cs] - self.key[ce] > 0:
o += self.key[ce]
else:
o -= self.key[cs]
if o == 10: o+=30
return o
def dechr(self,o,cs,ce):
""""""
if not self.key[cs] > self.key[ce] or self.enchr(o - self.key[ce],cs,ce)==o :
o -= self.key[ce]
else :
o += self.key[cs]
return o
def subenc(self,data):
""""""
rt = RuTime(eval(__CALLER__()))
s, i, lkey, ld = '', 0, len(self.key), len(data)
for c in data:
if i >= lkey: i = 0
s += chr(self.enchr(ord(c),i,lkey-1-i))
i += 1
for c in self.key[::-12]:
if ld-c < 0: continue
s = s[ld-c:ld][::-1]+s[0:ld-c]
rt.stop()
return s
def subdec(self,data):
""""""
rt = RuTime(eval(__CALLER__()))
s, i, lkey, ld = '', 0, len(self.key), len(data)
for c in self.key[::-12][::-1]:
if ld-c < 0: continue
data = data[c:ld]+data[0:c][::-1]
for c in data:
if i >= lkey: i = 0
try:
s += chr(self.dechr(ord(c),i,lkey-1-i))
except ValueError as e:
pass
i += 1
rt.stop()
return s
def encrypt(self, odata, label, cpart):
""""""
rt = RuTime(eval(__CALLER__()))
data = self.subenc(odata)
hlst = self.ck.getHashList(label,cpart,True)
dataEnc = ''
psize = ceil(len(data)/cpart)
cp = 0
for row in hlst['data']:
dataEnc += self.ck.noiser.getNoise(row[2],True)+data[cp*psize:cp*psize+psize]+self.ck.noiser.getNoise(row[3],True)
cp += 1
dataEnc = str(b2a_base64(bytes(dataEnc,'utf-8')),'utf-8')
2012-09-17 15:34:08 +00:00
rt.stop()
return dataEnc
2012-09-17 15:34:08 +00:00
def decrypt(self, data, label, cpart):
""""""
rt = RuTime(eval(__CALLER__()))
data = str(a2b_base64(bytes(data,'utf-8')),'utf-8')
dataDec = ''
hlst = self.ck.getHashList(label,cpart,True)
cp = ni = si = ei = rsz = 0
for row in hlst['data']:
rsz += row[2]+row[3]
psize = ceil((len(data)-rsz)/cpart)
for row in hlst['data']:
si = ni + row[2]
ei = si + psize
if cp == cpart-1 :
2012-09-17 15:34:08 +00:00
ei = -row[3]
if not si > len(data)+ei : pass
else :
dataDec=dataDec[:len(data)+ei-si]
break
dataDec += data[si:ei]
ni = ei + row[3]
cp += 1
dataDec = self.subdec(dataDec)
rt.stop()
return dataDec
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ class Randomiz ~~
class Randomiz:
""""""
def __init__(self,count,chl=None):
""""""
if chl ==None : self.lst = list(range(0,count))
else: self.lst = chl
self.count = len(self.lst)
def new(self,count=None, chl=None):
""""""
if count : self.count = count
self.__init__(self.count,chl)
def get(self,single=True):
""""""
pos = choice(self.lst)
if single: del self.lst[self.lst.index(pos)]
return pos
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~ class Noiser ~~
class Noiser:
""""""
def __init__(self, key, part=0):
""""""
self.key = key
self.build(part)
def build(self, part):
""""""
if not part < len(self.key)-1 : raise Exception('part exceed limit')
else :
self.part, v = part, 0
v = int(ceil((self.key[22]+v)/4.20583))
self.lns = abs(int(ceil(v/2))-self.key[self.part]+self.key[7])
self.lne = abs(int(v-self.lns-self.key[self.part+2]-self.key[44]/2.1934))
def getNoise(self, l, noBytes=False):
""""""
n = urandom(l)
if noBytes:
n = str(urlsafe_b64encode(n),'utf-8')[0:l]
return n