How do you encode SHA-256 twice? - hash

Very simply, how does one get the "second round" below?
hello
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 (first round of sha-256)
9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50 (second round of sha-256)
I went to an online hash calculator, put in "hello", got the first round, and then took that hash and put it into the calculator and got a different result than the second round. How does it do it twice then?

When you pasted the first hash output back into the hash calculator for a second round, you were hashing it as an ASCII string of letters and numbers, not as the actual sequence of bytes expressed by those hex numbers. In other words, the first byte of the string you plug into the hash calculator needs to actually be 0x2C, not 0x32 (which is the ASCII value of the character '2' at the beginning of "2cf2...")

echo -n hello | openssl dgst -sha256 -binary | openssl dgst -sha256

Related

What does \x do in print

I would like to start by saying that I am not familiar with Perl. That being said, I came across this piece of code and I could not figure out what the \x was for in the code below. In addition, I was unsure why nothing was displayed when I ran the following:
perl -e 'print "\x7c\x8e\x04\x08"'
It's not about print: it's about string representation, in which codes represent characters from your character set. For more information you should read Quote and Quote-like Operators and Effects of Character Semantics
In your case the character code is in hex. You should look in your character set table, and you may need to convert to decimal first.
You said "I was unsure why nothing was displayed when I ran the following:"
perl -e 'print "\x7c\x8e\x04\x08"'
That command outputs 4 characters to STDOUT. Each of the characters is specified in hexadecimal. The "\x7c" part will output the vertical bar character |. The other three characters are control characters, so probably wouldn't produce any visible output. If you redirect output to a file, you will end up with a 4 byte file.
It's possible that you're not seeing the vertical bar character because it's being overwritten by your command prompt. Unlike the shell echo or Python's print, Perl's print function does not automatically append a newline to all output. If you want new lines, you can insert them in the string using \n.
\x signifies the start of a hexadecimal character notation.

Variable substitution in tcsh

I've gone through the manual for the tcsh but still can't figure out how it should work in my case or whether it should work at all. I basically need to extract part of the variable whose value is a six digit number. So I need to drop the first two characters and retrieve the last four.
The example below doesn't work (it would probably work in bash but tcsh HAS to be used):
set VAR1 = value1
set VAR2 = `echo ${VAR1:2}`
echo VAR2
It comes up with error Bad : modifier in $ (2), apparently because it's bash syntax and not understandable by tcsh, but can't figure out how to do it with tcsh arguments.
I'm not sure about using modifiers, but you can slice your string using cut or sed:
set VAR1=abcdef
"cut" characters 3-to-end
echo $VAR1 | cut -c3-
capture everything (\(.*\)), except for the first 2 characters (..)
echo $VAR1 | sed 's/..\(.*\)$/\1/'
You could also use the perl command line with regexs like the sed in #shx2 answer above:
echo $VAR1 | perl -pe 's/^\d\d(.*)/$1/'
Drops the first two digits it starts with.
Certainly tcsh doesn't accept bash-style modifiers, or vice versa. They're very different shells.
You say you need to extract the last 4 digits of a 6-digit number. I'll assume that number is a non-negative integer.
If you think of it as an arithmetic problem rather than as a string-processing problem, you can use the built-in # command:
% set VAR1 = 123456
% # VAR2 = $VAR1 % 10000
% echo VAR1=$VAR1 VAR2=$VAR2
VAR1=123456 VAR2=3456
%
(Why do you have to use tcsh? Obligatory link: http://www.perl.com/doc/FMTEYEWTK/versus/csh.whynot .)

Using hexadecimal values in a Perl variable

I have a variable, $count, which will take a decimal value from the command line, and I have a piece of code which generates files with name filename001 filename002 ....filename00a,filename00b etc. It appends a hexadecimal number(without 0x) to filename for every new file it generates.
I use the variable $count to keep track of the number that is appended to filename. I want to pull out lines containing a particular text Result: from the generated file (filename00b filename00a, etc.). For this purpose I use the grep tool in the following way:
`grep "Result:" path to the file/filename0*$count`
This works fine till I reach the 10th file when 10 becomes a in hexadecimal, but $count in the grep command is simplified to 10, so grep is not able to find the file. I tried using hex($count), but it does not seem to work. It required that $count variable can be incremented or decremented, and it still holds the hex value. Is there a way to do this?
The hex function in perl interprets its argument as a hex string and returns the corresponding integer value, for example:
print hex '0xAf'; # prints '175'
print hex 'aF'; # same
This from the definition of hex in perldoc -f hex.
It seem you want to do the opposite, convert your decimal number to a hex representation. You can do that with printf for example:
printf("%03x", 175); # prints '0af'
printf("%03x", 0xAf); # same
If you want to save the value in a variable, use sprintf instead of printf, it returns the string instead of printing it.

Insert between each occurrence of two characters

If I can have somewhere in my input a series of two or more characters (in my case, >), how can I insert something between each occurrence of >?
For example: >> to >foo>, but also:
>>> to >foo>foo> and:
>>>> to >foo>foo>foo>.
Using 's/>>/>foo>/g' gives me of course >foo>>foo>, which is not what I need.
In other words, how can I push a character back to the pattern space, or match a character without consuming it (does that make any sense?)
Using Perl, you can do it iteratively
$ echo '>>>>' | perl -pe 's/>>/>foo>/ while />>/'
>foo>foo>foo>
or use a look-ahead assertion, which does not consume the 2nd >
$ echo '>>>>' | perl -pe 's/>(?=>)/>foo/g'
>foo>foo>foo>
This should also work
sed ':b; s/>>/>foo>/; tb'

How to convert special ASCII characters to hex in Perl

I have written a serial port program in Perl. Reading the output on STDOUT( screen), I get output as the special ASCII characters: :- Black smiley white heart. How do I convert them back to hex format?
See perldoc -f ord.