Home > Net >  Weird hash output
Weird hash output

Time:04-21

I'm trying to create a hash for files in the directory using this script:

for file in *.zip; do openssl dgst -sha256 -binary ${file%.*}.zip $file | base64 >> ${file%.*}.zip.base64sha256; done

It creates hash like this:

b5iQL1fo5r 6osykGr0mcEZ14Xdbn8y0SrFGIuzMfeRvmJAvV jmv7qh7OUavSZwRnXhd1ufzLRKsUYi7Mx95A==

But for terraform and AWS Lambdas I need a shorted hash value. I can get by using terminal and command like this:

openssl dgst -sha256 -binary archive.zip | base64 >> hash.base64sha256

And output is b5iQL1fo5r 6osykGr0mcEZ14Xdbn8y0SrFGIuzMfeQ=

So the question is: how I can retrieve short version of hash? It's required by terraform and AWS (when hash value is long - lambda are going to redeploy every time)

CodePudding user response:

If you decode the "long" base64 you'll see that it's the same sequence of bytes repeated. That's because here

openssl dgst -sha256 -binary ${file%.*}.zip $file

you are specifying the file twice, once removing the extension and then re-adding it as .zip in ${file%.*}.zip, the other plainly as $file. This results in outputting the concatenated hash for both inputs (that are the same). To fix this, just specify it once:

openssl dgst -sha256 -binary "$file"

(with quotes to avoid problems with whitespace in shell expansion)

CodePudding user response:

Instead of for file in *.zip; do openssl dgst -sha256 -binary ${file%.*}.zip $file | base64 >> ${file%.*}.zip.base64sha256; done

try

for file in *.zip; do openssl dgst -sha256 -binary ${file%.*}.zip | base64 >> ${file%.*}.zip.base64sha256; done

  • Related