1

I used python to calculate sha sum of a string but output is different by two different programs.

Here is what I did:

~/$ echo "Tuwile@com.termux" | sha256sum
cc85269412339c15a992fb8766f5d1d6e0c2f0fbab990d4fc569d747503d98a2  -
~/$ python                                                                                          
Python 3.9.1 (default, Jan  8 2021, 21:18:33)
[Clang 9.0.8 (https://android.googlesource.com/toolchain/llvm-project 98c855489 on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
>>> r=hashlib.sha256("Tuwile@com.termux".encode())
>>> print(r.hexdigest())
40445c42210abea6b0e681b29d15b163692826128c369834ab3b9e63ac8f6527
>>>
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
MrAdityaAlok
  • 95
  • 1
  • 6

1 Answers1

4

By default, echo adds the end of line \n to the output, and it becomes the part of the sha256sum input. Add the -n flag to suppress it:

>>> echo -n "Tuwile@com.termux" | sha256sum
40445c42210abea6b0e681b29d15b163692826128c369834ab3b9e63ac8f6527 -
wjandrea
  • 28,235
  • 9
  • 60
  • 81
bereal
  • 32,519
  • 6
  • 58
  • 104