salut tout le monde,
s'il vous plaît j'ai besoin d'une correction de mon programme python qui implemente la fonction de hachage sha-3
aprés le test du programme j'obtiens un hash different de celui calculé dans les sites internet.
vous trouverez ci-joint un document pdf contenant un algorithme detaillé de sha-3:
NIST.FIPS.202.pdf
merci d'avanceCode:import numpy as np from math import log2 int1=np.vectorize(int) def xor(a,b): return int(((not a) and b) or ((not b) and a)) def xors(s,t): m=max(len(s),len(t)) s='0'*(m-len(s))+s t='0'*(m-len(t))+t X='' for i in range(m): X=X+str(xor(int(s[i]),int(t[i]))) return X def converts_a(s): w=len(s)//25 A=np.zeros((5,5,w)) for x in range(5): for y in range(5): for z in range(w): A[x][y][z]=s[w*(5*y+x)+z] return int1(A) def converta_s(a): w=len(a[0][0]) s='' for y in range(5): p='' for x in range(5): l='' for z in range(w): l=l+str(a[x][y][z]) p=p+l s=s+p return s def theta(a): w=len(a[0][0]) c=np.zeros((5,w)) d=np.zeros((5,w)) A=np.zeros((5,5,w)) for x in range(5): for z in range(w): c[x][z]=xor(xor(xor(a[x][0][z],a[x][1][z]),xor(a[x][2][z],a[x][3][z])),a[x][4][z]) for x in range(5): for z in range(w): d[x][z]=xor(c[(x-1)%5][z],c[(x+1)%5][(z-1)%w]) for x in range(5): for y in range(5): for z in range(w): A[x][y][z]=xor(a[x][y][z],d[x][z]) return int1(A) def rho(a): w=len(a[0][0]) A=np.zeros((5,5,w)) for z in range(w): A[0][0][z]=a[0][0][z] (x,y)=(1,0) for t in range(24): for z in range(w): A[x][y][z]=a[x][y][(z-((t+1)*(t+2))//2)%w] (x,y)=(y,(2*x+3*y)%5) return int1(A) def pi(a): w=len(a[0][0]) A=np.zeros((5,5,w)) for x in range(5): for y in range(5): for z in range(w): A[x][y][z]=a[(x+3*y)%5][x][z] return int1(A) def chsy(a): w=len(a[0][0]) A=np.zeros((5,5,w)) for x in range(5): for y in range(5): for z in range(w): A[x][y][z]=xor(a[x][y][z],xor(a[(x+1)%5][y][z],1)*a[(x+2)%5][y][z]) return int1(A) def rc(t): n=t%255 if n==0: return 1 R=[1,0,0,0,0,0,0,0] for i in range(1,n+1): R=[0]+R R[0]=xor(R[0],R[8]) R[4]=xor(R[4],R[8]) R[5]=xor(R[5],R[8]) R[6]=xor(R[6],R[8]) R=R[:8] return int(R[0]) def iota(a,i): w=len(a[0][0]) l=int(log2(w)) A=np.zeros((5,5,w)) for x in range(5): for y in range(5): for z in range(w): A[x][y][z]=a[x][y][z] RC=[0]*w for j in range(l+1): RC[(2**j)-1]=rc(j+7*i) for z in range(w): A[0][0][z]=xor(A[0][0][z],RC[z]) return A def rnd(a,i): return iota(chsy(pi(rho(theta(a)))),i) def keccakp(b,n,s): w=b//25 l=int(log2(w)) a=converts_a(s) for i in range(12+2*l-n,12+2*l): a=int1(rnd(a,i)) a=int1(a) S=converta_s(a) return S def sponge(b,f,pad,r,s,d): P=s+pad(r,len(s)) n=len(P)//r c=b-r S='0'*b for i in range(n): S=f(xors(S,P[i*r:i*(r+1)]+'0'*c)) Z='' while len(Z)<d: Z=Z+S[:r] S=f(S) return Z[:d] def pad101(x,m): j=(-m-2)%x return '1'+j*'0'+'1' g=lambda x:keccakp(1600,24,x) def keccak(c,s,d): b=1600 f=g pad=pad101 r=1600-c S=sponge(b,f,pad,r,s,d) return S def binaire(m): l=list(m.encode()) txt='' for e in l: s=len(bin(e))-2 txt=txt+(8-s)*'0'+bin(e)[2:] return txt def sha3_224(M): S=binaire(M)+'01' return keccak(448,S,224) def sha3_256(M): S=binaire(M)+'01' return keccak(512,S,256) def sha3_384(M): S=binaire(M)+'01' return keccak(768,S,384) def sha3_512(M): S=binaire(M)+'01' return keccak(1024,S,512)
-----