Advertisement
Kodos

SHA 256

Jun 11th, 2014
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. crypt={}
  2. function crypt.xor(a,b)
  3. b=b:rep(math.ceil(#a/#b)):sub(1,#a)
  4. local o=""
  5. for l1=1,#a do
  6. o=o..string.char(bit.bxor(a:sub(l1,l1):byte(),b:sub(l1,l1):byte()))
  7. end
  8. return o
  9. end
  10. function crypt.rot13(txt)
  11. return txt:gsub("%l",function(c)
  12. return string.char(((c:byte()-84)%26)+97)
  13. end):gsub("%u",function(c)
  14. return string.char(((c:byte()-52)%26)+65)
  15. end)
  16. end
  17. function crypt.rot47(txt)
  18. txt=txt:gsub(".",function(c) if c:byte()<127 and c:byte()>32 then return string.char((((c:byte()-33)+47)%94)+33) end end)
  19. return txt
  20. end
  21. function crypt.tohex(txt)
  22. return ({txt:gsub(".",function(c) return string.format("%02x",c:byte()) end)})[1]
  23. end
  24. function crypt.fromhex(txt)
  25. return ({txt:gsub("%X",""):gsub("%x%x?",function(c) return string.char(tonumber("0x"..c)) end)})[1]
  26. end
  27. hook.new("command_rot13",function(user,chan,txt)
  28. return crypt.rot13(txt)
  29. end,{
  30. desc="most secure encryption ever made",
  31. group="fun",
  32. })
  33. hook.new("command_rot47",function(user,chan,txt)
  34. return crypt.rot47(txt)
  35. end,{
  36. desc="second most secure encryption ever made",
  37. group="fun",
  38. })
  39. function crypt.space(txt)
  40. return tobase(txt,nil,"01234567")
  41. :gsub("0","\226\128\139")
  42. :gsub("1","\226\128\138")
  43. :gsub("2","\226\128\137")
  44. :gsub("3","\226\128\136")
  45. :gsub("4","\194\160")
  46. :gsub("5","\226\128\128")
  47. :gsub("6","\226\128\175")
  48. :gsub("7","\226\129\159")
  49. end
  50. function crypt.unspace(txt)
  51. return tobase(txt
  52. :gsub("\226\128\139","0")
  53. :gsub("\226\128\138","1")
  54. :gsub("\226\128\137","2")
  55. :gsub("\226\128\136","3")
  56. :gsub("\194\160","4")
  57. :gsub("\226\128\128","5")
  58. :gsub("\226\128\175","6")
  59. :gsub("\226\129\159","7")
  60. ,"01234567")
  61. end
  62. function crypt.color(txt,cl)
  63. cl=tobase(cl,nil,"0123456789ABCDEF")
  64. return txt:gsub("%S",function(char)
  65. if cl~="" then
  66. local o="\3"..tobase(cl:sub(1,1),"0123456789ABCDEF","0123456789",2)..char
  67. cl=cl:sub(2)
  68. return o..(#cl==0 and "\15" or "")
  69. end
  70. end)
  71. end
  72. function crypt.decolor(txt)
  73. local o=""
  74. for cl in txt:gmatch("\3(%d%d)") do
  75. o=o..string.format("%X",tonumber(cl))
  76. end
  77. return tobase(o,"0123456789ABCDEF")
  78. end
  79. crypt.hash={}
  80. for k,v in pairs(crypto.list("digests")) do
  81. hook.new({"command_"..v},function(user,chan,txt)
  82. return crypto.digest(v,txt)
  83. end)
  84. crypt.hash[v]=function(txt)
  85. return crypto.digest(v,txt)
  86. end
  87. end
  88. crypt.encrypt={}
  89. for k,v in pairs(crypto.list("ciphers")) do
  90. crypt.encrypt[v]=function(pass)
  91. return crypto.encrypt.new(v,pass)
  92. end
  93. end
  94. crypt.decrypt={}
  95. for k,v in pairs(crypto.list("ciphers")) do
  96. crypt.decrypt[v]=function(pass)
  97. return crypto.decrypt.new(v,pass)
  98. end
  99. end
  100. hook.new({"command_hash","command_digest"},function(user,chan,txt)
  101. local err,res=pcall(crypto.digest,txt:match("^(%S+) ?(.*)$"))
  102. return res
  103. end)
  104.  
  105. function crypt.salt(len)
  106. return crypto.rand.bytes(len)
  107. end
  108.  
  109. local bxor=bit.bxor
  110. local ror=bit.ror
  111. local rshift=bit.rshift
  112. local band=bit.band
  113. local bnot=bit.bnot
  114.  
  115. function chars2num(txt)
  116. return (txt:byte(1)*16777216)+(txt:byte(2)*65536)+(txt:byte(3)*256)+(txt:byte(4))
  117. end
  118. function limit(num)
  119. return band(num)
  120. end
  121. local function num2chars(num,l)
  122. local out=""
  123. for l1=1,l or 4 do
  124. out=string.char(math.floor(num/(256^(l1-1)))%256)..out
  125. end
  126. return out
  127. end
  128. local k={
  129. [0]=0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  130. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  131. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  132. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  133. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  134. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  135. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  136. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  137. }
  138. function sha256(txt)
  139. local h0=0
  140. local h1=0
  141. local h2=0
  142. local h3=0
  143. local h4=0
  144. local h5=0
  145. local h6=0
  146. local h7=0
  147. local len=#txt
  148. txt=txt.."\128"..("\0"):rep(64-((len+9)%64))..num2chars(8*len,8)
  149. local w={}
  150. for chunkind=1,len,64 do
  151. local rawchunk=txt:sub(chunkind,chunkind+63)
  152. local chunk={}
  153. for i=1,64,4 do
  154. chunk[math.floor(i/4)]=chars2num(rawchunk:sub(i))
  155. end
  156. for i=0,15 do
  157. w[i]=chunk[i]
  158. end
  159. for i=16,63 do
  160. local s0=bxor(ror(w[i-15],7),ror(w[i-15],18),rshift(w[i-15],3))
  161. local s1=bxor(ror(w[i-2],17),ror(w[i-2],19),rshift(w[i-2],10))
  162. w[i]=w[i-16]+s0+w[i-7]+s1
  163. end
  164. local a=h0
  165. local b=h1
  166. local c=h2
  167. local d=h3
  168. local e=h4
  169. local f=h5
  170. local g=h6
  171. local h=h7
  172. for i=0,63 do
  173. local S1=bxor(ror(e,6),ror(e,11),ror(e,25))
  174. local ch=bxor(band(e,f),band(bnot(e),g))
  175. local temp1=limit(limit(h+S1)+limit(ch+k[i]+w[i]))
  176. local S0=bxor(ror(a,2),ror(a,13),ror(a,22))
  177. local maj=bxor(band(a,b),band(a,c),band(b,c))
  178. local temp2=S0+maj
  179. a,b,c,d,e,f,g,h=limit(temp1+temp2),a,b,c,limit(d+temp1),e,f,g
  180. end
  181. h0,h1,h2,h3,h4,h5,h6,h7=limit(h0+a),limit(h1+b),limit(h2+c),limit(h3+d),limit(h4+e),limit(h5+f),limit(h6+g),limit(h7+h)
  182. end
  183. return num2chars(h0)..num2chars(h1)..num2chars(h2)..num2chars(h3)..num2chars(h4)..num2chars(h5)..num2chars(h6)..num2chars(h7)
  184. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement