Create a file containing a string and no CR or LF in a batch script
I am trying to generate sha256 hash of a string in a batch script.
For this purpose I found sha256sum.exe (link) this application accepts file arguments only.
Thus I need to create a file with the string, and no trailing space nor CR or LF.
in my batch script I开发者_Go百科 did:
echo.|set /P =%var%>temp.txt
sha256sum temp.txt > temp2.txt
I used the first line to remove CR & LF however in temp.txt file I see a single trailing space char, which is then used by sha256sum.exe.
I am trying to get rid of that trailing space character at the end of the first line of the text file.
I am a linux user and I wasn't expecting such a simple thing to be a problem. Thanks in advance.
Simplest thing would be to replace all whitespace;
set h=0x0C29A1BCD3DC8425712A0107B6693361CCBBABAC
echo."%h%"
set h=%h: =%
echo."%h%"
>> "0x0C29A1BCD3DC8425712A0107B6693361CCBBABAC "
>> "0x0C29A1BCD3DC8425712A0107B6693361CCBBABAC"
Edit
Sorry, I didnt realise the extra space was in the written value, how about
@echo off
set var=hashme
for %%a in (%var%) do (
echo/|set /p ="%%a"
)>temp.txt
FOR /F "tokens=1" %%i in ('sha1sum temp.txt') do SET var=%%i
echo "%var:~0,-1%"
I downloaded that sha1sum.exe and for "hashme"
the output is correct;
"fb78992e561929a6967d5328f49413fa99048d06"
You can do:
set /p =%var%>temp.txt <NUL
instead of:
echo.|set /P =%var%>temp.txt
精彩评论