Alyssa

Krist Address Gen

Feb 5th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.91 KB | None | 0 0
  1. achars = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","e"}
  2. function makeaddressbyte(num)
  3.     --Compressed version of 3d6's 40+ line function.
  4.     div = math.floor(num/7)
  5.     div = div+1
  6.     return achars[div]
  7. end
  8. -- SHA-256 implementation in CC-Lua
  9. -- HMAC and PBKDF2 with SHA-256
  10. -- By Anavrins
  11. local mod32 = 2^32
  12. local sha_hashlen = 32
  13. local sha_blocksize = 64
  14.  
  15. local band    = bit32 and bit32.band or bit.band
  16. local bnot    = bit32 and bit32.bnot or bit.bnot
  17. local bxor    = bit32 and bit32.bxor or bit.bxor
  18. local blshift = bit32 and bit32.lshift or bit.blshift
  19. local upack   = unpack
  20.  
  21. local function rrotate(n, b)
  22.     local s = n/(2^b)
  23.     local f = s%1
  24.     return (s-f) + f*mod32
  25. end
  26. local function brshift(int, by) -- Thanks bit32 for bad rshift
  27.     local s = int / (2^by)
  28.     return s - s%1
  29. end
  30.  
  31. local H = {
  32.     0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  33.     0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
  34. }
  35.  
  36. local K = {
  37.     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  38.     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  39.     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  40.     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  41.     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  42.     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  43.     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  44.     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  45. }
  46.  
  47. local function counter(incr)
  48.     local t1, t2 = 0, 0
  49.     if 0xFFFFFFFF - t1 < incr then
  50.         t2 = t2 + 1
  51.         t1 = incr - (0xFFFFFFFF - t1) - 1      
  52.     else t1 = t1 + incr
  53.     end
  54.     return t2, t1
  55. end
  56.  
  57. local function BE_toInt(bs, i)
  58.     return blshift((bs[i] or 0), 24) + blshift((bs[i+1] or 0), 16) + blshift((bs[i+2] or 0), 8) + (bs[i+3] or 0)
  59. end
  60.  
  61. local function preprocess(data)
  62.     local len = #data
  63.     local proc = {}
  64.     data[#data+1] = 0x80
  65.     while #data%64~=56 do data[#data+1] = 0 end
  66.     local blocks = math.ceil(#data/64)
  67.     for i = 1, blocks do
  68.         proc[i] = {}
  69.         for j = 1, 16 do
  70.             proc[i][j] = BE_toInt(data, 1+((i-1)*64)+((j-1)*4))
  71.         end
  72.     end
  73.     proc[blocks][15], proc[blocks][16] = counter(len*8)
  74.     return proc
  75. end
  76.  
  77. local function digestblock(w, C)
  78.     for j = 17, 64 do
  79.         local v = w[j-15]
  80.         local s0 = bxor(bxor(rrotate(w[j-15], 7), rrotate(w[j-15], 18)), brshift(w[j-15], 3))
  81.         local s1 = bxor(bxor(rrotate(w[j-2], 17), rrotate(w[j-2], 19)), brshift(w[j-2], 10))
  82.         w[j] = (w[j-16] + s0 + w[j-7] + s1)%mod32
  83.     end
  84.     local a, b, c, d, e, f, g, h = upack(C)
  85.     for j = 1, 64 do
  86.         local S1 = bxor(bxor(rrotate(e, 6), rrotate(e, 11)), rrotate(e, 25))
  87.         local ch = bxor(band(e, f), band(bnot(e), g))
  88.         local temp1 = (h + S1 + ch + K[j] + w[j])%mod32
  89.         local S0 = bxor(bxor(rrotate(a, 2), rrotate(a, 13)), rrotate(a, 22))
  90.         local maj = bxor(bxor(band(a, b), band(a, c)), band(b, c))
  91.         local temp2 = (S0 + maj)%mod32
  92.         h, g, f, e, d, c, b, a = g, f, e, (d+temp1)%mod32, c, b, a, (temp1+temp2)%mod32
  93.     end
  94.     C[1] = (C[1] + a)%mod32
  95.     C[2] = (C[2] + b)%mod32
  96.     C[3] = (C[3] + c)%mod32
  97.     C[4] = (C[4] + d)%mod32
  98.     C[5] = (C[5] + e)%mod32
  99.     C[6] = (C[6] + f)%mod32
  100.     C[7] = (C[7] + g)%mod32
  101.     C[8] = (C[8] + h)%mod32
  102.     return C
  103. end
  104.  
  105. local mt = {
  106.     __tostring = function(a) return string.char(unpack(a)) end,
  107.     __index = {
  108.         toHex = function(self, s) return ("%02x"):rep(#self):format(unpack(self)) end,
  109.         isEqual = function(self, t)
  110.             if type(t) ~= "table" then return false end
  111.             if #self ~= #t then return false end
  112.             local ret = 0
  113.             for i = 1, #self do
  114.                 ret = bit32.bor(ret, bxor(self[i], t[i]))
  115.             end
  116.             return ret == 0
  117.         end
  118.     }
  119. }
  120.  
  121. local function toBytes(t, n)
  122.     local b = {}
  123.     for i = 1, n do
  124.         b[(i-1)*4+1] = band(brshift(band(t[i], 0xFF000000), 24), 0xFF)
  125.         b[(i-1)*4+2] = band(brshift(band(t[i], 0xFF0000), 16), 0xFF)
  126.         b[(i-1)*4+3] = band(brshift(band(t[i], 0xFF00), 8), 0xFF)
  127.         b[(i-1)*4+4] = band(t[i], 0xFF)
  128.     end
  129.     return setmetatable(b, mt)
  130. end
  131.  
  132. function sha256(data)
  133.     data = data or ""
  134.     data = type(data) == "string" and {data:byte(1,-1)} or data
  135.  
  136.     data = preprocess(data)
  137.     local C = {upack(H)}
  138.     for i = 1, #data do C = digestblock(data[i], C) end
  139.     return toBytes(C, 8)
  140. end
  141.  
  142. function makev2address(key)
  143.   local protein = {}
  144.   local stick = sha256(sha256(key))
  145.   local n = 0
  146.   local link = 0
  147.   local v2 = "k"
  148.   repeat
  149.     if n < 9 then protein[n] = string.sub(stick,0,2)
  150.     stick = sha256(sha256(stick)) end
  151.     n = n + 1
  152.   until n == 9
  153.   n = 0
  154.   repeat
  155.     link = tonumber(string.sub(stick,1+(2*n),2+(2*n)),16) % 9
  156.     if n == 8 then
  157.         for i = 0, 8, 1 do
  158.             if protein[i]:len() ~= 0 then
  159.                 v2 = v2 .. makeaddressbyte(tonumber(protein[i],16))
  160.                 protein[i] = ""
  161.                 n = n+1
  162.             end
  163.         end
  164.     end
  165.     if string.len(protein[link]) ~= 0 then
  166.       v2 = v2 .. makeaddressbyte(tonumber(protein[link],16))
  167.       protein[link] = ''
  168.       n = n + 1
  169.     else
  170.       stick = sha256(stick)
  171.     end
  172.   until n == 9
  173.   return v2
  174. end
  175. function gen(pass_key, lean)
  176.     if not lean then
  177.         password = sha256("KRISTWALLET"..pass_key)
  178.         masterkey = password.."-000"
  179.     else
  180.         masterkey = pass_key
  181.     end
  182.     addressv1 = string.sub(sha256(masterkey),0,10)
  183.     address = makev2address(masterkey)
  184.     return address, masterkey
  185. end
Add Comment
Please, Sign In to add comment