Guest User

Untitled

a guest
May 26th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.14 KB | None | 0 0
  1. function bit(p)
  2.   return 2 ^ (p - 1)
  3. -- 1-based indexing
  4. end
  5.  
  6. -- Typical call: if hasbit(x, bit(3)) then ...
  7. function hasbit(x, p)
  8.   return x % (p + p) >= p
  9. end
  10.  
  11. function setbit(x, p)
  12.   return hasbit(x, p) and x or x + p
  13. end
  14.  
  15. function clearbit(x, p)
  16.   return hasbit(x, p) and x - p or x
  17. end
  18.  
  19. function toBin(i, b)
  20.   local sBin = ""
  21.   for n=1,b do
  22.     if hasbit(i,bit(n)) then
  23.       sBin = "1" .. sBin
  24.     else
  25.       sBin = "0" .. sBin
  26.     end
  27.   end
  28.   return sBin
  29. end
  30.  
  31. aHex = {
  32.   [0] = "0",
  33.   [1] = "1",
  34.   [2] = "2",
  35.   [3] = "3",
  36.   [4] = "4",
  37.   [5] = "5",
  38.   [6] = "6",
  39.   [7] = "7",
  40.   [8] = "8",
  41.   [9] = "9",
  42.   [10] = "A",
  43.   [11] = "B",
  44.   [12] = "C",
  45.   [13] = "D",
  46.   [14] = "E",
  47.   [15] = "F",
  48. }
  49. local tArgs = { ... }
  50. if not tArgs[1] then
  51.   print("Usage: hash <TEXT_TO_HASH>")
  52.   return
  53. end
  54. sTestPhrase = "KyoHash31415926535897" .. tArgs[1]
  55.  
  56. nPos = 1;
  57. nStep = 0;
  58. nBits = 32*4
  59. nLoops = 3
  60.  
  61. aBits = {}
  62.  
  63. for n=1,nBits do
  64.   table.insert( aBits, "0" )
  65. end
  66.  
  67. for l=1,nLoops do
  68.   for n=1,string.len(sTestPhrase) do
  69.     local x = toBin(string.byte(string.sub(sTestPhrase,n,n)), 7)
  70.     for n2=1,7 do
  71.       if string.sub(x,n2,n2) == "1" then
  72.         if nStep > 3 then
  73.           nStep = nStep - 2
  74.         end
  75.         if aBits[nPos] == "1" then
  76.           aBits[nPos] = "0"
  77.         else
  78.           aBits[nPos] = "1"
  79.         end
  80.       else
  81.         nStep = nStep + nStep + 1
  82.         nPos = nPos + nStep
  83.         if nStep > nBits then
  84.           nStep = nStep - nBits
  85.         end
  86.       end
  87.       nPos = nPos + 4
  88.       if nPos > nBits then
  89.         local nTemp = aBits[1]
  90.         for n3=2,nBits do
  91.           aBits[n3 - 1] = aBits[n3]
  92.         end
  93.         aBits[nBits] = nTemp
  94.         nPos = nPos - nBits
  95.       end
  96.     end
  97.   end
  98.   term.clear()
  99.   term.setCursorPos(1,1)
  100.   for n=1,nBits do
  101.     io.write(aBits[n])
  102.   end
  103.   io.write("\n\n" .. nStep .. " " .. nPos .. "\n\n")
  104.   sleep(0.01)
  105. end
  106.  
  107.  
  108. print("")
  109. nCount = 0
  110. sHash = ""
  111. for n=1,nBits/4 do
  112.   sTemp = ""
  113.   for n2=1,4 do
  114.     nCount = nCount + 1
  115.     sTemp = sTemp .. aBits[nCount]
  116.   end
  117.   sHash = sHash .. aHex[tonumber(sTemp,2)]
  118. end
  119. print("")
  120. print(sHash)
Add Comment
Please, Sign In to add comment