Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bit(p)
- return 2 ^ (p - 1)
- -- 1-based indexing
- end
- -- Typical call: if hasbit(x, bit(3)) then ...
- function hasbit(x, p)
- return x % (p + p) >= p
- end
- function setbit(x, p)
- return hasbit(x, p) and x or x + p
- end
- function clearbit(x, p)
- return hasbit(x, p) and x - p or x
- end
- function toBin(i, b)
- local sBin = ""
- for n=1,b do
- if hasbit(i,bit(n)) then
- sBin = "1" .. sBin
- else
- sBin = "0" .. sBin
- end
- end
- return sBin
- end
- aHex = {
- [0] = "0",
- [1] = "1",
- [2] = "2",
- [3] = "3",
- [4] = "4",
- [5] = "5",
- [6] = "6",
- [7] = "7",
- [8] = "8",
- [9] = "9",
- [10] = "A",
- [11] = "B",
- [12] = "C",
- [13] = "D",
- [14] = "E",
- [15] = "F",
- }
- local tArgs = { ... }
- if not tArgs[1] then
- print("Usage: hash <TEXT_TO_HASH>")
- return
- end
- sTestPhrase = "KyoHash31415926535897" .. tArgs[1]
- nPos = 1;
- nStep = 0;
- nBits = 32*4
- nLoops = 3
- aBits = {}
- for n=1,nBits do
- table.insert( aBits, "0" )
- end
- for l=1,nLoops do
- for n=1,string.len(sTestPhrase) do
- local x = toBin(string.byte(string.sub(sTestPhrase,n,n)), 7)
- for n2=1,7 do
- if string.sub(x,n2,n2) == "1" then
- if nStep > 3 then
- nStep = nStep - 2
- end
- if aBits[nPos] == "1" then
- aBits[nPos] = "0"
- else
- aBits[nPos] = "1"
- end
- else
- nStep = nStep + nStep + 1
- nPos = nPos + nStep
- if nStep > nBits then
- nStep = nStep - nBits
- end
- end
- nPos = nPos + 4
- if nPos > nBits then
- local nTemp = aBits[1]
- for n3=2,nBits do
- aBits[n3 - 1] = aBits[n3]
- end
- aBits[nBits] = nTemp
- nPos = nPos - nBits
- end
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- for n=1,nBits do
- io.write(aBits[n])
- end
- io.write("\n\n" .. nStep .. " " .. nPos .. "\n\n")
- sleep(0.01)
- end
- print("")
- nCount = 0
- sHash = ""
- for n=1,nBits/4 do
- sTemp = ""
- for n2=1,4 do
- nCount = nCount + 1
- sTemp = sTemp .. aBits[nCount]
- end
- sHash = sHash .. aHex[tonumber(sTemp,2)]
- end
- print("")
- print(sHash)
Add Comment
Please, Sign In to add comment