1

I need to run a code like this

dk=$(python -c "import hashlib; hashlib.sha256('$1' + 'salt').hexdigest()");
echo $dk

But it's like the '$1' is not working using 'python -c'. How to do ? :)

2
  • 2
    What do you mean by "is not working"? What results do you get? What result would you expect? Commented Aug 22, 2015 at 1:50
  • I added a echo $1 before the python and the output is : "lol" ( I chosen "lol" as password ). What I call "is not working" is that I expected that bash take my argument, then use it in the python function. The function make a sha256 salted password using a salt. But the actual output is a blank line. Commented Aug 22, 2015 at 2:00

1 Answer 1

3

Try adding a print to print the hex digest to stdout:

import hashlib; print hashlib.sha256('$1' + 'salt').hexdigest()

So the shell command becomes:

$ dk=$(python -c "import hashlib; print hashlib.sha256('$1' + 'salt').hexdigest()")
$ echo $dk
532b86a29eae4a625bac7c2f0208a58b5ff08e65780917d54e027d927bc92381

Note that this uses the value of $1 in the parent's environment. If you wanted to pass a value as a command line argument you could modify the command to:

$ dk=$(python -c "import sys; import hashlib; print hashlib.sha256(sys.argv[1] + 'salt').hexdigest()" argument)
$ echo $dk
7353bc6a6fcee8b3c908bdaed02b49e6140818a0dcaf37507451b23ae6952687
Sign up to request clarification or add additional context in comments.

1 Comment

Great ! Thanks ! Exactly what I wanted :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.