pdb

Simple password manager
Log | Files | Refs | README

actions.lua (1693B)


      1 local symmetric = require( "symmetric" )
      2 local arc4 = require( "arc4random" )
      3 
      4 local paths = require( "paths" )
      5 
      6 local _M = { }
      7 
      8 function _M.get( key, path )
      9 	local ciphertext = io.readfile( path )
     10 	if not ciphertext then
     11 		return "No such password."
     12 	end
     13 
     14 	local password, err = symmetric.decrypt( ciphertext, key )
     15 	if not password then
     16 		return err
     17 	end
     18 
     19 	print( password )
     20 end
     21 
     22 function _M.add( key, path )
     23 	if io.readfile( path ) then
     24 		return "That password is already in the DB."
     25 	end
     26 
     27 	io.stdout:write( "Enter a password: " )
     28 	io.stdout:flush()
     29 
     30 	local password = assert( io.stdin:read( "*l" ) ) -- TODO
     31 	local ciphertext = symmetric.encrypt( password, key )
     32 
     33 	local _, err = io.writefile( path, ciphertext )
     34 	return err
     35 end
     36 
     37 function _M.delete( _, path )
     38 	local ok, err = os.remove( path )
     39 	return err
     40 end
     41 
     42 function _M.list()
     43 	local names = { }
     44 	for file in lfs.dir( paths.db ) do
     45 		if file ~= "." and file ~= ".." and not file:match( "^%.git" ) then
     46 			table.insert( names, file )
     47 		end
     48 	end
     49 	table.sort( names )
     50 
     51 	print( table.concat( names, "\n" ) )
     52 end
     53 
     54 function _M.gen( key, path, length, pattern )
     55 	if io.readfile( path ) then
     56 		return "That password is already in the DB."
     57 	end
     58 
     59 	local allowed_chars = { }
     60 	for i = 0, 255 do
     61 		if string.char( i ):match( pattern ) then
     62 			table.insert( allowed_chars, string.char( i ) )
     63 		end
     64 	end
     65 	if #allowed_chars == 0 then
     66 		return "Nothing matches given pattern."
     67 	end
     68 
     69 	local password = ""
     70 	for i = 1, length do
     71 		local c = allowed_chars[ arc4.random( 1, #allowed_chars ) ]
     72 		password = password .. c
     73 	end
     74 	local ciphertext = symmetric.encrypt( password, key )
     75 
     76 	local _, err = io.writefile( path, ciphertext )
     77 	return err
     78 end
     79 
     80 return _M