命令行下的类似keepass一个东东.
如何使用Command line for Keep Password.
Usage:
keepass.sh -l
keepass.sh -e plain-text
keepass.sh -d encoded-text
keepass.sh -s encoded-text name
-l
展示保存下所有加密后的密码信息
-e
将明文密码, 加密, 密文自动保存到剪切板上
-d
将密文界面, 解密后的明文自动保存到剪切板上
-s
将密文持久化保存到文件中
代码实现
#!/bin/sh
DATA="$HOME/.password/data"
declare -a passwords
function enc()
{
#$1: plain text
echo "$1" | openssl enc -des | base64
}
function dec()
{
#$1: encoded text
echo "$1" | base64 -D | openssl enc -des -d
}
function sync()
{
for ((loop=0;loop<${#passwords[*]};loop++))
do
if [ -z "${passwords[$loop]}" ];then
continue
fi
value="$value\n${passwords[$loop]}"
done
echo $value > $DATA
}
function save()
{
#$1 encoded password
#$2 name
for ((loop=0; loop<${#passwords[*]}; loop++))
do
info=${passwords[$loop]}
password=$(echo $info | awk '{print $1}')
name=$(echo $info | awk '{print $2}')
if [ "$2" == "$name" ];then
passwords[$loop]="$1 $2"
return
fi
done
passwords[$loop]="$1 $2"
sync
}
function delete()
{
#$1: name
for ((loop=0; loop<${#passwords[*]}; loop++))
do
info=${passwords[$loop]}
password=$(echo $info | awk '{print $1}')
name=$(echo $info | awk '{print $2}')
if [ "$1" == "$name" ];then
passwords[$loop]=""
fi
done
sync
}
function list()
{
for ((loop=0;loop<${#passwords[*]};loop++))
do
echo ${passwords[$loop]}
done
}
function help()
{
echo "Command line for Keep Password.
Usage:
keepass.sh -l
keepass.sh -e plain-text
keepass.sh -d encoded-text
keepass.sh -s encoded-text name"
}
function init()
{
loop=0
while read line
do
if [ -z "$line" ];then
continue
fi
passwords[$loop]=$line
loop=$(echo $loop+1 | bc)
done < $DATA
}
init
case "$1" in
-l)
list
;;
-e)
enc "$2" | tr -d '\n' | pbcopy
;;
-d)
dec "$2" | tr -d '\n' | pbcopy
;;
-s)
save "$2" "$3"
;;
-r)
delete "$2"
;;
*)
help
;;
esac
备注:
1. 纯shell打造
2. 目前支持Mac shell, 理论上支持linux系统(唯一的改动, 把pbcopy改成linux下剪切板复制的命令即可)