[Linux]md5加密字符串 和 urlencode操作

  • 在cli中对字符串进行md5加密
#!/bin/bash 
echo  -n  $1  | md5sum  |  awk  '{print $1}'
  • 在bash中进行urlencode,推荐
function encodeurl()
{
    encoded_str=`echo "$*" | awk 'BEGIN {
        split ("1 2 3 4 5 6 7 8 9 A B C D E F", hextab, " ")
        hextab [0] = 0
        for (i=1; i<=255; ++i) {
            ord [ sprintf ("%c", i) "" ] = i + 0
        }
    }
    {
        encoded = ""
        for (i=1; i<=length($0); ++i) {
            c = substr ($0, i, 1)
            if ( c ~ /[a-zA-Z0-9.-]/ ) {
                encoded = encoded c             # safe character
            } else if ( c == " " ) {
                encoded = encoded "+"   # special handling
            } else {
                # unsafe character, encode it as a two-digit hex-number
                lo = ord [c] % 16
                hi = int (ord [c] / 16);
                encoded = encoded "%" hextab [hi] hextab [lo]
            }
        }
        print encoded
    }' 2>/dev/null`
}

encodeurl $pa
p=$encoded_str

以下两种可能有时候会出现BUG 不推荐

  • 在cli中进行urlencode编码
echo '加密' | tr -d '\n' | xxd -plain | sed 's/\(..\)/%\1/g'  
  • 解码
printf $(echo -n "%e6%89%8b%e6%9c%ba" | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g')

你可能感兴趣的:([Linux]md5加密字符串 和 urlencode操作)