homa
Guru
Joined: 05/11/2021 Location: GermanyPosts: 351 |
Posted: 11:43pm 11 Feb 2024 |
Copy link to clipboard |
Print this post |
|
Hello all,
out of interest in hash algorithms, i once implemented the sha-1 in mmbasic as a function. with the restriction that an input string cannot be longer than 183 characters. I hope I haven't made any mistakes. Tests are always welcome. Suggestions for better code as well, I can only learn. For comparison and also to learn, I would be interested in how to create a csub for the sha-1, I haven't dealt with this yet. Who else is interested and wants to help?
Matthias
'dev sha-1 as function by homa 02/2024 'all variables are unsigned 32-bit Option EXPLICIT Print sha1("mmbasic_sha-1") End ' 'SHA-1 hash function, takes an input (maximum 183 character!!) 'and produces a 160-bit(20-byte) hash value Function sha1(m$) As string 'Initializing hash variables Local h0=&h67452301 Local h1=&hEFCDAB89 Local h2=&h98BADCFE Local h3=&h10325476 Local h4=&hC3D2E1F0 ' Local block$(2) As string 'or chunk Local word(79) As integer Local w As integer 'w ord counter Local pm$ As string Local ol, pl As integer 'original length, padded length Local b, bmax As integer 'b lock max and counter Local wa,wb,wc,wd,we,wf, wk, wtemp As integer ' 'Pre-processing (padding message) ol=Len(m$) pm$=m$+Chr$(&h80) pl=448-((ol+1)*8 Mod 512) pm$=pm$+String$(pl/8, 0) pm$=pm$+Bin2str$(int64,ol*8,big) 'big-endian format! ' 'Break padded message into blocks of 512 bits (maximum of 3) 'This also means a maximum of 183 characters for the incoming message! 'as the basic length of string in mmbasic is 255 characters! Select Case Len(pm$) Case 64 block$(0)=pm$ bmax=0 Case 128 block$(0)=Mid$(pm$,1,64) block$(1)=Mid$(pm$,65,64) bmax=1 Case 192 block$(0)=Mid$(pm$,1,64) block$(1)=Mid$(pm$,65,64) block$(2)=Mid$(pm$,129,64) bmax=2 Case Else bmax=-1 End Select ' For b=0 To bmax 'main loop 'words | split block in 16 32-bit big-endian words w(i), 0 = i = 15 For w=0 To 15 word(w)=Str2bin(uint32,Mid$(block$(b),w*4+1,4),big) Next w For w=16 To 79 ' and extend word(w)=(word(w-3) Xor word(w-8) Xor word(w-14) Xor word(w-16)) word(w) = lrt(word(w), 1) Next w 'Initializing the working variables wa=h0 wb=h1 wc=h2 wd=h3 we=h4 'Compression function For w=0 To 79 Select Case w Case 0 To 19 'wf=(wb And wc) Or ((Not wb) And wd) wf= wd Xor (wb And (wc Xor wd)) 'Alternative required by NOT wk=&h5A827999 Case 20 To 39 wf=wb Xor wc Xor wd wk=&h6ED9EBA1 Case 40 To 59 wf=(wb And wc) Or (wb And wd) Or (wc And wd) wk=&h8F1BBCDC Case 60 To 79 wf=wb Xor wc Xor wd wk=&hCA62C1D6 End Select '... fix it with ... And &hFFFFFFFF ... due to 32bit! wtemp = ( lrt(wa, 5) + wf ) And &hFFFFFFFF wtemp = ( wtemp + we ) And &hFFFFFFFF wtemp = ( wtemp + wk ) And &hFFFFFFFF wtemp = ( wtemp + word(w) ) And &hFFFFFFFF we = wd wd = wc wc = lrt(wb, 30) wb = wa wa = wtemp Next w 'Add this block's hash to result so far 'and the 64to32bit-FIX h0 = ( h0 + wa ) And &hFFFFFFFF h1 = ( h1 + wb ) And &hFFFFFFFF h2 = ( h2 + wc ) And &hFFFFFFFF h3 = ( h3 + wd ) And &hFFFFFFFF h4 = ( h4 + we ) And &hFFFFFFFF Next b 'digest hash - string hex sha1=Hex$(h0,8)+Hex$(h1,8)+Hex$(h2,8)+Hex$(h3,8)+Hex$(h4,8) End Function ' Function lrt(a1,a2) 'left rotate for 32bit lrt =( (a1 << a2) Or (a1 >> (32-a2)) ) And &hFFFFFFFF End Function
> option list PicoMiteVGA MMBasic Version 5.08.00 OPTION SYSTEM I2C GP14,GP15 OPTION COLOURCODE ON OPTION KEYBOARD GR OPTION CPUSPEED (KHz) 252000 OPTION SDCARD GP13, GP10, GP11, GP12 OPTION AUDIO GP4,GP5', ON PWM CHANNEL 2 OPTION RTC AUTO ENABLE OPTION MODBUFF ENABLE > run 004C50C315E7D77687F670F68EE12D6E7579A3FC >
|