Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- _____ __ _ _____ _
- / ____| / _| | / ____| | |
- | | _ __ __ _| |_| |_ _ _| | ___ _ __ ___ _ __ _ _| |_ ___ _ __
- | | | '__/ _` | _| __| | | | | / _ \| '_ ` _ \| '_ \| | | | __/ _ \ '__|
- | |____| | | (_| | | | |_| |_| | |___| (_) | | | | | | |_) | |_| | || __/ |
- \_____|_| \__,_|_| \__|\__, |\_____\___/|_| |_| |_| .__/ \__,_|\__\___|_|
- __/ | | |
- |___/ |_|
- ]]--
- --Created by Anonomit
- local sDir = "craftycomputer"
- local sPassword
- local tHistory = {}
- local tPrograms = { "clear", "exit", "help", "id", "programs", "reboot", "shell", "shutdown", "time" }
- local ZERO, hex_to_bits, from_hex, COPY, ADD, XOR, AND, OR, OR3, NOT, ROTATE, asHEX, sha1
- do --[[SHA1 HASHING FUNCTIONS]]--
- do --[[NOTICE]]--
- --[[
- This SHA1 algorithm was created by Jeffrey Friedl.
- Code can be found online at
- http://regex.info/code/sha1.lua
- ]]--
- end --[[END NOTICE]]--
- do --[[HOW TO USE]]--
- --[[
- local hash_as_hex = sha1(message) -- returns a hex string
- local hash_as_data = sha1_binary(message) -- returns raw bytes
- --
- local hmac_as_hex = hmac_sha1(key, message) -- hex string
- local hmac_as_data = hmac_sha1_binary(key, message) -- raw bytes
- ]]--
- end --[[END HOW TO USE]]--
- do --[[SHA1 HELPER FUNCTIONS]]--
- function ZERO()
- return {
- false, false, false, false, false, false, false, false,
- false, false, false, false, false, false, false, false,
- false, false, false, false, false, false, false, false,
- false, false, false, false, false, false, false, false,
- }
- end
- local hex_to_bits = {
- ["0"] = { false, false, false, false },
- ["1"] = { false, false, false, true },
- ["2"] = { false, false, true, false },
- ["3"] = { false, false, true, true },
- ["4"] = { false, true, false, false },
- ["5"] = { false, true, false, true },
- ["6"] = { false, true, true, false },
- ["7"] = { false, true, true, true },
- ["8"] = { true, false, false, false },
- ["9"] = { true, false, false, true },
- ["A"] = { true, false, true, false },
- ["B"] = { true, false, true, true },
- ["C"] = { true, true, false, false },
- ["D"] = { true, true, false, true },
- ["E"] = { true, true, true, false },
- ["F"] = { true, true, true, true },
- ["a"] = { true, false, true, false },
- ["b"] = { true, false, true, true },
- ["c"] = { true, true, false, false },
- ["d"] = { true, true, false, true },
- ["e"] = { true, true, true, false },
- ["f"] = { true, true, true, true },
- }
- --
- -- Given a string of 8 hex digits, return a W32 object representing that number
- --
- function from_hex(hex)
- assert(type(hex) == 'string')
- assert(hex:match('^[0123456789abcdefABCDEF]+$'))
- assert(#hex == 8)
- local W32 = { }
- for letter in hex:gmatch('.') do
- local b = hex_to_bits[letter]
- assert(b)
- table.insert(W32, 1, b[1])
- table.insert(W32, 1, b[2])
- table.insert(W32, 1, b[3])
- table.insert(W32, 1, b[4])
- end
- return W32
- end
- function COPY(old)
- local W32 = { }
- for k,v in pairs(old) do
- W32[k] = v
- end
- return W32
- end
- function ADD(first, ...)
- local a = COPY(first)
- local C, b, sum
- for v = 1, select('#', ...) do
- b = select(v, ...)
- C = 0
- for i = 1, #a do
- sum = (a[i] and 1 or 0)
- + (b[i] and 1 or 0)
- + C
- if sum == 0 then
- a[i] = false
- C = 0
- elseif sum == 1 then
- a[i] = true
- C = 0
- elseif sum == 2 then
- a[i] = false
- C = 1
- else
- a[i] = true
- C = 1
- end
- end
- -- we drop any ending carry
- end
- return a
- end
- function XOR(first, ...)
- local a = COPY(first)
- local b
- for v = 1, select('#', ...) do
- b = select(v, ...)
- for i = 1, #a do
- a[i] = a[i] ~= b[i]
- end
- end
- return a
- end
- function AND(a, b)
- local c = ZERO()
- for i = 1, #a do
- -- only need to set true bits; other bits remain false
- if a[i] and b[i] then
- c[i] = true
- end
- end
- return c
- end
- function OR(a, b)
- local c = ZERO()
- for i = 1, #a do
- -- only need to set true bits; other bits remain false
- if a[i] or b[i] then
- c[i] = true
- end
- end
- return c
- end
- function OR3(a, b, c)
- local d = ZERO()
- for i = 1, #a do
- -- only need to set true bits; other bits remain false
- if a[i] or b[i] or c[i] then
- d[i] = true
- end
- end
- return d
- end
- function NOT(a)
- local b = ZERO()
- for i = 1, #a do
- -- only need to set true bits; other bits remain false
- if not a[i] then
- b[i] = true
- end
- end
- return b
- end
- function ROTATE(bits, a)
- local b = COPY(a)
- while bits > 0 do
- bits = bits - 1
- table.insert(b, 1, table.remove(b))
- end
- return b
- end
- local binary_to_hex = {
- ["0000"] = "0",
- ["0001"] = "1",
- ["0010"] = "2",
- ["0011"] = "3",
- ["0100"] = "4",
- ["0101"] = "5",
- ["0110"] = "6",
- ["0111"] = "7",
- ["1000"] = "8",
- ["1001"] = "9",
- ["1010"] = "a",
- ["1011"] = "b",
- ["1100"] = "c",
- ["1101"] = "d",
- ["1110"] = "e",
- ["1111"] = "f",
- }
- function asHEX(a)
- local hex = ""
- local i = 1
- while i < #a do
- local binary = (a[i + 3] and '1' or '0')
- ..
- (a[i + 2] and '1' or '0')
- ..
- (a[i + 1] and '1' or '0')
- ..
- (a[i + 0] and '1' or '0')
- hex = binary_to_hex[binary] .. hex
- i = i + 4
- end
- return hex
- end
- x67452301 = from_hex("67452301")
- xEFCDAB89 = from_hex("EFCDAB89")
- x98BADCFE = from_hex("98BADCFE")
- x10325476 = from_hex("10325476")
- xC3D2E1F0 = from_hex("C3D2E1F0")
- x5A827999 = from_hex("5A827999")
- x6ED9EBA1 = from_hex("6ED9EBA1")
- x8F1BBCDC = from_hex("8F1BBCDC")
- xCA62C1D6 = from_hex("CA62C1D6")
- end --[[END SHA1 HELPER FUNCTIONS]]--
- do --[[MAIN SHA1 FUNCTIONS]]--
- function sha1(msg)
- assert(type(msg) == 'string')
- assert(#msg < 0x7FFFFFFF) -- have no idea what would happen if it were large
- local H0 = x67452301
- local H1 = xEFCDAB89
- local H2 = x98BADCFE
- local H3 = x10325476
- local H4 = xC3D2E1F0
- local msg_len_in_bits = #msg * 8
- local first_append = string.char(0x80) -- append a '1' bit plus seven '0' bits
- local non_zero_message_bytes = #msg +1 +8 -- the +1 is the appended bit 1, the +8 are for the final appended length
- local current_mod = non_zero_message_bytes % 64
- local second_append = ""
- if current_mod ~= 0 then
- second_append = string.rep(string.char(0), 64 - current_mod)
- end
- -- now to append the length as a 64-bit number.
- local B1, R1 = math.modf(msg_len_in_bits / 0x01000000)
- local B2, R2 = math.modf( 0x01000000 * R1 / 0x00010000)
- local B3, R3 = math.modf( 0x00010000 * R2 / 0x00000100)
- local B4 = 0x00000100 * R3
- local L64 = string.char( 0) .. string.char( 0) .. string.char( 0) .. string.char( 0) -- high 32 bits
- .. string.char(B1) .. string.char(B2) .. string.char(B3) .. string.char(B4) -- low 32 bits
- msg = msg .. first_append .. second_append .. L64
- assert(#msg % 64 == 0)
- --local fd = io.open("/tmp/msg", "wb")
- --fd:write(msg)
- --fd:close()
- local chunks = #msg / 64
- local W = { }
- local start, A, B, C, D, E, f, K, TEMP
- local chunk = 0
- while chunk < chunks do
- --
- -- break chunk up into W[0] through W[15]
- --
- start = chunk * 64 + 1
- chunk = chunk + 1
- for t = 0, 15 do
- W[t] = from_hex(string.format("%02x%02x%02x%02x", msg:byte(start, start + 3)))
- start = start + 4
- end
- --
- -- build W[16] through W[79]
- --
- for t = 16, 79 do
- -- For t = 16 to 79 let Wt = S1(Wt-3 XOR Wt-8 XOR Wt-14 XOR Wt-16).
- W[t] = ROTATE(1, XOR(W[t-3], W[t-8], W[t-14], W[t-16]))
- end
- A = H0
- B = H1
- C = H2
- D = H3
- E = H4
- for t = 0, 79 do
- if t <= 19 then
- -- (B AND C) OR ((NOT B) AND D)
- f = OR(AND(B, C), AND(NOT(B), D))
- K = x5A827999
- elseif t <= 39 then
- -- B XOR C XOR D
- f = XOR(B, C, D)
- K = x6ED9EBA1
- elseif t <= 59 then
- -- (B AND C) OR (B AND D) OR (C AND D
- f = OR3(AND(B, C), AND(B, D), AND(C, D))
- K = x8F1BBCDC
- else
- -- B XOR C XOR D
- f = XOR(B, C, D)
- K = xCA62C1D6
- end
- -- TEMP = S5(A) + ft(B,C,D) + E + Wt + Kt;
- TEMP = ADD(ROTATE(5, A), f, E, W[t], K)
- --E = D; D = C; C = S30(B); B = A; A = TEMP;
- E = D
- D = C
- C = ROTATE(30, B)
- B = A
- A = TEMP
- --printf("t = %2d: %s %s %s %s %s", t, A:HEX(), B:HEX(), C:HEX(), D:HEX(), E:HEX())
- end
- -- Let H0 = H0 + A, H1 = H1 + B, H2 = H2 + C, H3 = H3 + D, H4 = H4 + E.
- H0 = ADD(H0, A)
- H1 = ADD(H1, B)
- H2 = ADD(H2, C)
- H3 = ADD(H3, D)
- H4 = ADD(H4, E)
- end
- return asHEX(H0) .. asHEX(H1) .. asHEX(H2) .. asHEX(H3) .. asHEX(H4)
- end
- end --[[END MAIN SHA1 FUNCTIONS]]--
- end --[[END SHA1 HASHING FUNCTIONS]]--
- local function fColor( _nColor )
- if term.isColor ~= nil then
- if term.isColor() then
- term.setTextColor( _nColor )
- end --if term.isColor()
- end --if term.isColor ~= nil
- end --local function fColor( _nColor )
- term.clear()
- term.setCursorPos( 1, 1 )
- if not fs.isDir( "craftycomputer" ) then
- fs.makeDir( "craftycomputer" )
- end --if not fs.isDir( "craftycomputer" )
- if fs.exists( fs.combine( sDir, "password" ) ) then
- local file = fs.open( fs.combine( sDir, "password" ), "r" )
- sPassword = file.readLine()
- file.close()
- else --fs.exists( fs.combine( sDir, "password" ) )
- fColor( 2 ^ math.random( 1, 14 ) )
- print( "CraftyComputer" )
- local sV0, sV1
- while true do
- term.setCursorPos( 1, 4 )
- term.clearLine()
- term.setCursorPos( 1, 3 )
- term.clearLine()
- fColor( colors.yellow )
- write( "Set The Password: " )
- fColor( colors.white )
- sV0 = read( "*" )
- fColor( colors.yellow )
- write( "Confirm Password: " )
- fColor( colors.white )
- sV1 = read( "*" )
- if sV0 == sV1 then
- break --while true
- end --if sV0 == sV1
- end --while true
- local file = fs.open( fs.combine( sDir, "password" ), "w" )
- sPassword = sha1( sV0 )
- file.writeLine( sPassword )
- file.close()
- end --if fs.exists( fs.combine( sDir, "craftycomputer" ) )
- local oldPullEvent = os.pullEvent
- os.pullEvent = os.pullEventRaw
- term.clear()
- term.setCursorPos( 1, 1 )
- fColor( colors.yellow )
- print( os.version() )
- local function fConsole()
- while true do
- fColor( colors.yellow )
- write( "> " )
- fColor( colors.white )
- local sInput = read( nil, tHistory )
- table.insert( tHistory, sInput )
- if sha1( sInput ) == sPassword then
- return
- end --if sha1( sInput ) == sPassword
- local sInput = string.sub( string.lower( sInput:match( "^%s*(.-)%s*$" ) ), string.find( string.lower( sInput:match( "^%s*(.-)%s*$" ) ), "(%S*)" ) )
- if sInput == "programs" then
- textutils.pagedTabulate( tPrograms )
- elseif sInput == "clear" then
- term.clear()
- term.setCursorPos( 1, 1 )
- elseif sInput == "help" then
- print( "No help available" )
- elseif sInput == "id" then
- print( "This is computer #" .. os.computerID() )
- elseif sInput == "shell" then
- fColor( colors.yellow )
- print( os.version() )
- elseif sInput == "time" then
- print( "The time is " .. textutils.formatTime( os.time(), false ) .. " on Day " .. os.day() )
- elseif sInput == "reboot" or sInput == "shutdown" or sInput == "exit" then
- fColor( colors.yellow )
- print( "Goodbye" )
- fColor( colors.white )
- local foo = sInput ~= "exit" and ( function() sleep( 1 ) return true end )() or os.shutdown()
- local foo = sInput == "reboot" and os.reboot() or os.shutdown()
- elseif sInput ~= "" then
- fColor( colors.red )
- print( "No such program" )
- end --if sInput == "programs"
- end --while true
- end --local function fConsole()
- local function fTerminate()
- os.pullEvent( "terminate" )
- end --local function fTerminate()
- if parallel.waitForAny( fConsole, fTerminate ) == 1 then
- fColor( 2 ^ math.random( 1, 14 ) )
- print( "Exiting CraftyComputer..." )
- os.pullEvent = oldPullEvent
- sleep( 1 )
- term.clear()
- term.setCursorPos( 1, 1 )
- fColor( colors.yellow )
- print( os.version() )
- else --parallel.waitForAny( fConsole, fTerminate ) == 1
- fColor( colors.red )
- print( "Terminated" )
- fColor( colors.white )
- term.setCursorBlink( false )
- print( "Press any key to continue" )
- os.pullEvent = oldPullEvent
- os.pullEvent( "key" )
- os.shutdown()
- end --if parallel.waitForAny( fConsole, fTerminate ) == 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement