Created
September 26, 2022 22:49
-
-
Save ihanson/3ef652b55b7c258d7df125fc419e63f3 to your computer and use it in GitHub Desktop.
SHA256 implemented in an Excel formula
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=LAMBDA(str,LET( | |
RightShift,LAMBDA(num,bits,QUOTIENT(num,2^bits)), | |
RightRotate,LAMBDA(word,bits,LET(b,MOD(bits,32),MOD(word,2^b)*2^(32-b)+RightShift(word,b))), | |
BitwiseOp,LAMBDA(op,LAMBDA(f,LAMBDA(lw,rw,f(f,op,lw,rw,0,0)))( | |
LAMBDA(f,op,lw,rw,r,i,IF( | |
i>31,r, | |
f(f,op,RightShift(lw,1),RightShift(rw,1),r+op(MOD(lw,2),MOD(rw,2))*(2^i),i+1) | |
)) | |
)), | |
BXOR,BitwiseOp(LAMBDA(l,r,IF(l=r,0,1))), | |
BAND,BitwiseOp(LAMBDA(l,r,IF(AND(l=1,r=1),1,0))), | |
BNOT,LAMBDA(f,LAMBDA(w,f(w,0)))(BitwiseOp(LAMBDA(l,r,IF(l=1,0,1)))), | |
Plus,LAMBDA(l,r,MOD(l+r,2^32)), | |
EncodeInt,LAMBDA(f,LAMBDA(n,bytes,LOWER(f(f,n,bytes))))(LAMBDA(f,n,bytes,IF(bytes=0,"",f(f,RightShift(n,8),bytes-1)&DEC2HEX(MOD(n,256),2)))), | |
DecodeInt,LAMBDA(f,LAMBDA(b,f(f,b)))(LAMBDA(f,b,IF(LEN(b)=0,0,f(f,LEFT(b,LEN(b)-2))*256+HEX2DEC(RIGHT(b,2))))), | |
EncodeString,LET( | |
UTF8PadByte,LAMBDA(n,b,EncodeInt((2^(8-b-1)-1)*2^(b+1)+n,1)), | |
EncodeChar,LAMBDA(f,LAMBDA(c,IF(c<2^7,EncodeInt(c,1),f(f,c,6))))(LAMBDA(f,c,b,IF(c<2^b,UTF8PadByte(c,b),f(f,RightShift(c,6),b-1)&UTF8PadByte(MOD(c,2^6),6)))), | |
LAMBDA(f,LAMBDA(s,f(f,s)))(LAMBDA(f,s,IF(LEN(s)=0,"",LET(c,LEFT(s,1),EncodeChar(UNICODE(c))&f(f,MID(s,LEN(c)+1,LEN(s))))))) | |
), | |
Slice,LAMBDA(bytes,index,len,MID(bytes,index*8+1,len*8)), | |
Word,LAMBDA(bytes,i,DecodeInt(Slice(bytes,i,1))), | |
SetWord,LAMBDA(bytes,i,word,Slice(bytes,0,i)&EncodeInt(word,4)&Slice(bytes,i+1,LEN(bytes))), | |
Consts,LET( | |
IsPrime,LAMBDA(f,LAMBDA(n,f(f,n,2)))(LAMBDA(f,n,d,IF(d*d>n,TRUE,IF(MOD(n,d)=0,FALSE,f(f,n,d+1))))), | |
NextPrime,LAMBDA(f,LAMBDA(n,f(f,n)))(LAMBDA(f,n,IF(IsPrime(n),n,f(f,n+1)))), | |
FracToHex,LAMBDA(n,bytes,EncodeInt(FLOOR.MATH((n-FLOOR.MATH(n))*2^(bytes*8)),bytes)), | |
LAMBDA(f,LAMBDA(g,n,f(f,g,1,n)))(LAMBDA(f,g,p,n,IF(n=0,"",LET(np,NextPrime(p+1),FracToHex(g(np),4)&f(f,g,np,n-1))))) | |
), | |
H,Consts(LAMBDA(n,n^(1/2)),8), | |
K,Consts(LAMBDA(n,n^(1/3)),64), | |
PreProcess,LAMBDA(str,LET(e,EncodeString(str),e&"80"&EncodeInt(LEN(e)*4,64-MOD(LEN(e)/2+1,64)))), | |
BuildSched,LAMBDA(f,LAMBDA(chunk,f(f,chunk,16)))(LAMBDA(f,w,i,IF(i>63,w,LET( | |
s_0,BXOR(BXOR(RightRotate(Word(w,i-15),7),RightRotate(Word(w,i-15),18)),RightShift(Word(w,i-15),3)), | |
s_1,BXOR(BXOR(RightRotate(Word(w,i-2),17),RightRotate(Word(w,i-2),19)),RightShift(Word(w,i-2),10)), | |
f(f,w&EncodeInt(Plus(Plus(Plus(Word(w,i-16),s_0),Word(w,i-7)),s_1),4),i+1) | |
)))), | |
CompressionLoop,LAMBDA(f,LAMBDA(w,h,f(f,0,w,h)))( | |
LAMBDA(f,i,w,vars,LET( | |
va,Word(vars,0),vb,Word(vars,1), | |
vc,Word(vars,2),vd,Word(vars,3), | |
ve,Word(vars,4),vf,Word(vars,5), | |
vg,Word(vars,6),vh,Word(vars,7), | |
s_1,BXOR(BXOR(RightRotate(ve,6),RightRotate(ve,11)),RightRotate(ve,25)), | |
ch,BXOR(BAND(ve,vf),BAND(BNOT(ve),vg)), | |
temp_1,Plus(Plus(Plus(Plus(vh,s_1),ch),Word(K,i)),Word(w,i)), | |
s_0,BXOR(BXOR(RightRotate(va,2),RightRotate(va,13)),RightRotate(va,22)), | |
maj,BXOR(BXOR(BAND(va,vb),BAND(va,vc)),BAND(vb,vc)), | |
temp_2,Plus(s_0,maj), | |
newVars,CONCAT( | |
EncodeInt(Plus(temp_1,temp_2),4), | |
EncodeInt(va,4), | |
EncodeInt(vb,4), | |
EncodeInt(vc,4), | |
EncodeInt(Plus(vd,temp_1),4), | |
EncodeInt(ve,4), | |
EncodeInt(vf,4), | |
EncodeInt(vg,4) | |
), | |
IF(i=63,newVars,f(f,i+1,w,newVars)) | |
)) | |
), | |
ChunkLoop,LAMBDA(f,LAMBDA(message,f(f,message,H,0)))( | |
LAMBDA(f,message,hash,i,LET( | |
chunk,Slice(message,i*16,16), | |
IF(LEN(chunk)=0,hash,LET( | |
w,BuildSched(chunk), | |
vars,CompressionLoop(w,hash), | |
newHash,CONCAT( | |
EncodeInt(Plus(Word(hash,0),Word(vars,0)),4), | |
EncodeInt(Plus(Word(hash,1),Word(vars,1)),4), | |
EncodeInt(Plus(Word(hash,2),Word(vars,2)),4), | |
EncodeInt(Plus(Word(hash,3),Word(vars,3)),4), | |
EncodeInt(Plus(Word(hash,4),Word(vars,4)),4), | |
EncodeInt(Plus(Word(hash,5),Word(vars,5)),4), | |
EncodeInt(Plus(Word(hash,6),Word(vars,6)),4), | |
EncodeInt(Plus(Word(hash,7),Word(vars,7)),4), | |
), | |
f(f,message,newHash,i+1) | |
)) | |
)) | |
), | |
ChunkLoop(PreProcess(str)) | |
))("string to hash") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment