lebalusch

MkUser_CCraft

Nov 19th, 2016
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.72 KB | None | 0 0
  1. --[[
  2. -- Program: MkUser
  3. -- Version: 0.1
  4. -- Author:  LebaLusch
  5. -- Created: 19/11/16 -uk date style
  6.  
  7. -- Dependencies:
  8. api=sha sha256.lua made by MaHuJa  https://github.com/MaHuJa/CC-scripts/blob/master/sha256.lua (From http://pastebin.com/gsFrNjbt)
  9.  
  10. -- Use: This will Check a password file (if not Present it will create it) for a user Name if not present it will store the name and Password.  The password will be Hashed and Salted.
  11.  
  12. -- Features i want to add:
  13.   Removal of users.  
  14.   Auto Add a Admin user.
  15.  
  16. -- Credits:
  17.  
  18. All those that have Created and contributed to making Lua what it is today.
  19.  
  20. This is my variation of CompuTech's user maker Whos code can be found at page (How to Securely Store Passwords).
  21. This can be found at http://www.computercraft.info/forums2/index.php?/topic/27496-how-to-securely-store-passwords/page__p__260768#entry260768
  22.  
  23. --Links:
  24.  
  25.  http://www.lua.org/
  26.  http://www.computercraft.info/
  27.  http://www.computercraft.info/forum2
  28.  http://www.minecraft.net
  29. ]]
  30.  
  31. --[[Licence]]
  32. --[[
  33. THE SOFTWARE IS PROVIDED "AS IS",
  34. WITHOUT WARRANTY OF ANY KIND,
  35. EXPRESS OR IMPLIED,
  36. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  37. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  38. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  39. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  40. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  41.  
  42. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
  43. IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
  44. ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  45.  
  46. YOU MAY NOT USE ANY PART OF THIS SOFTWARE UNLESS YOU CREDIT THE AUTHOR FOUND ABOVE IN YOUR HEADER.
  47. YOU MAY NOT USE ANY PART OF THIS SOFTWARE IN A COMERCIAL PRODUCT FOR PROFIT,
  48. OR SOFTWARE THAT DOES NOT ALLOW THE USER FULL ACCESS TO THE CODE.
  49.  
  50. ]]
  51.  
  52.  
  53. --Api's to load
  54.     os.loadAPI("sha")
  55.  
  56. local fileName = "PSW" --name of password file.
  57.  
  58. --FUNCTIONS
  59.  
  60.    
  61.         local function setScreen()-- works, This clears the screen and positions us in the top left of the terminal. This needs to be a global function.
  62.             term.clear() -- set screen up
  63.             term.setCursorPos(1,1)
  64.             term.setCursorBlink(true)          
  65.         end--close function
  66.        
  67.  
  68.         local function createFile ( )--This creates The password file if it doesnt exsit.
  69.             setScreen()
  70.             write(">Creating User-\n\n")
  71.             if not fs.exists(fileName) then --Create a password file
  72.            
  73.                 local f = fs.open(fileName, "w")  --This creates the file in a writable state as a veriableM
  74.                 f.write(textutils.serialize({ })) --This puts the text we want in the file
  75.                 f.close() --This closes the file
  76.                
  77.             else
  78.                 --nothing
  79.             end
  80.         end
  81.  
  82.        
  83.         local function readFile()
  84.             local f = fs.open(fileName,"r")
  85.             local usrs = textutils.unserialize(f.readAll())
  86.             f.close()-- we have now checked the file to see whos in it read and stored all the names and passwords in memorry.         
  87.         end
  88.        
  89.        
  90.             function userName()
  91.             setScreen()
  92.             write(">Creating User-\n\n")
  93.             write(">User Name: ") -- ask for username
  94.             local u = read()--read the user name response, comit to variable u
  95.            
  96.             local f = fs.open(fileName,"r")
  97.             local usrs = textutils.unserialize(f.readAll())
  98.             f.close()
  99.            
  100.             if not usrs [u] then -- we reconise its a new user and carry on and create the password .usrs
  101.                
  102.                
  103.                 write(">Password: ") --ask for user password
  104.                  p = read("*")--read the user password response, comit to variable p
  105.                 write(">Password: ") --ask for user password again and check them agains each other
  106.             local chkP = read("*")
  107.        
  108.             if p == chkP then
  109.                 write("\n>Passwords Match\n")
  110.                 os.sleep(3)
  111.        
  112.             else
  113.                 write(">Passwords Do Not Match\n")
  114.                 os.sleep(3)
  115.                 userName()
  116.             end
  117.        
  118.                
  119.                 local salt = os.time() *math.random(1,10000000000000)*math.random(1,10000000000000)
  120.                 *math.random (1,10000000000000) -- massive over kill to get a really big random number i know
  121.                
  122.                 usrs[u] = {
  123.                     pwd = sha.sha256( p .. salt ),
  124.                     salt = salt,
  125.                  }
  126.                 local f = fs.open(fileName, "w")
  127.                  f.write( textutils.serialize( usrs ) )
  128.                 f.close()
  129.                
  130.                
  131.                 write(">User created\n")
  132.                 os.sleep(3)
  133.                 setScreen()
  134.             else
  135.                 setScreen()
  136.                 write(">User Name already taken\n")
  137.                 write(">Please Try again")
  138.                 os.sleep(3)
  139.                 userName()
  140.             end
  141.         end
  142.  
  143. --END OF FUNCTIONS
  144.  
  145. --CODE
  146.  
  147.  
  148. createFile(fileName) --call function and pass file name  PSW into it.
  149.  
  150. readFile(fileName) -- Read the File PSW
  151.  
  152. userName() -- take the user name and check it and also check the password with each other.
  153.  
  154. write("\n\n--program finished--\n") --place card holder
  155.  
  156. -- END OF CODE
Advertisement
Add Comment
Please, Sign In to add comment