[英]Bash script that prints out contents of a binary file, one word at a time, without xxd
[英]bash - hash binary contents of variable without creating a file
我試圖獲取存儲在從curl語句創建的變量中的內容的哈希,而沒有將curl輸出到文件中。
基本上我想避免:
curl website.com/file.exe >> tempfile.bin
md5sum tempfile.bin
上面提供了正確的md5哈希。 當測試純文本文件時,我的方法(下面)似乎可行,但是當我嘗試下載二進制文件並將其保存到變量時,哈希與將其保存到文件時不同。
我的嘗試:
binary=$(curl website.com/file.exe)
echo $binary | md5sum
我認為我可能缺少標志,或者回聲可能不是最好的方法。 挑戰的重要部分不是將文件寫入磁盤,而是實現與將其寫入磁盤相同的md5哈希。
要也跳過使用臨時變量的步驟,可以使用進程替換:
md5sum <(curl website.com/file.exe)
或直接通過管道傳遞到md5sum
:
curl website.com/file.exe | md5sum
正如您所經歷的那樣,bash shell無法很好地處理原始二進制數據。 為了實現您的目標,您需要在將文件內容讀入bash變量時將其編碼為文本格式,並在寫入文件時對其進行解碼。
例如,如果您擁有base64工具,則可以使用它重新實現示例,如下所示:
encoded=$(curl website.com/file.exe | base64)
echo "$encoded" | base64 --decode | md5sum
如果以后要將數據保存到名為$ output的文件中,則可以這樣操作:
echo "$encoded" | base64 --decode -o "$output"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.