views:

190

answers:

3

Hi all! There's a website that generates a password by joining a master password with the website's name (e.g. master password=abc and site=google) then hashing that with SHA1 and then coding the resulting hash with Base64. I've been trying to implement that in Bash, but I can't. I mean I can but my results are extremely different from the ones in the website. Help?

The website that generates the password using JavaScript is at http://angel.net/~nic/passwd.sha1.html

And here is my bash script:

#!/bin/bash

CUT=8

echo -n "Enter your master password. "
read -s MASTER
echo -en "\nEnter the site's name. "
read SITE

PASS=$(echo -n $MASTER$SITE | sha1sum | sed -e 's/[ -]//g' | base64 | cut -b 1-$CUT)

echo $PASS | sed -e 's/[\/+=]//g'

I'm new to Stack Overflow so tell me if I'm breaking any rule, etc...

+1  A: 

For what I can tell by

password.value = b64_hmac_sha1(master.value, site.value).substr(0,8);

It doesn't exactly concatenates and computes SHA1, but rather computes HMAC-SHA1 for key and message.

I do not know any command-line equivalent for HMAC calculation, but if you don't mind invoking perl that would be something like

perl -MDigest::HMAC_SHA1 \
 -e "print Digest::HMAC_SHA1::hmac_sha1('$SITE','$MASTER')" \
| base64 | cut -c1-8
Michael Krelin - hacker
In that case, what's the equivalent of sha1sum for HMAC?
AntonioPT
See updated answer
Michael Krelin - hacker
Thank you! This way I got it working!
AntonioPT
A: 

the script uses the hmac-sha1 algorithm rather than a direct sha1 hash. it then returns only the first 8 characters of the base64-encoded result.

you can find a bash/openssl implementation of hmac-sha1 here or you can call out to php's hash_hmac function.

in php, you can do something like this:

password=`php -r "echo substr(base64_encode(hash_hmac('sha1', '$SITE', '$MASTER', true)), 0, 8);"`
jspcal
The ""bash's implementation of hmac-sha1"" it's a script to access S3 servers (Amazon) thru bash. But thanks anyway.
AntonioPT
No, it does indeed implement it in http://code.google.com/p/s3-bash/source/browse/trunk/s3-common-functions , even though I don't think it's exactly what you're looking for.
Michael Krelin - hacker
you will find the function in that library. it's not only for s3.
jspcal
I'll try to use the function in s3-bash and see if there's any difference with the perl implementation. I prefer using a native bash implementation so I'll maybe use this one.
AntonioPT
No. The code is tooo "optimized" for that specific use, S3. I'll continue using the perl implementation.
AntonioPT
not really, it's pretty generic. if you don't want to use that example, you can use php, perl, etc.
jspcal
+1  A: 

You can use the openssl command to compute an HMAC digest, and to convert to base64, as follows.

echo -n $SITE | openssl dgst -binary -sha1 -hmac $MASTER | openssl base64 | cut -c1-8
Brian Campbell
Yes! I failed to make up this command line after reading `openssl dgst -h` output ;-) +1 Go for this one, AntonioPT
Michael Krelin - hacker
Yeah, it took me a bit to go through all of the iterations of that command to finally get the right result.
Brian Campbell
Persistence is the key ;-) Obviously the first idea I had was to make use of `openssl` binary, but then I fell back to perl.
Michael Krelin - hacker
Thanks! This sounds even better than Perl! :D
AntonioPT
OK, this works, but "it doesn't make sense". Shouldn't this be: openssl dgst -binary -sha1 -hmac $MASTER$SITE | openssl base64 | cut -c1-8
AntonioPT
Sorry, I got it. HMAC is like a key so openssl is hashing MASTER with a key, SITE.
AntonioPT
Actually, it's the other way around. It's hashing SITE with the key MASTER. But yeah, that's pretty much it.
Brian Campbell
I would like to thanks all the geeks (:D) that helped me out. I'm now using this scheme for my passwords!
AntonioPT