Alexi Ned
Alexi Ned

Reputation: 21

Javascript File Name Encryption and Referencing

I want to change the JavaScript names in my application for additional security like facebook and google plus suring deployment.

Is there are an application or a library that can change the JavaScript file names and reference them in the my view (written in php) and JavaScript files.


EXAMPLE OF THE DESIRED EFFECT

Change this (Before Deployment):

In folder: js/myfunction.js

In file:<script type="text/javascript" src="https://mysite.com/myfunction.js"></script>

To this (After Deployment):

In folder: js/PuKJS78UyH.js

In file: <script type="text/javascript" src="https://mysite.com/PuKJS78UyHK.js"></script>

Upvotes: 2

Views: 3114

Answers (2)

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94349

You should not depend on JavaScript encryption. It is not safe, and might be hacked in a short time. Using sever side languages like PHP is much safer than JavaScript.

However, if you would like to perform a simple base-64 encoding in JavaScript, for which normal people will not able to read, you are lucky, it doesn't need any library. \(^o^)/

Just use btoa() for encoding, and atob() for decoding. Then you can create a <script> tag using the encoded URL.

Read more in MDN: window.atob

Example:

var txt = "myfunction.js";
var encode = btoa(txt);
var decode = atob(encode);
console.log( encode );  //return "bXlmdW5jdGlvbi5qcw=="
console.log( decode );  //return "myfunction.js" (orginal)

//Do whatever you want with the encoded text, like
$("<script src='/js/"+encode+".js' type='text/javascript'></script>")
    .appendTo("head");  //dynamically adding an script tag using jQuery

Demo: http://jsfiddle.net/DerekL/JWSUs/

Result:
result

Result from jsFiddle. You can see that "myfunction.js" is encoded to "bXlmdW5jdGlvbi5qcw==", which normal people will not be able to read.

Upvotes: 0

valentinas
valentinas

Reputation: 4337

Instead of obfuscating and encryption you should think about optimization. Couple things that you could do:

  1. Combine all common JS files in one file (minimizes number of requests and also solves your problem - there will be no file names to obfuscate)
  2. Minimize JS - it's faster that way and takes less space (and in addition it becomes unreadable)

This tool looks like a good place to start: http://code.google.com/p/minify/

Upvotes: 1

Related Questions