终端命令对字符串进行sha1、md5、base64、urlencode/urldecode

sha1、md5、base64

mac

$ echo -n foo|shasum
0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33  -
$ echo -n foo|shasum -a 256
2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae  -
$ echo -n foo|openssl dgst -sha1
0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
$ echo -n foo|openssl dgst -sha256
2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae
$ echo -n foo|md5
acbd18db4cc2f85cedef654fccc4a4d8
$ md5 -s foo
MD5 ("foo") = acbd18db4cc2f85cedef654fccc4a4d8
$ echo -n foo|openssl dgst -md5
acbd18db4cc2f85cedef654fccc4a4d8
$ echo -n foo|base64
Zm9v
$ echo Zm9v|base64 -D;echo
foo
$ echo -n foo|openssl enc -base64
Zm9v
$ echo Zm9v|openssl enc -base64 -d;echo
foo
$ echo -n foo|openssl dgst -md5 -binary|openssl enc -base64
rL0Y20zC+Fzt72VPzMSk2A==

openssl命令可以简单写:

openssl sha1
openssl sha -sha256
openssl md5
openssl base64
openssl base64 -d

 

linux

# echo -n foo|shasum
0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33  -
# echo -n foo|shasum -a 256
2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae  -
# echo -n foo|openssl dgst -sha1
(stdin)= 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
# echo -n foo|openssl dgst -sha256
(stdin)= 2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae
# echo -n foo|md5sum
acbd18db4cc2f85cedef654fccc4a4d8  -
# echo -n foo|openssl dgst -md5
(stdin)= acbd18db4cc2f85cedef654fccc4a4d8
# echo -n foo|base64
Zm9v
# echo Zm9v|base64 -d;echo
foo
# echo -n foo|openssl enc -base64
Zm9v
# echo Zm9v|openssl enc -base64 -d;echo
foo
# echo -n foo|openssl dgst -md5 -binary|openssl enc -base64
rL0Y20zC+Fzt72VPzMSk2A==

openssl命令可以简单写:

openssl sha1
openssl sha256
openssl sha sha256
openssl md5
openssl base64
openssl base64 -d

 

 

urlencode/urldecode[1]

$ alias urldecode='python -c "import sys, urllib as ul; \
    print ul.unquote_plus(sys.argv[1])"'

$ alias urlencode='python -c "import sys, urllib as ul; \
    print ul.quote_plus(sys.argv[1])"'

$ urlencode 'lalala啦啦啦'
lalala%E5%95%A6%E5%95%A6%E5%95%A6

$ urldecode 'alala%E5%95%A6%E5%95%A6%E5%95%A6'
alala啦啦啦

可以将alias个语句放到.bashrc或者其他shell的启动rc文件里(如:.zshrc),用source载入。

已安装的shell和当前shell:

$ cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

$ echo $0
-/bin/zsh
$ echo $SHELL
/bin/zsh
$ env|grep SHELL
SHELL=/bin/zsh

引用:

[1] Decoding URL encoding (percent encoding)

 

posted @ 2016-04-12 17:48  Bob-wei  阅读(6625)  评论(0编辑  收藏  举报