def hash_pass(password, salt): if len(password) < 12: loop = 1000 + len(password) else: loop = 1000 + len(password) % 3 salt = salt.encode() for i in range(loop): password = sha3_256(password.encode() + salt).hexdigest() return password
def hash_pass_simple(password): if len(password) < 12: loop = 1000 + len(password) else: loop = 1000 + len(password) % 3 salt = PASS_SALT.encode() for i in range(loop): password = sha3_256(password.encode() + salt).hexdigest() return password
def setstate(self, state): state = deepcopy(state) if state['fast']: fast_state = state['fast_impl'] del state['fast_impl'] c = state['c'] if c == 448: state['fast_impl'] = _sha3.sha3_224() elif c == 512: state['fast_impl'] = _sha3.sha3_256() elif c == 768: state['fast_impl'] = _sha3.sha3_384() elif c == 1024: state['fast_impl'] = _sha3.sha3_512() elif c == 576: state['fast_impl'] = _sha3.sha3_0() else: raise TypeError('Malformed state') state['fast_impl'].state = fast_state for k, v in state.iteritems(): setattr(self, k, v)
def __init__(self, r=1024,c=576,fixed_out=False,duplex=False,verbose=False): """Constructor: r: bitrate (default 1024) c: capacity (default 576) verbose: print the details of computations(default:False) r + c must be 25, 50, 100, 200, 400, 800 or 1600 (recommended 1600) see http://keccak.noekeon.org/NoteOnKeccakParametersAndUsage.pdf """ if fixed_out and duplex: raise ValueError('It is nonsense to try to instantiate a fixed output, duplex Keccak') self.fixed_out = fixed_out self.duplex = duplex self.verbose = verbose if (r<0) or (r%8!=0): raise ValueError('r must be a multiple of 8 in this implementation') self.r = r self.c = c self.b = b = r+c if b not in [25, 50, 100, 200, 400, 800, 1600]: raise ValueError('b value not supported - use 25, 50, 100, 200, 400, 800 or 1600') self.w = b//25 self.l=(self.w-1).bit_length() self.nr=12+2*self.l self.done_absorbing = False if has_fast and not duplex: if fixed_out and b == 1600 and c == 448: self.fast = True self.fast_impl = _sha3.sha3_224() return elif fixed_out and b == 1600 and c == 512: self.fast = True self.fast_impl = _sha3.sha3_256() return elif fixed_out and b == 1600 and c == 768: self.fast = True self.fast_impl = _sha3.sha3_384() return elif fixed_out and b == 1600 and c == 1024: self.fast = True self.fast_impl = _sha3.sha3_512() return elif not fixed_out and b == 1600 and c == 576: self.fast = True self.fast_impl = _sha3.sha3_0() return self.fast = False if verbose: print "Create a Keccak function with (r=%d, c=%d (i.e. w=%d))" % (r,c,(r+c)//25) # Initialisation of state self.S = ((0,0,0,0,0), (0,0,0,0,0), (0,0,0,0,0), (0,0,0,0,0), (0,0,0,0,0)) self.P = '' self.output_cache = ''
#!/usr/bin/python3 from lexer.lex import * import binascii import _sha3 sha3_256 = lambda x: _sha3.sha3_256(x).digest() sha3_count = [0] def sha3(seed): sha3_count[0] += 1 return sha3_256(seed) def die_error(msg): print(msg) exit(1) class Stack: def __init__(self, vars, fname="", parent=None): self.parent = parent self.childs = dict() self.memmap = dict() self.lookup = dict() self.retAddr = 0 self.asm = [] self.memoffset = 1 if parent is not None:
def convert(svg, file, amount=1000, cmd=True): #檢查此檔案是否運算過 s = sha3_256() s.update(encode(svg + str(amount))) hash = s.hexdigest() try: with open('./FourierData/{}_{}'.format(file, hash), 'rb') as f: Print("The file is exists, return the value.", True) out = pickle.load(f) return out + [hash] except FileNotFoundError: pass #開始解析SVG資料 Print("Converting start.", True) global end, prjNum, prjCur end = amount #圓圈數量 prjNum = end + 1 rawdata = svg curve = re.sub(r'\s', '', "".join(rawdata)) cells = re.findall(r'\w[\d\,\-\.]+', curve) for cell in cells: trcdata = [] formatString = re.sub(r'-', ',-', cell) trcdata.append(re.match(r'[A-Za-z]', formatString).group(0)) rawvers = re.sub(r'[A-Za-z]\,?', '', formatString).split(',') rawvers = [float(i) for i in rawvers] vergroup = [] vercurgrp = [] for st in range(len(rawvers)): vercurgrp.append(rawvers[st]) if len(vercurgrp) >= 2: vergroup.append(vercurgrp[0] + vercurgrp[1] * 1j) vercurgrp.clear() if len(vercurgrp) > 0: if re.match(r'v', trcdata[0], re.I): vergroup.append(0 + vercurgrp[0] * 1j) elif re.match(r'h', trcdata[0], re.I): vergroup.append(vercurgrp[0] + 0j) trcdata.append(vergroup) trDatas.append(trcdata) for i in range(1, len(trDatas)): #解析SVG信息 if re.match(r'[a-z]', trDatas[i][0]): for j in range(len(trDatas[i][1])): trDatas[i][1][j] += trDatas[i - 1][1][-1] for i in range(len(trDatas)): #解析SVG信息 flag = trDatas[i][0] if re.match(r'm', flag, re.I): continue trDatas[i][1].insert(0, trDatas[i - 1][1][-1]) if re.match(r's', flag, re.I): trDatas[i][1].insert( 1, 2 * trDatas[i - 1][1][-1] - trDatas[i - 1][1][-2]) if re.match(r'[lvh]', flag, re.I): trDatas[i][1].insert( 1, trDatas[i][1][0] / 3 + trDatas[i][1][-1] * 2 / 3) trDatas[i][1].insert( 1, trDatas[i][1][0] * 2 / 3 + trDatas[i][1][-1] / 3) offset = center[0] + 1j * center[1] points = [i[1] for i in trDatas if not re.match(r'm', i[0], re.I)] #解析SVG信息 points = [[j - offset for j in i] for i in points] #中心歸零 #計算每個採集點的時間權重 wsum = 0 for curve in points: wst = 10 a, b, c, d = curve total = sum([ abs( bezier(linear(i, 0, wst, 0, 1), a, b, c, d) - bezier(linear(i - 1, 0, wst, 0, 1), a, b, c, d)) for i in range(1, wst) ]) curWeight.append(total) wsum += total #確保所有時間權重相加為一 for i in range(len(curWeight)): curWeight[i] /= wsum #累加來讓時間權重變成時間座標 for i in range(1, len(curWeight)): curWeight[i] += curWeight[i - 1] #加入0,1的頭尾 curWeight.insert(0, 0) curWeight[-1] = 1 #開始生成傅立葉係數 prjCur = mp.Value('i', 0) pool = mp.Pool(initializer=initParam, initargs=(points, curWeight, prjCur)) out = pool.map_async(mainCalculation, range(0, end + 1)) #列印進度 length = 50 while True: cent = prjCur.value / prjNum nump = round(cent * length) Print( "[" + "=" * nump + " " * (length - nump) + "]{}%".format(round(cent * 100)), True, '\r' if cmd else '\n') if prjCur.value >= prjNum: if cmd: print() break #關閉多進程處理並獲取答案 pool.close() pool.join() out.wait() out = out.get() #將輸出結果儲存至檔案中(使用pickle直接儲存變數) with open('./FourierData/{}_{}'.format(file, hash), 'wb') as f: pickle.dump([out, wsum], f) return [out, wsum, hash]