2

I try to do something like this directly on my console as some testruns :

It does not seem to work.. any idea what is the mistake I am doing

salt="3245678906789045689"
password="12321312231"
blub=`sha1($salt.$password)`
-bash: command substitution: line 1: syntax error near unexpected token `$salt.$password'
-bash: command substitution: line 1: `sha1($salt.$password)'

It throws out an errors this is what I intend to do at the end:

echo $blub

Can some one please helpout as to what is the error I am doing?

Gumbo
  • 643,351
  • 109
  • 780
  • 844
user1524529
  • 897
  • 3
  • 10
  • 12
  • 1
    I would guess that you have an syntax error near `($salt.$password)`. Did you want `sha1 "$salt.$password"` – msw Jul 29 '12 at 11:47
  • yes. That's is what im trying to achieve. – user1524529 Jul 29 '12 at 11:51
  • if you want to concatenate `$salt` and `$password` do `"$salt$password"`. if `sha1` is a function or another bash-script call it `sha1 "$salt$password"` – tzelleke Jul 29 '12 at 12:20
  • @TheodrosZelleke: Can I generate a sha1 with the concatenated string of $salt$password?. – user1524529 Jul 29 '12 at 12:23
  • dont know how `sha1` works. if its meant to be two separate arguments to the function (make probably more sense) then call it `sha1 $salt $password` – tzelleke Jul 29 '12 at 12:25
  • `sha1 "$salt$pass" No command 'sha1' found, did you mean: Command 'shar' from package 'sharutils' (main) sha1: command not found` That's the error that is being thrown – user1524529 Jul 29 '12 at 12:28

2 Answers2

11

Probably you want to use SHA1 from the OpenSSL package. This should be already installed on your system.

echo -n "$salt$password" | openssl dgst -sha1
(stdin)= a1b2ce5a82e18f454db6b2d6ee82533914f90337

To capture just the sha1-digest:

blub=`echo -n "$salt$password" | openssl dgst -sha1 |awk '{print $NF}'`
echo $blub
a1b2ce5a82e18f454db6b2d6ee82533914f90337

I assume you copied your code from PHP. There functions are called with brackets and the .-Operator concatenates strings. In that interpretation my code is the exact equivalent of your code in BASH.

tzelleke
  • 15,023
  • 5
  • 33
  • 49
1

I didn't came across a lot of explanations on how to easily do a SHA checksum in bash and put it in a variable in the right way, at the same time we should not use sha1 anymore, but something more difficult to bruteforce, like sha512:

dash=`echo -n $password$salt|sha512sum` ##gives hash with trailing dash
read hash rest <<< "$dash" ##removes trailing dash and gives $hash
echo $hash
6555b9d2331203634664f48f604b59372b32badbc115ec6b2586890ef4d3a6b6816d84cc424cdfc538f6a7ccf664c90caeeb942dbf7953051bb1d7414a191c51
  • 1
    Welcome to SO. Consider adding more details to this answer and explain what the OP was doing wrong and why your solution works. – Matt Mar 27 '15 at 17:40