Bash sha256 not matching erlang one [duplicate] - cryptography

This question already has answers here:
Generating a SHA-256 hash from the Linux command line
(8 answers)
Closed 1 year ago.
Erlang returns me a different result than the bash command when I "sha256".
echo a | sha256sum, returns: 87428fc522803d31065e7bce3cf03fe475096631e5e07bbd7a0fde60c4cf25c7 -
Erlang
Bin = crypto:hash(sha256, "a").
<<202,151,129,18,202,27,189,202,250,194,49,179,154,35,220,
77,167,134,239,248,20,124,78,114,185,128,119,133,175,
238,72,187>>
I tried different bin to hex from bin to hex
None of them gave the result I was expecting.
I got this as a result:
bin_to_hex:bin_to_hex(Bin).
<<"CA978112CA1BBDCAFAC231B39A23DC4DA786EFF8147C4E72B9807785AFEE48BB">>

Your echo a will include a newline. When I add a newline to the erlang version, I get the expected hash:
1> Bin = crypto:hash(sha256, "a\n").
<<135,66,143,197,34,128,61,49,6,94,123,206,60,240,63,228,
117,9,102,49,229,224,123,189,122,15,222,96,196,...>>
2> binary:encode_hex(Bin).
<<"87428FC522803D31065E7BCE3CF03FE475096631E5E07BBD7A0FDE60C4CF25C7">>
You can also tell echo not to use a newline with -n:
$ echo -n a | sha256sum
ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb -

Related

Issues converting a small Hex value to a Binary value

I am trying to take the contents of a file that has a Hex number and convert that number to Binary and output to a file.
This is what I am trying but not getting the binary value:
xxd -r -p Hex.txt > Binary.txt
The contents of Hex.txt is: ff
I have also tried FF and 0xFF, but would like to just use ff since the device I am pulling the info from has it in that format.
Instead of 11111111 which it should be, I get a y with 2 dots above it.
If I change it to ee, I get an i with 2 dots. It seems to be reading it just fine but according to what I have read on the xxd -r -p command, it is not outputing it in the correct format.
The other ways I have found to convert Hex to Binary have either also not worked or is a pretty big Bash script that seems unnecessary to do what I thought would be a simple task.
This also gives me the y with 2 dots.
$ for i in $(cat Hex.txt) ; do printf "\x$i" ; done > Binary.txt
For some reason almost every solution I find gives me this format instead of a human readable Binary value with 1s and 0s.
Any help is appreciated. I am planning on using this in a script to pull the Relay values from Digital Loggers devices using curl and giving Home Assistant a readable file to record the Relay State. Digital Loggers curl cmd gives the state of all 8 relays at once using Hex instead of being able to pull the status of a specific relay.
If "file.txt" contains:
fe
0a
and you run this:
perl -ane 'printf("%08b\n",hex($_))' file.txt
You'll get this:
11111110
00001010
If you use it a lot, you might want to make a bash function of it in your login profile along these lines - being extremely respectful of spaces and semi-colons that might look unnecessary:
bin(){ perl -ane 'printf("%08b\n",hex($_))' $1 ; }
Then you'll be able to do:
bin file.txt
If you dislike Perl for some reason, you can achieve something similar without it as follows:
tr '[:lower:]' '[:upper:]' < file.txt |
while read h ; do
echo "obase=2; ibase=16; $h" | bc
done

How to insert a Line into a file in AIX using sed preferably?

I want to insert a line "new line" into a file "Textfile.txt" at line number 3 in AIX.
Before insertion Textfile.txt looks like
one
two
four
After Insertion Textfile.txt looks like
one
two
new line
four
I have already done it on Linux how ever with AIX I am finding it not working with solution of Linux.
Surprisingly I couldn't find a simple solution for this problem anywhere.
I am using this command in Linux and is working
echo "target_node = ${arr[0]}"
echo "target_file = ${arr[1]}"
echo "target_line = ${arr[2]}"
echo "target_text = ${arr[3]}"
escape "$(ssh -f ${arr[0]} "sed -i "${arr[2]}i$(escape ${arr[3]})" ${arr[1]}; exit")"
To sum the previous bits of information written as comments:
Option -i doesn't exist in AIX!sed, use a temporary file; the syntax of command is more strict than in Linux.
sed '2a\
Insert this after the 2nd line' "$target_file" >"$target_file.tmp"
mv -- "$target_file.tmp" "$target_file"
Hi Thanks for the help,
I created script in such a way that it copies the file to linux update changes and movies to AIX.

Windows command line script has embedded blank character [duplicate]

This question already has answers here:
What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean?
(2 answers)
Closed 4 years ago.
I want to copy a file to another file that contains the time and date
in its name.
I use the statement below but the problem is for time values earlier than 10 AM (for which the hour value is only a single digit) there is a blank character instead of a leading zero, which I want.
copy "M:\Production Schedule.xlsm" m:\gsdBackups\ProductionSchedule%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,0%.xlsm
In the example above when I run it at 9:50 I get the resulting name:
GSDProductionSchedule20180509_ 950.xlsm
I do not understand all of the formatting that is going on in the above copy command. Rather than the " 950" below I'd like to have "0950"
As a recurring task this should be a batch file hiding the details.
use wmic or PowerShell to get date/time in a user settings /locale independent format
name the batch file to any name with the extension .bat or .cmd and place it in a folder which is in the path
:: Q:\Test\2018\05\09\SO_50256566.cmd
#echo off
Set "Src=M:\Production Schedule.xlsm"
Set "Dst=M:\gsdBackups"
:: Get date and time in a user settinhs/locale independent format
For /f %%Y in ('
powershell -NoP -C "(get-date).AddDays(0).ToString('yyyyMMdd_HHmm')"
') Do Set _DT=%%Y
:: get source and use for variable modifiers to get name extension separated
For %%F in ("%Src%") Do echo copy "%%~F" "%Dst%\%%~nF_%_DT%%%~xF"
Sample output:
> SO_50256566.cmd
copy "M:\Production Schedule.xlsm" "M:\gsdBackups\Production Schedule_20180509_1743.xlsm"
If it looks OK to you remove the echo in front of copy.

redirect the sql query will add all the files in that directory [duplicate]

This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 5 years ago.
I have shell script as below
query="SELECT * from [myDB.myTable]"
echo $(date +"%Y-%m-%d %H:%M:%S")'|'land'|'Failure'|||'twice'|'$query > test.log
When I run above in some directory in my Unix PC, the test.log shows the result as below
2017-12-13 06:54:03|land|Failure|||twice| SELECT temp1.txt test tmp.log tt.sh from [myDB.myTable]
Actually I wanted query to be redirected as it is in the log file, instead it printed all the file names of that directory.
How can I fix this issue?
The dreaded pathname expansion strikes again! You can attribute that list of filenames to having a * in an unquoted expression.
echo "$(date +'%Y-%m-%d %H:%M:%S')'|'land'|'Failure'|||'twice'|'$query" > "test.log"
# ^ ^
cat "test.log"
> 2017-12-13 06:54:03|land|Failure|||twice| SELECT * from [myDB.myTable]
Use double quotes for your complete echo, the single quotes can be deleted:
echo "$(date +"%Y-%m-%d %H:%M:%S")|land|Failure|||twice|${query}" > test.log
I also have put curly braves in ${query}, that is not a solution for your problem but a good habit for future scripts.

How to Edit a text from the output in DCL -- OpenVMS scripting

I wrote the below code, which will extract the directory name along with the file name and I will use purge command on that extracted Text.
$ sear VAXMANAGERS_ROOT:[PROC]TEMP.LIS LOG/out=VAXMANAGERS_ROOT:[DEV]FVLIM.TXT
$ OPEN IN VAXMANAGERS_ROOT:[DEV]FVLIM.TXT
$ LOOP:
$ READ/END_OF_FILE=ENDIT IN ABCD
$ GOTO LOOP
$ ENDIT:
$ close in
$ ERROR=F$EXTRACT(0,59,ABCD)
$ sh sym ERROR
$ purge/keep=1 'ERROR'
The output is as follows:
ERROR = "$1$DKC102:[PROD_LIVE.LOG]DP2017_TMP2.LIS;27392 "
Problem here is --- Every time the directory length varies (Length may be 59 or 40 or some other value, but the directory and filename length will not exceed 59 characters in my system). So in the above output, the system is also fetching the Version number of that file number. So I am not able to purge the file along with the version number.
%PURGE-E-PURGEVER, version numbers not permitted
Any suggestion -- How to eliminate the version number from the output ?
I cannot use the exact length of the directory, as directory length varies everytime.... :(
The answer with F$ELEMENT( 0, ";", ABCD ) should work, as confirmed. I might script something like this:
$ ERROR = F$PARSE(";",ERROR) ! will return $1$DKC102:[PROD_LIVE.LOG]DP2017_TMP2.LIS;
$ ERROR = ERROR - ";"
$ PURGE/KEEP=1 'ERROR'
Not sure why you have the read loop. What you will get is the last line in the file, but assuming that's what you want.
While HABO explained it, some more explanations
Suppose I use f$search to check if a file exists
a = f$search("sys$manager:net$server.log")
then I find I it exists
wr sys$output a
shows
SYS$SYSROOT:[SYSMGR]NET$SERVER.LOG;9
From the help of f$parse I get
help lex f$parse arg
shows, among other things
`Specifies a character string containing the name of a field
in a file specification. Specifying the field argument causes
the F$PARSE function to return a specific portion of a file
specification.
Specify one of the following field names (do not abbreviate):
NODE Node name
DEVICE Device name
DIRECTORY Directory name
NAME File name
TYPE File type
VERSION File version number`
So I can do
wr sys$output f$parse(a,,,"DEVICE")
which shows
SYS$SYSROOT:
and also
wr sys$output f$parse(a,,,"DIRECTORY")
so I get
[SYSMGR]
and
wr sys$output f$parse(a,,,"NAME")
shows
NET$SERVER
and
wr sys$output f$parse(a,,,"TYPE")
shows
.LOG
the version is
wr sys$output f$parse(a,,,"VERSION")
shown as
;9
The lexicals functions can be handy, check it using
help lexical
it shows
F$CONTEXT F$CSID F$CUNITS F$CVSI F$CVTIME F$CVUI F$DELTA_TIME F$DEVICE F$DIRECTORY F$EDIT
F$ELEMENT F$ENVIRONMENT F$EXTRACT F$FAO F$FID_TO_NAME F$FILE_ATTRIBUTES F$GETDVI F$GETENV
F$GETJPI F$GETQUI F$GETSYI F$IDENTIFIER F$INTEGER F$LENGTH F$LICENSE F$LOCATE F$MATCH_WILD
F$MESSAGE F$MODE F$MULTIPATH F$PARSE F$PID F$PRIVILEGE F$PROCESS F$READLINK F$SEARCH
F$SETPRV F$STRING F$SYMLINK_ATTRIBUTES F$TIME F$TRNLNM F$TYPE F$UNIQUE F$USER