I don't know if you realize are re-inventing the wheel here,
but if you don't want to do that you can check
Freshmeat
for a few different implementations of this. One that I know
of, written in Perl, is
CPM Cyberlot Password Manager
Even if you don't use it, reading the source code may be helpful
to determine the best approach to the problem.