Advertisement
Anonomit

CraftyComputer

Jan 30th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.31 KB | None | 0 0
  1. --[[
  2.    _____            __ _          _____                            _            
  3.   / ____|          / _| |        / ____|                          | |          
  4.  | |     _ __ __ _| |_| |_ _   _| |     ___  _ __ ___  _ __  _   _| |_ ___ _ __
  5.  | |    | '__/ _` |  _| __| | | | |    / _ \| '_ ` _ \| '_ \| | | | __/ _ \ '__|
  6.  | |____| | | (_| | | | |_| |_| | |___| (_) | | | | | | |_) | |_| | ||  __/ |  
  7.   \_____|_|  \__,_|_|  \__|\__, |\_____\___/|_| |_| |_| .__/ \__,_|\__\___|_|  
  8.                             __/ |                     | |                      
  9.                            |___/                      |_|                      
  10. ]]--
  11.  
  12. --Created by Anonomit
  13.  
  14.  
  15. local sDir = "craftycomputer"
  16.  
  17.  
  18. local sPassword
  19. local tHistory = {}
  20. local tPrograms = { "clear", "exit", "help", "id", "programs", "reboot", "shell", "shutdown", "time" }
  21. local ZERO, hex_to_bits, from_hex, COPY, ADD, XOR, AND, OR, OR3, NOT, ROTATE, asHEX, sha1
  22.  
  23.  
  24.  
  25. do --[[SHA1 HASHING FUNCTIONS]]--
  26.    
  27.     do --[[NOTICE]]--
  28.     --[[
  29.        
  30.         This SHA1 algorithm was created by Jeffrey Friedl.
  31.         Code can be found online at
  32.         http://regex.info/code/sha1.lua
  33.        
  34.     ]]--
  35.     end --[[END NOTICE]]--
  36.    
  37.     do --[[HOW TO USE]]--
  38.     --[[
  39.        
  40.         local hash_as_hex   = sha1(message)            -- returns a hex string
  41.         local hash_as_data  = sha1_binary(message)     -- returns raw bytes
  42.         --
  43.         local hmac_as_hex   = hmac_sha1(key, message)        -- hex string
  44.         local hmac_as_data  = hmac_sha1_binary(key, message) -- raw bytes
  45.        
  46.     ]]--
  47.     end --[[END HOW TO USE]]--
  48.    
  49.     do --[[SHA1 HELPER FUNCTIONS]]--
  50.  
  51.         function ZERO()
  52.            return {
  53.               false, false, false, false,     false, false, false, false,
  54.               false, false, false, false,     false, false, false, false,
  55.               false, false, false, false,     false, false, false, false,
  56.               false, false, false, false,     false, false, false, false,
  57.            }
  58.         end
  59.  
  60.         local hex_to_bits = {
  61.            ["0"] = { false, false, false, false },
  62.            ["1"] = { false, false, false, true  },
  63.            ["2"] = { false, false, true,  false },
  64.            ["3"] = { false, false, true,  true  },
  65.  
  66.            ["4"] = { false, true,  false, false },
  67.            ["5"] = { false, true,  false, true  },
  68.            ["6"] = { false, true,  true,  false },
  69.            ["7"] = { false, true,  true,  true  },
  70.  
  71.            ["8"] = { true,  false, false, false },
  72.            ["9"] = { true,  false, false, true  },
  73.            ["A"] = { true,  false, true,  false },
  74.            ["B"] = { true,  false, true,  true  },
  75.  
  76.            ["C"] = { true,  true,  false, false },
  77.            ["D"] = { true,  true,  false, true  },
  78.            ["E"] = { true,  true,  true,  false },
  79.            ["F"] = { true,  true,  true,  true  },
  80.  
  81.            ["a"] = { true,  false, true,  false },
  82.            ["b"] = { true,  false, true,  true  },
  83.            ["c"] = { true,  true,  false, false },
  84.            ["d"] = { true,  true,  false, true  },
  85.            ["e"] = { true,  true,  true,  false },
  86.            ["f"] = { true,  true,  true,  true  },
  87.         }
  88.  
  89.         --
  90.         -- Given a string of 8 hex digits, return a W32 object representing that number
  91.         --
  92.         function from_hex(hex)
  93.  
  94.            assert(type(hex) == 'string')
  95.            assert(hex:match('^[0123456789abcdefABCDEF]+$'))
  96.            assert(#hex == 8)
  97.  
  98.            local W32 = { }
  99.  
  100.            for letter in hex:gmatch('.') do
  101.               local b = hex_to_bits[letter]
  102.               assert(b)
  103.               table.insert(W32, 1, b[1])
  104.               table.insert(W32, 1, b[2])
  105.               table.insert(W32, 1, b[3])
  106.               table.insert(W32, 1, b[4])
  107.            end
  108.  
  109.            return W32
  110.         end
  111.  
  112.         function COPY(old)
  113.            local W32 = { }
  114.            for k,v in pairs(old) do
  115.               W32[k] = v
  116.            end
  117.  
  118.            return W32
  119.         end
  120.  
  121.         function ADD(first, ...)
  122.  
  123.            local a = COPY(first)
  124.  
  125.            local C, b, sum
  126.  
  127.            for v = 1, select('#', ...) do
  128.               b = select(v, ...)
  129.               C = 0
  130.  
  131.               for i = 1, #a do
  132.                  sum = (a[i] and 1 or 0)
  133.                      + (b[i] and 1 or 0)
  134.                      + C
  135.  
  136.                  if sum == 0 then
  137.                     a[i] = false
  138.                     C    = 0
  139.                  elseif sum == 1 then
  140.                     a[i] = true
  141.                     C    = 0
  142.                  elseif sum == 2 then
  143.                     a[i] = false
  144.                     C    = 1
  145.                  else
  146.                     a[i] = true
  147.                     C    = 1
  148.                  end
  149.               end
  150.               -- we drop any ending carry
  151.  
  152.            end
  153.  
  154.            return a
  155.         end
  156.  
  157.         function XOR(first, ...)
  158.  
  159.            local a = COPY(first)
  160.            local b
  161.            for v = 1, select('#', ...) do
  162.               b = select(v, ...)
  163.               for i = 1, #a do
  164.                  a[i] = a[i] ~= b[i]
  165.               end
  166.            end
  167.  
  168.            return a
  169.  
  170.         end
  171.  
  172.         function AND(a, b)
  173.  
  174.            local c = ZERO()
  175.  
  176.            for i = 1, #a do
  177.               -- only need to set true bits; other bits remain false
  178.               if  a[i] and b[i] then
  179.                  c[i] = true
  180.               end
  181.            end
  182.  
  183.            return c
  184.         end
  185.  
  186.         function OR(a, b)
  187.  
  188.            local c = ZERO()
  189.  
  190.            for i = 1, #a do
  191.               -- only need to set true bits; other bits remain false
  192.               if  a[i] or b[i] then
  193.                  c[i] = true
  194.               end
  195.            end
  196.  
  197.            return c
  198.         end
  199.  
  200.         function OR3(a, b, c)
  201.  
  202.            local d = ZERO()
  203.  
  204.            for i = 1, #a do
  205.               -- only need to set true bits; other bits remain false
  206.               if a[i] or b[i] or c[i] then
  207.                  d[i] = true
  208.               end
  209.            end
  210.  
  211.            return d
  212.         end
  213.  
  214.         function NOT(a)
  215.  
  216.            local b = ZERO()
  217.  
  218.            for i = 1, #a do
  219.               -- only need to set true bits; other bits remain false
  220.               if not a[i] then
  221.                  b[i] = true
  222.               end
  223.            end
  224.  
  225.            return b
  226.         end
  227.  
  228.         function ROTATE(bits, a)
  229.  
  230.            local b = COPY(a)
  231.  
  232.            while bits > 0 do
  233.               bits = bits - 1
  234.               table.insert(b, 1, table.remove(b))
  235.            end
  236.  
  237.            return b
  238.  
  239.         end
  240.  
  241.  
  242.         local binary_to_hex = {
  243.            ["0000"] = "0",
  244.            ["0001"] = "1",
  245.            ["0010"] = "2",
  246.            ["0011"] = "3",
  247.            ["0100"] = "4",
  248.            ["0101"] = "5",
  249.            ["0110"] = "6",
  250.            ["0111"] = "7",
  251.            ["1000"] = "8",
  252.            ["1001"] = "9",
  253.            ["1010"] = "a",
  254.            ["1011"] = "b",
  255.            ["1100"] = "c",
  256.            ["1101"] = "d",
  257.            ["1110"] = "e",
  258.            ["1111"] = "f",
  259.         }
  260.  
  261.         function asHEX(a)
  262.  
  263.            local hex = ""
  264.            local i = 1
  265.            while i < #a do
  266.               local binary = (a[i + 3] and '1' or '0')
  267.                              ..
  268.                              (a[i + 2] and '1' or '0')
  269.                              ..
  270.                              (a[i + 1] and '1' or '0')
  271.                              ..
  272.                              (a[i + 0] and '1' or '0')
  273.  
  274.               hex = binary_to_hex[binary] .. hex
  275.  
  276.               i = i + 4
  277.            end
  278.  
  279.            return hex
  280.  
  281.         end
  282.  
  283.         x67452301 = from_hex("67452301")
  284.         xEFCDAB89 = from_hex("EFCDAB89")
  285.         x98BADCFE = from_hex("98BADCFE")
  286.         x10325476 = from_hex("10325476")
  287.         xC3D2E1F0 = from_hex("C3D2E1F0")
  288.  
  289.         x5A827999 = from_hex("5A827999")
  290.         x6ED9EBA1 = from_hex("6ED9EBA1")
  291.         x8F1BBCDC = from_hex("8F1BBCDC")
  292.         xCA62C1D6 = from_hex("CA62C1D6")
  293.  
  294.     end --[[END SHA1 HELPER FUNCTIONS]]--
  295.  
  296.     do --[[MAIN SHA1 FUNCTIONS]]--
  297.  
  298.         function sha1(msg)
  299.  
  300.            assert(type(msg) == 'string')
  301.            assert(#msg < 0x7FFFFFFF) -- have no idea what would happen if it were large
  302.  
  303.            local H0 = x67452301
  304.            local H1 = xEFCDAB89
  305.            local H2 = x98BADCFE
  306.            local H3 = x10325476
  307.            local H4 = xC3D2E1F0
  308.  
  309.            local msg_len_in_bits = #msg * 8
  310.  
  311.            local first_append = string.char(0x80) -- append a '1' bit plus seven '0' bits
  312.  
  313.            local non_zero_message_bytes = #msg +1 +8 -- the +1 is the appended bit 1, the +8 are for the final appended length
  314.            local current_mod = non_zero_message_bytes % 64
  315.            local second_append = ""
  316.            if current_mod ~= 0 then
  317.               second_append = string.rep(string.char(0), 64 - current_mod)
  318.            end
  319.  
  320.            -- now to append the length as a 64-bit number.
  321.            local B1, R1 = math.modf(msg_len_in_bits  / 0x01000000)
  322.            local B2, R2 = math.modf( 0x01000000 * R1 / 0x00010000)
  323.            local B3, R3 = math.modf( 0x00010000 * R2 / 0x00000100)
  324.            local B4     =            0x00000100 * R3
  325.  
  326.            local L64 = string.char( 0) .. string.char( 0) .. string.char( 0) .. string.char( 0) -- high 32 bits
  327.                     .. string.char(B1) .. string.char(B2) .. string.char(B3) .. string.char(B4) --  low 32 bits
  328.  
  329.  
  330.  
  331.            msg = msg .. first_append .. second_append .. L64        
  332.  
  333.            assert(#msg % 64 == 0)
  334.  
  335.            --local fd = io.open("/tmp/msg", "wb")
  336.            --fd:write(msg)
  337.            --fd:close()
  338.  
  339.            local chunks = #msg / 64
  340.  
  341.            local W = { }
  342.            local start, A, B, C, D, E, f, K, TEMP
  343.            local chunk = 0
  344.  
  345.            while chunk < chunks do
  346.               --
  347.               -- break chunk up into W[0] through W[15]
  348.               --
  349.               start = chunk * 64 + 1
  350.               chunk = chunk + 1
  351.  
  352.               for t = 0, 15 do
  353.                  W[t] = from_hex(string.format("%02x%02x%02x%02x", msg:byte(start, start + 3)))
  354.                  start = start + 4
  355.               end
  356.  
  357.               --
  358.               -- build W[16] through W[79]
  359.               --
  360.               for t = 16, 79 do
  361.                  -- For t = 16 to 79 let Wt = S1(Wt-3 XOR Wt-8 XOR Wt-14 XOR Wt-16).
  362.                  W[t] = ROTATE(1, XOR(W[t-3], W[t-8], W[t-14], W[t-16]))
  363.               end
  364.  
  365.               A = H0
  366.               B = H1
  367.               C = H2
  368.               D = H3
  369.               E = H4
  370.  
  371.               for t = 0, 79 do
  372.                  if t <= 19 then
  373.                     -- (B AND C) OR ((NOT B) AND D)
  374.                     f = OR(AND(B, C), AND(NOT(B), D))
  375.                     K = x5A827999
  376.                  elseif t <= 39 then
  377.                     -- B XOR C XOR D
  378.                     f = XOR(B, C, D)
  379.                     K = x6ED9EBA1
  380.                  elseif t <= 59 then
  381.                     -- (B AND C) OR (B AND D) OR (C AND D
  382.                     f = OR3(AND(B, C), AND(B, D), AND(C, D))
  383.                     K = x8F1BBCDC
  384.                  else
  385.                     -- B XOR C XOR D
  386.                     f = XOR(B, C, D)
  387.                     K = xCA62C1D6
  388.                  end
  389.  
  390.                  -- TEMP = S5(A) + ft(B,C,D) + E + Wt + Kt;
  391.                  TEMP = ADD(ROTATE(5, A), f, E, W[t], K)
  392.  
  393.                  --E = D;   D = C;    C = S30(B);   B = A;   A = TEMP;
  394.                  E = D
  395.                  D = C
  396.                  C = ROTATE(30, B)
  397.                  B = A
  398.                  A = TEMP
  399.  
  400.                  --printf("t = %2d: %s  %s  %s  %s  %s", t, A:HEX(), B:HEX(), C:HEX(), D:HEX(), E:HEX())
  401.               end
  402.  
  403.               -- Let H0 = H0 + A, H1 = H1 + B, H2 = H2 + C, H3 = H3 + D, H4 = H4 + E.
  404.               H0 = ADD(H0, A)
  405.               H1 = ADD(H1, B)
  406.               H2 = ADD(H2, C)
  407.               H3 = ADD(H3, D)
  408.               H4 = ADD(H4, E)
  409.            end
  410.            
  411.            return asHEX(H0) .. asHEX(H1) .. asHEX(H2) .. asHEX(H3) .. asHEX(H4)
  412.         end
  413.        
  414.     end --[[END MAIN SHA1 FUNCTIONS]]--
  415.    
  416. end --[[END SHA1 HASHING FUNCTIONS]]--
  417.  
  418.  
  419.  
  420. local function fColor( _nColor )
  421.     if term.isColor ~= nil then
  422.         if term.isColor() then
  423.             term.setTextColor( _nColor )
  424.         end --if term.isColor()
  425.     end --if term.isColor ~= nil
  426. end --local function fColor( _nColor )
  427.  
  428.  
  429. term.clear()
  430. term.setCursorPos( 1, 1 )
  431.  
  432. if not fs.isDir( "craftycomputer" ) then
  433.     fs.makeDir( "craftycomputer" )
  434. end --if not fs.isDir( "craftycomputer" )
  435.  
  436. if fs.exists( fs.combine( sDir, "password" ) ) then
  437.     local file = fs.open( fs.combine( sDir, "password" ), "r" )
  438.     sPassword = file.readLine()
  439.     file.close()
  440. else --fs.exists( fs.combine( sDir, "password" ) )
  441.     fColor( 2 ^ math.random( 1, 14 ) )
  442.     print( "CraftyComputer" )
  443.     local sV0, sV1
  444.     while true do
  445.         term.setCursorPos( 1, 4 )
  446.         term.clearLine()
  447.         term.setCursorPos( 1, 3 )
  448.         term.clearLine()
  449.         fColor( colors.yellow )
  450.         write( "Set The Password: " )
  451.         fColor( colors.white )
  452.         sV0 = read( "*" )
  453.         fColor( colors.yellow )
  454.         write( "Confirm Password: " )
  455.         fColor( colors.white )
  456.         sV1 = read( "*" )
  457.         if sV0 == sV1 then
  458.             break --while true
  459.         end --if sV0 == sV1
  460.     end --while true
  461.    
  462.     local file = fs.open( fs.combine( sDir, "password" ), "w" )
  463.     sPassword = sha1( sV0 )
  464.     file.writeLine( sPassword )
  465.     file.close()
  466.    
  467. end --if fs.exists( fs.combine( sDir, "craftycomputer" ) )
  468.  
  469.  
  470. local oldPullEvent = os.pullEvent
  471. os.pullEvent = os.pullEventRaw
  472.  
  473.  
  474. term.clear()
  475. term.setCursorPos( 1, 1 )
  476. fColor( colors.yellow )
  477. print( os.version() )
  478.  
  479. local function fConsole()
  480.    
  481.     while true do
  482.        
  483.         fColor( colors.yellow )
  484.         write( "> " )
  485.         fColor( colors.white )
  486.         local sInput = read( nil, tHistory )
  487.         table.insert( tHistory, sInput )
  488.        
  489.         if sha1( sInput ) == sPassword then
  490.             return
  491.         end --if sha1( sInput ) == sPassword
  492.        
  493.         local sInput = string.sub( string.lower( sInput:match( "^%s*(.-)%s*$" ) ), string.find( string.lower( sInput:match( "^%s*(.-)%s*$" ) ), "(%S*)" ) )
  494.        
  495.         if sInput == "programs" then
  496.             textutils.pagedTabulate( tPrograms )
  497.         elseif sInput == "clear" then
  498.             term.clear()
  499.             term.setCursorPos( 1, 1 )
  500.         elseif sInput == "help" then
  501.             print( "No help available" )
  502.         elseif sInput == "id" then
  503.             print( "This is computer #" .. os.computerID() )
  504.         elseif sInput == "shell" then
  505.             fColor( colors.yellow )
  506.             print( os.version() )
  507.         elseif sInput == "time" then
  508.             print( "The time is " .. textutils.formatTime( os.time(), false ) .. " on Day " .. os.day() )
  509.         elseif sInput == "reboot" or sInput == "shutdown" or sInput == "exit" then
  510.             fColor( colors.yellow )
  511.             print( "Goodbye" )
  512.             fColor( colors.white )
  513.             local foo = sInput ~= "exit" and ( function() sleep( 1 ) return true end )() or os.shutdown()
  514.             local foo = sInput == "reboot" and os.reboot() or os.shutdown()
  515.            
  516.         elseif sInput ~= "" then
  517.             fColor( colors.red )
  518.             print( "No such program" )
  519.         end --if sInput == "programs"
  520.        
  521.     end --while true
  522.    
  523. end --local function fConsole()
  524.  
  525.  
  526. local function fTerminate()
  527.    
  528.     os.pullEvent( "terminate" )
  529.    
  530. end --local function fTerminate()
  531.  
  532.  
  533. if parallel.waitForAny( fConsole, fTerminate ) == 1 then
  534.     fColor( 2 ^ math.random( 1, 14 ) )
  535.     print( "Exiting CraftyComputer..." )
  536.     os.pullEvent = oldPullEvent
  537.     sleep( 1 )
  538.     term.clear()
  539.     term.setCursorPos( 1, 1 )
  540.     fColor( colors.yellow )
  541.     print( os.version() )
  542. else --parallel.waitForAny( fConsole, fTerminate ) == 1
  543.     fColor( colors.red )
  544.     print( "Terminated" )
  545.     fColor( colors.white )
  546.     term.setCursorBlink( false )
  547.     print( "Press any key to continue" )
  548.     os.pullEvent = oldPullEvent
  549.     os.pullEvent( "key" )
  550.     os.shutdown()
  551. end --if parallel.waitForAny( fConsole, fTerminate ) == 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement