Standard hashing algorithm for Bash script and Javascript?


I have a directory in my mac which has files with following names -

Directory images -

I would like to write a bash script to rename all the files in this folder, by using some standard hashing algorithm, so that the directory now looks something like this -

Lets assume that the hashing key is common_key = "mykey123"

Directory images -

How can I achieve this ?

Furthur, I am writing some code in Nodejs (basically javascript), where I wish to pick a random image out of these four and display it, so I am using-

var random_image_number = Math.floor((Math.random()*4)+1); // Picking a random number out of 4

Suppose random_image comes out to be 3. Now I need to display image number 3. If I had not hashed the image names, I would normally do this -

<img src="/images/" + random_image_number + ".jpg" />

But now I need to use the standard encryption algorithm again on the random number that I have generated, before I can display the image. (I keep stressing on "standard" since the same hashing algorithm has to work on JS side as well as in the bash script and generate the same result)

so i need some thing like this

var random_image_number = Math.floor((Math.random()*4)+1); // Picking a random number out of 4
var random_image_number_hashed = some_hashing_function(common_key, random_image_number)
<img src="/images/" + random_image_number_hashed + ".jpg" />

Please tell me what algorithm I could use. It doesn't need to be much sophisticated, but must give the same result in the bash script as well as javascript side.

Also, I am not sure how to write the bash script for renaming all the files using the hashing algorithm so any code regarding that will be really useful. Thank you.


You'll probably want to look at aes256 as your algorithm for this.

On the command line you can use the openssl utility to encrypt your file names:

echo 'filename_to_encrypt' | openssl aes-256-cbc -a

And then in node you can use the crypto module to decrypt:

var crypto = require('crypto');

var algorithm = 'aes256'; // or any other algorithm supported by OpenSSL
var key = 'password';
var text = 'encrypted_filename';

var decipher = crypto.createDecipher(algorithm, key);
var decrypted = decipher.update(text, 'hex', 'utf8')'utf8');

If you just want to do hashing then you can use bcrypt. bcrypt is a utility that works in bash and there are node.js modules for it as well.

By : Bill

Although I accepted the above answer, I eventually ended up using node.js module known as crypto for hashing. Also, I had to tweak the bash script a bit to make it work.

So this is what I finally used -

Bash Script (run in images folder) -

for file in `/bin/ls *.jpg`; do
    hashedFileName=`echo -n ${file%.*} | openssl dgst -sha1 -hmac "myKey123"`;
    mv $file ${hashedFileName##* }.jpg;

NodeJs -

var crypto = require('crypto');
random_image_number_hashed = crypto.createHmac('sha1', 'myKey123').update(random_image_number).digest('hex');

Using openssl to rename the files and crypto.js on the javascript side, you should be able to use SHA1 to hash the filenames.

The following bash loop will do the renames. Note that it assumes no space characters in filenames.

for file in `/bin/ls *.jpg`; do
    hashedFileName=`echo -n ${file%.*} | openssl dgst -sha1 -hmac "myKey123"`;
    mv $file $hashedFileName.jpg;

Javascript-side will look something like the following for each image name:

// hash filename "1.jpg":
var hashedFileName = CryptoJS.HmacSHA1("1", "myKey123")   ".jpg";
By : pb2q

This video can help you solving your question :)
By: admin