/utils/cryptostick/pwdtool.vim
Vim Script | 184 lines | 73 code | 10 blank | 101 comment | 4 complexity | c72df699281421119393bb90bb58128f MD5 | raw file
Possible License(s): LGPL-3.0
- " pwdtool.vim ported to vim-python by Mark Fink
- " based on openssl.vim from Noah Spurrier
- " uses Python + PyCrypto instead of openssl (easier to set up on my environment)
- " and I need Python anyway :-))
- "
- " Adapted from the original documentation:
- " openssl.vim version 3.3 2008 Noah Spurrier <noah@noah.org>
- "
- " == Edit OpenSSL encrypted files and turn Vim into a Password Safe! ==
- "
- " This plugin enables reading and writing of files encrypted using OpenSSL.
- " The file must have the extension of one of the ciphers used by OpenSSL.
- " For example:
- "
- " .des3 .aes .bf .bfa .idea .cast .rc2 .rc4 .rc5 (.bfa is base64 ASCII
- " encoded blowfish.)
- "
- " This will turn off the swap file and the .viminfo log. The `openssl` command
- " line tool must be in the path.
- "
- " == Install ==
- "
- " Put this in your plugin directory and Vim will automatically load it:
- "
- " ~/.vim/plugin/pwdtool.vim
- "
- " You can start by editing an empty unencrypted file. Give it one of the
- " extensions above. When you write the file you will be asked to give it a new
- " password.
- "
- " == Simple Vim Password Safe ==
- "
- " If you edit any file named '.auth.bfa' (that's the full name, not just the
- " extension) then this plugin will add folding features and an automatic quit
- " timeout.
- "
- " Vim will quit automatically after 2 minutes of no typing activity (unless
- " the file has been changed).
- "
- " This plugin will fold on wiki-style headlines in the following format:
- "
- " == This is a headline ==
- "
- " Any notes under the headline will be inside the fold until the next headline
- " is reached. The SPACE key will toggle a fold open and closed. The q key will
- " quit Vim. Create the following example file named ~/.auth.bfa:
- "
- " == Colo server ==
- "
- " username: maryjane password: esydpm
- "
- " == Office server ==
- "
- " username: peter password: 4m4z1ng
- "
- " Then create this bash alias:
- "
- " alias auth='view ~/.auth.bfa'
- "
- " Now you can view your password safe by typing 'auth'. When Vim starts all
- " the password information will be hidden under the headlines. To view the
- " password information put the cursor on the headline and press SPACE. When
- " you write an encrypted file a backup will automatically be made.
- "
- " This plugin can also make a backup of an encrypted file before writing
- " changes. This helps guard against the situation where you may edit a file
- " and write changes with the wrong password. You can still go back to the
- " previous backup version. The backup file will have the same name as the
- " original file with .bak appended. For example:
- "
- " .auth.bfa --> .auth.bfa.bak
- "
- " Backups are NOT made by default. To turn on backups put the following global
- " definition in your .vimrc file:
- "
- " let g:openssl_backup = 1
- "
- " Thanks to Tom Purl for the original des3 tip.
- "
- " I release all copyright claims. This code is in the public domain.
- " Permission is granted to use, copy modify, distribute, and sell this
- " software for any purpose. I make no guarantee about the suitability of this
- " software for any purpose and I am not liable for any damages resulting from
- " its use. Further, I am under no obligation to maintain or extend this
- " software. It is provided on an 'as is' basis without any expressed or
- " implied warranty.
- "
- augroup pwdtool
- function! s:CryptoReadPre()
- set secure
- set viminfo=
- set clipboard=
- set noswapfile
- set noshelltemp
- set bin
- endfunction
- function! s:CryptoReadPost()
- python << EOF
- from Crypto.Cipher import Blowfish
- import hashlib, base64
- import vim
- a = vim.eval('inputsecret("Password: ")')
- key = hashlib.sha256(a).digest()
- cipher = Blowfish.new(key, Blowfish.MODE_CFB)
- text = cipher.decrypt(base64.b64decode('\n'.join(vim.current.buffer)))
- #text = '\n'.join(vim.current.buffer)
- vim.current.buffer[:] = text.split('\n')
- EOF
- set nobin
- " execute ":doautocmd BufReadPost ".expand("%:r")
- redraw!
- endfunction
- function! s:CryptoWritePre()
- set bin
- python << EOF
- from Crypto.Cipher import Blowfish
- import hashlib, base64
- import vim
- a = vim.eval('inputsecret(" New password: ")')
- ac = vim.eval('inputsecret("Retype new password: ")')
- if a != ac:
- # This gives OpenSSLWritePost something to UNDO..
- vim.command('silent! execute "0goto"')
- vim.command('silent! execute "normal iThis file has not been saved.\n"')
-
- print "ERROR -- COULD NOT ENCRYPT"
- print "The new password and the confirmation password did not match."
- print "ERROR -- COULD NOT ENCRYPT"
- print "Press any key to continue..."
- # vim.command("redraw!")
- vim.eval('getchar()')
- else:
- key = hashlib.sha256(a).digest()
- cipher = Blowfish.new(key, Blowfish.MODE_CFB)
- text = base64.b64encode(cipher.encrypt('\n'.join(vim.current.buffer)))
- #text = '\n'.join(vim.current.buffer)
- vim.current.buffer[:] = text.split('\n')
- #vim.command("redraw")
- EOF
- set bin
- endfunction
- function! s:CryptoWritePost()
- " It was necessary to encrypt the buffer for writing
- " Undo the encryption.
- silent! undo
- set nobin
- redraw!
- endfunction
- autocmd BufReadPre,FileReadPre *.bfa call s:CryptoReadPre()
- autocmd BufReadPost,FileReadPost *.bfa call s:CryptoReadPost()
- autocmd BufWritePre,FileWritePre *.bfa call s:CryptoWritePre()
- autocmd BufWritePost,FileWritePost *.bfa call s:CryptoWritePost()
- "
- " The following implements a simple password safe for any file named
- " '.auth.bfa'. The file is encrypted with Blowfish and base64 encoded.
- " Folding is supported for == headlines == style lines.
- "
- function! HeadlineDelimiterExpression(lnum)
- if a:lnum == 1
- return ">1"
- endif
- return (getline(a:lnum)=~"^\\s*==.*==\\s*$") ? ">1" : "="
- endfunction
- autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa set foldexpr=HeadlineDelimiterExpression(v:lnum)
- autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa set foldlevel=0
- autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa set foldcolumn=0
- autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa set foldmethod=expr
- autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa set foldtext=getline(v:foldstart)
- autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa nnoremap <silent><space> :exe 'silent! normal! za'.(foldlevel('.')?'':'l')<CR>
- autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa nnoremap <silent>q :q<CR>
- autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa highlight Folded ctermbg=red ctermfg=black
- autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa set updatetime=300000
- autocmd CursorHold .auth.bfa quit
- " End of pwdtool
- augroup END