Saturday, December 1, 2012

MySQL PASSWORD() function in JavaScript


If you need to implement a MySQL PASSWORD() function in JavaScript - use http://code.google.com/p/crypto-js/#SHA-1 with 2 encodings.

Here is an example:

var password = "TestPassword";
var result = ("*"+CryptoJS.SHA1(CryptoJS.SHA1(password))).toUpperCase();
console.log("result : " + result);

Result will be *0F437A73F4E4014091B7360F60CF81271FB73180. If you check it with MySQL password() it will be the same:

mysql> select password("TestPassword") as result;
+-------------------------------------------+
| result                                    |
+-------------------------------------------+
| *0F437A73F4E4014091B7360F60CF81271FB73180 |
+-------------------------------------------+

1 comment:

Myron E. Bennett said...

I looked at the example and used it in one of the scripts, the password field had to change, but overall it wasn't bad. Works and this is important. To get a promotion, I had to find a tutorial I can understand in more
detail the basic structures of java, for example, to do a simple job with a snake game, you can get more info here and I'm sure at the initial stages of the study you also did something similar. Further, I have many plans and I'll go deep into programming in Java.