PageRenderTime 23ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/utils/cryptostick/pwdtool.vim

https://bitbucket.org/markfink/testing-software
Vim Script | 184 lines | 73 code | 10 blank | 101 comment | 4 complexity | c72df699281421119393bb90bb58128f MD5 | raw file
Possible License(s): LGPL-3.0
  1. " pwdtool.vim ported to vim-python by Mark Fink
  2. " based on openssl.vim from Noah Spurrier
  3. " uses Python + PyCrypto instead of openssl (easier to set up on my environment)
  4. " and I need Python anyway :-))
  5. "
  6. " Adapted from the original documentation:
  7. " openssl.vim version 3.3 2008 Noah Spurrier <noah@noah.org>
  8. "
  9. " == Edit OpenSSL encrypted files and turn Vim into a Password Safe! ==
  10. "
  11. " This plugin enables reading and writing of files encrypted using OpenSSL.
  12. " The file must have the extension of one of the ciphers used by OpenSSL.
  13. " For example:
  14. "
  15. " .des3 .aes .bf .bfa .idea .cast .rc2 .rc4 .rc5 (.bfa is base64 ASCII
  16. " encoded blowfish.)
  17. "
  18. " This will turn off the swap file and the .viminfo log. The `openssl` command
  19. " line tool must be in the path.
  20. "
  21. " == Install ==
  22. "
  23. " Put this in your plugin directory and Vim will automatically load it:
  24. "
  25. " ~/.vim/plugin/pwdtool.vim
  26. "
  27. " You can start by editing an empty unencrypted file. Give it one of the
  28. " extensions above. When you write the file you will be asked to give it a new
  29. " password.
  30. "
  31. " == Simple Vim Password Safe ==
  32. "
  33. " If you edit any file named '.auth.bfa' (that's the full name, not just the
  34. " extension) then this plugin will add folding features and an automatic quit
  35. " timeout.
  36. "
  37. " Vim will quit automatically after 2 minutes of no typing activity (unless
  38. " the file has been changed).
  39. "
  40. " This plugin will fold on wiki-style headlines in the following format:
  41. "
  42. " == This is a headline ==
  43. "
  44. " Any notes under the headline will be inside the fold until the next headline
  45. " is reached. The SPACE key will toggle a fold open and closed. The q key will
  46. " quit Vim. Create the following example file named ~/.auth.bfa:
  47. "
  48. " == Colo server ==
  49. "
  50. " username: maryjane password: esydpm
  51. "
  52. " == Office server ==
  53. "
  54. " username: peter password: 4m4z1ng
  55. "
  56. " Then create this bash alias:
  57. "
  58. " alias auth='view ~/.auth.bfa'
  59. "
  60. " Now you can view your password safe by typing 'auth'. When Vim starts all
  61. " the password information will be hidden under the headlines. To view the
  62. " password information put the cursor on the headline and press SPACE. When
  63. " you write an encrypted file a backup will automatically be made.
  64. "
  65. " This plugin can also make a backup of an encrypted file before writing
  66. " changes. This helps guard against the situation where you may edit a file
  67. " and write changes with the wrong password. You can still go back to the
  68. " previous backup version. The backup file will have the same name as the
  69. " original file with .bak appended. For example:
  70. "
  71. " .auth.bfa --> .auth.bfa.bak
  72. "
  73. " Backups are NOT made by default. To turn on backups put the following global
  74. " definition in your .vimrc file:
  75. "
  76. " let g:openssl_backup = 1
  77. "
  78. " Thanks to Tom Purl for the original des3 tip.
  79. "
  80. " I release all copyright claims. This code is in the public domain.
  81. " Permission is granted to use, copy modify, distribute, and sell this
  82. " software for any purpose. I make no guarantee about the suitability of this
  83. " software for any purpose and I am not liable for any damages resulting from
  84. " its use. Further, I am under no obligation to maintain or extend this
  85. " software. It is provided on an 'as is' basis without any expressed or
  86. " implied warranty.
  87. "
  88. augroup pwdtool
  89. function! s:CryptoReadPre()
  90. set secure
  91. set viminfo=
  92. set clipboard=
  93. set noswapfile
  94. set noshelltemp
  95. set bin
  96. endfunction
  97. function! s:CryptoReadPost()
  98. python << EOF
  99. from Crypto.Cipher import Blowfish
  100. import hashlib, base64
  101. import vim
  102. a = vim.eval('inputsecret("Password: ")')
  103. key = hashlib.sha256(a).digest()
  104. cipher = Blowfish.new(key, Blowfish.MODE_CFB)
  105. text = cipher.decrypt(base64.b64decode('\n'.join(vim.current.buffer)))
  106. #text = '\n'.join(vim.current.buffer)
  107. vim.current.buffer[:] = text.split('\n')
  108. EOF
  109. set nobin
  110. " execute ":doautocmd BufReadPost ".expand("%:r")
  111. redraw!
  112. endfunction
  113. function! s:CryptoWritePre()
  114. set bin
  115. python << EOF
  116. from Crypto.Cipher import Blowfish
  117. import hashlib, base64
  118. import vim
  119. a = vim.eval('inputsecret(" New password: ")')
  120. ac = vim.eval('inputsecret("Retype new password: ")')
  121. if a != ac:
  122. # This gives OpenSSLWritePost something to UNDO..
  123. vim.command('silent! execute "0goto"')
  124. vim.command('silent! execute "normal iThis file has not been saved.\n"')
  125. print "ERROR -- COULD NOT ENCRYPT"
  126. print "The new password and the confirmation password did not match."
  127. print "ERROR -- COULD NOT ENCRYPT"
  128. print "Press any key to continue..."
  129. # vim.command("redraw!")
  130. vim.eval('getchar()')
  131. else:
  132. key = hashlib.sha256(a).digest()
  133. cipher = Blowfish.new(key, Blowfish.MODE_CFB)
  134. text = base64.b64encode(cipher.encrypt('\n'.join(vim.current.buffer)))
  135. #text = '\n'.join(vim.current.buffer)
  136. vim.current.buffer[:] = text.split('\n')
  137. #vim.command("redraw")
  138. EOF
  139. set bin
  140. endfunction
  141. function! s:CryptoWritePost()
  142. " It was necessary to encrypt the buffer for writing
  143. " Undo the encryption.
  144. silent! undo
  145. set nobin
  146. redraw!
  147. endfunction
  148. autocmd BufReadPre,FileReadPre *.bfa call s:CryptoReadPre()
  149. autocmd BufReadPost,FileReadPost *.bfa call s:CryptoReadPost()
  150. autocmd BufWritePre,FileWritePre *.bfa call s:CryptoWritePre()
  151. autocmd BufWritePost,FileWritePost *.bfa call s:CryptoWritePost()
  152. "
  153. " The following implements a simple password safe for any file named
  154. " '.auth.bfa'. The file is encrypted with Blowfish and base64 encoded.
  155. " Folding is supported for == headlines == style lines.
  156. "
  157. function! HeadlineDelimiterExpression(lnum)
  158. if a:lnum == 1
  159. return ">1"
  160. endif
  161. return (getline(a:lnum)=~"^\\s*==.*==\\s*$") ? ">1" : "="
  162. endfunction
  163. autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa set foldexpr=HeadlineDelimiterExpression(v:lnum)
  164. autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa set foldlevel=0
  165. autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa set foldcolumn=0
  166. autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa set foldmethod=expr
  167. autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa set foldtext=getline(v:foldstart)
  168. autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa nnoremap <silent><space> :exe 'silent! normal! za'.(foldlevel('.')?'':'l')<CR>
  169. autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa nnoremap <silent>q :q<CR>
  170. autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa highlight Folded ctermbg=red ctermfg=black
  171. autocmd BufReadPost,FileReadPost,BufWritePost,FileWritePost .auth.bfa set updatetime=300000
  172. autocmd CursorHold .auth.bfa quit
  173. " End of pwdtool
  174. augroup END