Advertisement
Alakazard12

Lua RSA

Jul 29th, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.92 KB | None | 0 0
  1. -------------------------------------------------
  2. ---      *** BitLibEmu for Lua ***            ---
  3. -------------------------------------------------
  4. --- Author:  Martin Huesser                   ---
  5. --- Date:    2008-06-16                       ---
  6. --- License: You may use this code in your    ---
  7. ---          projects as long as this header  ---
  8. ---          stays intact.                    ---
  9. -------------------------------------------------
  10.  
  11. local mod   = math.fmod
  12. local floor = math.floor
  13. bit = {}
  14.  
  15. ----------------------------------------
  16.  
  17. local function cap(x)
  18.     return mod(x,4294967296)
  19. end
  20.  
  21. ----------------------------------------
  22.  
  23. function bit.bnot(x)
  24.     return 4294967295-cap(x)
  25. end
  26.  
  27. ----------------------------------------
  28.  
  29. function bit.lshift(x,n)
  30.     return cap(cap(x)*2^n)
  31. end
  32.  
  33. ----------------------------------------
  34.  
  35. function bit.rshift(x,n)
  36.     return floor(cap(x)/2^n)
  37. end
  38.  
  39. ----------------------------------------
  40.  
  41. function bit.band(x,y)
  42.     local z,i,j = 0,1
  43.     for j = 0,31 do
  44.         if (mod(x,2)==1 and mod(y,2)==1) then
  45.             z = z + i
  46.         end
  47.         x = bit.rshift(x,1)
  48.         y = bit.rshift(y,1)
  49.         i = i*2
  50.     end
  51.     return z
  52. end
  53.  
  54. ----------------------------------------
  55.  
  56. function bit.bor(x,y)
  57.     local z,i,j = 0,1
  58.     for j = 0,31 do
  59.         if (mod(x,2)==1 or mod(y,2)==1) then
  60.             z = z + i
  61.         end
  62.         x = bit.rshift(x,1)
  63.         y = bit.rshift(y,1)
  64.         i = i*2
  65.     end
  66.     return z
  67. end
  68.  
  69. ----------------------------------------
  70.  
  71. function bit.bxor(x,y)
  72.     local z,i,j = 0,1
  73.     for j = 0,31 do
  74.         if (mod(x,2)~=mod(y,2)) then
  75.             z = z + i
  76.         end
  77.         x = bit.rshift(x,1)
  78.         y = bit.rshift(y,1)
  79.         i = i*2
  80.     end
  81.     return z
  82. end
  83.  
  84.  
  85.  
  86.  
  87.  
  88. -------------------------------------------------
  89. ---      *** BigInteger for Lua ***           ---
  90. -------------------------------------------------
  91. --- Author:  Martin Huesser                   ---
  92. --- Date:    2008-06-16                       ---
  93. --- License: You may use this code in your    ---
  94. ---          projects as long as this header  ---
  95. ---          stays intact.                    ---
  96. -------------------------------------------------
  97.  
  98. ---------------------------------------
  99. --- Lua 5.0/5.1/WoW Header ------------
  100. ---------------------------------------
  101.  
  102. local strlen  = string.len
  103. local strchar = string.char
  104. local strbyte = string.byte
  105. local strsub  = string.sub
  106. local max     = math.max
  107. local min     = math.min
  108. local floor   = math.floor
  109. local ceil    = math.ceil
  110. local mod     = math.fmod
  111. local getn    = function(t) return #t end
  112. local setn    = function() end
  113. local tinsert = table.insert
  114.  
  115. ---------------------------------------
  116. --- Helper Functions ------------------
  117. ---------------------------------------
  118.  
  119. local function Digit(x,i)                   --returns i-th digit or zero
  120.     local d = x[i]                      --if out of bounds
  121.     if (d==nil) then
  122.         return 0
  123.     end
  124.     return d
  125. end
  126.  
  127. ---------------------------------------
  128.  
  129. local function Clean(x)                     --remove leading zeros
  130.     local i = getn(x)
  131.     while (i>1 and x[i]==0) do
  132.         x[i] = nil
  133.         i = i-1
  134.     end
  135.     setn(x,i)
  136. end
  137.  
  138. ---------------------------------------
  139. --- String Conversion -----------------
  140. ---------------------------------------
  141.  
  142. local function Hex(i)                       --convert number to ascii
  143.     if (i>-1 and i<10) then
  144.         return strchar(48+i)
  145.     end
  146.     if (i>9 and i<16) then
  147.         return strchar(55+i)
  148.     end
  149.     return strchar(48)
  150. end
  151.  
  152. ---------------------------------------
  153.  
  154. local function Dec(i)                       --convert ascii to number
  155.     if (i==nil) then
  156.         return 0
  157.     end
  158.     if (i>47 and i<58) then
  159.         return i-48
  160.     end
  161.     if (i>64 and i<71) then
  162.         return i-55
  163.     end
  164.     if (i>96 and i<103) then
  165.         return i-87
  166.     end
  167.     return 0
  168. end
  169.  
  170. ---------------------------------------
  171.  
  172. function BigInt_NumToHex(x)                 --convert number to hexstring
  173.     local s,i,j,c = ""
  174.     for i = 1,getn(x) do
  175.         c = x[i]
  176.         for j = 1,6 do
  177.             s = Hex(mod(c,16))..s
  178.             c = floor(c/16)
  179.         end
  180.     end
  181.     i = 1
  182.     while (i<strlen(s) and strbyte(s,i)==48) do
  183.         i = i+1
  184.     end
  185.     return strsub(s,i)
  186. end
  187.  
  188. ---------------------------------------
  189.  
  190. function BigInt_HexToNum(h)                 --convert hexstring to number
  191.     local x,i,j = {}
  192.     for i = 1,ceil(strlen(h)/6) do
  193.         x[i] = 0
  194.         for j = 1,6 do
  195.             x[i] = 16*x[i]+Dec(strbyte(h,max(strlen(h)-6*i+j,0)))
  196.         end
  197.     end
  198.     Clean(x)
  199.     return x
  200. end
  201.  
  202. ---------------------------------------
  203. --- Math Functions --------------------
  204. ---------------------------------------
  205.  
  206. function BigInt_Add(x,y)                    --add numbers
  207.     local z,l,i,r = {},max(getn(x),getn(y))
  208.     z[1] = 0
  209.     for i = 1,l do
  210.         r = Digit(x,i)+Digit(y,i)+z[i]
  211.         if (r>16777215) then
  212.             z[i] = r-16777216
  213.             z[i+1] = 1
  214.         else
  215.             z[i] = r
  216.             z[i+1] = 0
  217.         end
  218.     end
  219.     Clean(z)
  220.     return z
  221. end
  222.  
  223. ---------------------------------------
  224.  
  225. function BigInt_Sub(x,y)                    --subtract numbers
  226.     local z,l,i,r = {},max(getn(x),getn(y))
  227.     z[1] = 0
  228.     for i = 1,l do
  229.         r = Digit(x,i)-Digit(y,i)-z[i]
  230.         if (r<0) then
  231.             z[i] = r+16777216
  232.             z[i+1] = 1
  233.         else
  234.             z[i] = r
  235.             z[i+1] = 0
  236.         end
  237.     end
  238.     if (z[l+1]==1) then
  239.         return nil
  240.     end
  241.     Clean(z)
  242.     return z
  243. end
  244.  
  245. ---------------------------------------
  246.  
  247. function BigInt_Mul(x,y)                    --multiply numbers
  248.     local z,t,i,j,r = {},{}
  249.     for i = getn(y),1,-1 do
  250.         t[1] = 0
  251.         for j = 1,getn(x) do
  252.             r = x[j]*y[i]+t[j]
  253.             t[j+1] = floor(r/16777216)
  254.             t[j] = r-t[j+1]*16777216
  255.         end
  256.         tinsert(z,1,0)
  257.         z = BigInt_Add(z,t)
  258.     end
  259.     Clean(z)
  260.     return z
  261. end
  262.  
  263. ---------------------------------------
  264.  
  265. local function Div2(x)                      --divide number by 2, (modifies
  266.     local u,v,i = 0                     --passed number and returns
  267.     for i = getn(x),1,-1 do                 --remainder)
  268.         v = x[i]
  269.         if (u==1) then
  270.             x[i] = floor(v/2)+8388608
  271.         else
  272.             x[i] = floor(v/2)
  273.         end
  274.         u = mod(v,2)
  275.     end
  276.     Clean(x)
  277.     return u
  278. end
  279.  
  280. ---------------------------------------
  281.  
  282. local function SimpleDiv(x,y)                   --divide numbers, result
  283.     local z,u,v,i,j = {},0                  --must fit into 1 digit!
  284.     j = 16777216
  285.     for i = 1,getn(y) do                    --This function is costly and
  286.         z[i+1] = y[i]                   --may benefit most from an
  287.     end                         --optimized algorithm!
  288.     z[1] = 0
  289.     for i = 23,0,-1 do
  290.         j = j/2
  291.         Div2(z)
  292.         v = BigInt_Sub(x,z)
  293.         if (v~=nil) then
  294.             u = u+j
  295.             x = v
  296.         end
  297.     end
  298.     return u,x
  299. end
  300.  
  301. ---------------------------------------
  302.  
  303. function BigInt_Div(x,y)                    --divide numbers
  304.     local z,u,i,v = {},{},getn(x)
  305.     for v = 1,min(getn(x),getn(y))-1 do
  306.         tinsert(u,1,x[i])
  307.         i = i - 1
  308.     end
  309.     while (i>0) do
  310.         tinsert(u,1,x[i])
  311.         i = i - 1
  312.         v,u = SimpleDiv(u,y)
  313.         tinsert(z,1,v)
  314.     end
  315.     Clean(z)
  316.     return z,u
  317. end
  318.  
  319. ---------------------------------------
  320.  
  321. function BigInt_ModPower(b,e,m)                 --calculate b^e mod m
  322.     local t,s,r = {},{1}
  323.     for r = 1,getn(e) do
  324.         t[r] = e[r]
  325.     end
  326.     repeat
  327.         r = Div2(t)
  328.         --print(getn(t))
  329.         if (r==1) then
  330.             r,s = BigInt_Div(BigInt_Mul(s,b),m)
  331.         end
  332.         r,b = BigInt_Div(BigInt_Mul(b,b),m)
  333.     until (getn(t)==1 and t[1]==0)
  334.     return s
  335. end
  336.  
  337. ---------------------------------------
  338. --- ModPower Step Functions -----------
  339. ---------------------------------------
  340.  
  341. function BigInt_MP_StepInit(b,e,m)              --initialize nonblocking ModPower,
  342.     local x,i = {b,{},m,{1},1}              --pass resulting table to StepExec!
  343.     for i = 1,getn(e) do
  344.         x[2][i] = e[i]
  345.     end
  346.     return x
  347. end
  348.  
  349. ---------------------------------------
  350.  
  351. function BigInt_MP_StepExec(x)                  --execute next calculation step,
  352.     local r                         --finished if result~=nil.
  353.     if (x[5]==1) then
  354.         x[5] = 2
  355.         r = Div2(x[2])
  356.         if (r==1) then
  357.             r,x[4] = BigInt_Div(BigInt_Mul(x[4],x[1]),x[3])
  358.         end
  359.         return nil
  360.     end
  361.     if (x[5]==2) then
  362.         x[5] = 1
  363.         r,x[1] = BigInt_Div(BigInt_Mul(x[1],x[1]),x[3])
  364.         if (getn(x[2])==1 and x[2][1]==0) then
  365.             x[5] = 0
  366.             return x[4]
  367.         end
  368.         return nil
  369.     end
  370.     return nil
  371. end
  372.  
  373. -------------------------------------------------
  374. -------------------------------------------------
  375. -------------------------------------------------
  376.  
  377.  
  378.  
  379.  
  380.  
  381. -------------------------------------------------
  382. ---      *** SHA-1 algorithm for Lua ***      ---
  383. -------------------------------------------------
  384. --- Author:  Martin Huesser                   ---
  385. --- Date:    2008-06-16                       ---
  386. --- License: You may use this code in your    ---
  387. ---          projects as long as this header  ---
  388. ---          stays intact.                    ---
  389. -------------------------------------------------
  390.  
  391. local strlen  = string.len
  392. local strchar = string.char
  393. local strbyte = string.byte
  394. local strsub  = string.sub
  395. local floor   = math.floor
  396. local bnot    = bit.bnot
  397. local band    = bit.band
  398. local bor     = bit.bor
  399. local bxor    = bit.bxor
  400. local shl     = bit.lshift
  401. local shr     = bit.rshift
  402. local h0, h1, h2, h3, h4
  403.  
  404. -------------------------------------------------
  405.  
  406. local function LeftRotate(val, nr)
  407.     return shl(val, nr) + shr(val, 32 - nr)
  408. end
  409.  
  410. -------------------------------------------------
  411.  
  412. local function ToHex(num)
  413.     local i, d
  414.     local str = ""
  415.     for i = 1, 8 do
  416.         d = band(num, 15)
  417.         if (d < 10) then
  418.             str = strchar(d + 48) .. str
  419.         else
  420.             str = strchar(d + 87) .. str
  421.         end
  422.         num = floor(num / 16)
  423.     end
  424.     return str
  425. end
  426.  
  427. -------------------------------------------------
  428.  
  429. local function PreProcess(str)
  430.     local bitlen, i
  431.     local str2 = ""
  432.     bitlen = strlen(str) * 8
  433.     str = str .. strchar(128)
  434.     i = 56 - band(strlen(str), 63)
  435.     if (i < 0) then
  436.         i = i + 64
  437.     end
  438.     for i = 1, i do
  439.         str = str .. strchar(0)
  440.     end
  441.     for i = 1, 8 do
  442.         str2 = strchar(band(bitlen, 255)) .. str2
  443.         bitlen = floor(bitlen / 256)
  444.     end
  445.     return str .. str2
  446. end
  447.  
  448. -------------------------------------------------
  449.  
  450. local function MainLoop(str)
  451.     local a, b, c, d, e, f, k, t
  452.     local i, j
  453.     local w = {}
  454.     while (str ~= "") do
  455.         for i = 0, 15 do
  456.             w[i] = 0
  457.             for j = 1, 4 do
  458.                 w[i] = w[i] * 256 + strbyte(str, i * 4 + j)
  459.             end
  460.         end
  461.         for i = 16, 79 do
  462.             w[i] = LeftRotate(bxor(bxor(w[i - 3], w[i - 8]), bxor(w[i - 14], w[i - 16])), 1)
  463.         end
  464.         a = h0
  465.         b = h1
  466.         c = h2
  467.         d = h3
  468.         e = h4
  469.         for i = 0, 79 do
  470.             if (i < 20) then
  471.                 f = bor(band(b, c), band(bnot(b), d))
  472.                 k = 1518500249
  473.             elseif (i < 40) then
  474.                 f = bxor(bxor(b, c), d)
  475.                 k = 1859775393
  476.             elseif (i < 60) then
  477.                 f = bor(bor(band(b, c), band(b, d)), band(c, d))
  478.                 k = 2400959708
  479.             else
  480.                 f = bxor(bxor(b, c), d)
  481.                 k = 3395469782
  482.             end
  483.             t = LeftRotate(a, 5) + f + e + k + w[i]
  484.             e = d
  485.             d = c
  486.             c = LeftRotate(b, 30)
  487.             b = a
  488.             a = t
  489.         end
  490.         h0 = band(h0 + a, 4294967295)
  491.         h1 = band(h1 + b, 4294967295)
  492.         h2 = band(h2 + c, 4294967295)
  493.         h3 = band(h3 + d, 4294967295)
  494.         h4 = band(h4 + e, 4294967295)
  495.         str = strsub(str, 65)
  496.     end
  497. end
  498.  
  499. -------------------------------------------------
  500.  
  501. function Sha1(str)
  502.     str = PreProcess(str)
  503.     h0  = 1732584193
  504.     h1  = 4023233417
  505.     h2  = 2562383102
  506.     h3  = 0271733878
  507.     h4  = 3285377520
  508.     MainLoop(str)
  509.     return  ToHex(h0) ..
  510.         ToHex(h1) ..
  511.         ToHex(h2) ..
  512.         ToHex(h3) ..
  513.         ToHex(h4)
  514. end
  515.  
  516. -------------------------------------------------
  517. -------------------------------------------------
  518. -------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement