我正在創建一個函式,該函式將接受輸入并確定該值是否為某種型別的哈希編碼(md5、sha1、sha256 和 sha512)。我問了幾個同學,從邏輯上講是有道理的,但顯然有些不對勁。
#!/usr/bin/bash
function identify-hash() {
encryptinput=$(echo $1 | grep -E -i '^[a-z0-9=] ${32}')
if [[ -n $encryptinput ]]; then
echo "The $1 is a valid md5sum string"
exit
else
encryptinput=$(echo $1 | grep -E -i '^[a-z0-9=] ${40}')
if [[ -n $encryptinput ]]; then
echo "The $1 is a valid sha1sum string"
exit
else
encryptinput=$(echo $1 | grep -E -i '^[a-z0-9=] ${64}')
if [[ -n $encryptinput ]]; then
echo "The $1 is a valid sha256sum string"
exit
else
encryptinput=$(echo $1 | grep -E -i '^[a-z0-9=] ${128}')
if [[ -n $encryptinput ]]; then
echo "The $1 is a valid sha512sum string"
exit
else
echo "Unable to determine the hash function used to generate the input"
fi
fi
fi
fi
}
identify-hash $1
我知道散列有特定數量的字符,但我不知道為什么它不起作用。從第4行中洗掉{32}允許它作為 md5sum 回答,但它假設一切都是 md5sum。
建議?
uj5u.com熱心網友回復:
修復了你的腳本。我建議您如果使用ShellCheck就會發現大部分問題:
#!/usr/bin/env bash
identify_hash() {
# local variables
local -- encrypt_input
local -- sumname
# Regex capture the hexadecimal digits
if [[ "$1" =~ ([[:xdigit:]] ) ]]; then
encrypt_input="${BASH_REMATCH[1]}"
else
encrypt_input=''
fi
# Determine name of sum algorithm based on length of encrypt_input
case "${#encrypt_input}" in
32) sumname=md5sum ;;
40) sumname=sha1sum ;;
64) sumname=sha256sum ;;
128) sumname=sha512sum ;;
*) sumname=;;
esac
# If sum algorithm name found (sumname is not empty)
if [ -n "$sumname" ]; then
printf 'The %s is a valid %s string\n' "$encrypt_input" "$sumname"
else
printf 'Unable to determine the hash function used to generate the input\n' >&2
exit 1
fi
}
identify_hash "$1"
uj5u.com熱心網友回復:
進一步解釋@Gordon Davissons 的評論以及為任何路過的人提供的一些基礎知識
注意此答案非常簡化,僅適用于當前問題。這是我更喜歡的正則運算式指南
正則運算式基礎
^-一行的開始$-一行的結尾[...]-可能的字符 串列- 有特制的醬汁
a-z= 所有小寫(英文)字母;0-9= 所有數字;等等。- 也接受字符類-如
[:xdigit:]為十六進制字符- 運算式現在是
[[:xdigit:]]- 即[:class:]在里面[...]
- 運算式現在是
{...}-前面的運算式應該被匹配的次數^[a]{1}$將匹配a但不匹配aa^f[o]{2}d$將匹配food但不匹配fod, foood, fooo*d^[a-z]{4}$會匹配ball?? 但不是buffalo?cove?? 但不是cover?- 基本上任何線(因為
^...$)含有串的恰好4(英國)字母字符
{1,5}- 至少1和最多5*-{0,}表示 0 次或任意次數的簡寫-{1,}表示至少為 1 的簡寫;但沒有上限?- 簡寫{1}
因此,${32}尋找32“行結束”\n的行話,你需要的是[a-z0-9=]{32}代替
但是正如Andrej Podzimek 在評論中指出的那樣,您只需要匹配[0-9a-f]與[:xdigit:]. 兩者都可以使用。
聚苯乙烯
更多基礎知識
.(句號/句點)匹配任何字符,包括空格和特殊字符(...)是匹配模式
[a-z ]*(chicken).*
將匹配任何從chicken coop到chicken soup和please pass that chicken cookbook, Alex?
[.]表示句點/句號不是任何字符- 注意后面的空格
z是為了使空格(ascii 32)成為可能的字符 - 并且
.不區分大小寫
PPS如果是用于家庭作業/作業/學校作業,請在您的問題中說明:)
uj5u.com熱心網友回復:
更短的東西,使用 猛擊:
checkHash() {
local -ar sumnames=([32]=md5sum [40]=sha1sum [64]=sha256sum [128]=sha512sum)
[[ "$1" =~ [[:xdigit:]]{32,129} ]]
echo "${sumnames[${#BASH_REMATCH}] String $BASH_REMATCH could be }${sumnames[
${#BASH_REMATCH}]:-No hash tool match this string.}"
}
這將從任何完整行中提取 [:xdigit:]部分:
checkHash 'Filename: 13aba32dbe4db7a7117ed40a25c29fa8 --'
String 13aba32dbe4db7a7117ed40a25c29fa8 could be md5sum
checkHash a32dba32dbe4db7a7117ed40a25c29fa8e4db7a7117ed40a25c29fa8
No hash tool match this string.
checkHash a32dba32dbe4db7a7117ed40a25c29fa8e4db7a7117ed40a25c29fa8da921adb
String a32dba32dbe4db7a7117ed40a25c29fa8e4db7a7117ed40a25c29fa8da921adb could be sha256sum
... 然后 ${var return this only if $var exist}
... 和 ${var:-return this if $var is empty}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/326777.html
上一篇:如何復制不同目錄中不同檔案的特定部分并將它們保存以在R中打開?
下一篇:檢查字串中的空格和np.NaN
