Skip to main content

PHP generating random salt using your own function

Generating salts is useful, when performing some kind of hashing on critical data such as user passwords, as it makes the hash decoding much more complicated. So, even if someone gets into the system and steals the essential data, it is almost impossible for him to obtain the original data because hash decoding requires a large amount of computer resources, time and some serious brutal attacks.
You can copy the following PHP script to generate salts with your own function which means you can control the salt generation, The comments are just for documentation purpose, so, you can remove the comments for development purposes.

/**
     * This function generates random string containing the specified characters and length
     * @param string $charset The characters to be used for generating the string
     * @param boolean $randomLength TRUE for using random length between <strong>$min</strong> & <strong>$max</strong>
     * @param int $min The minimum length to be used when generating the string of random length
     * @param int $max The maximum length to be used when generating the string of random length
     * @param int $len The length of the string to be generated when no random length is required
     * @param int $shuffle The number of times the characters are to be shuffled. More shuffling means more uniqueness
     * @return string The generated string
     */
    public function generate_salt($charset = "abcdefghijklmnopqrstuvwxyz0123456789", $randomLength = TRUE, $min = 5, $max = 15, $len = 8, $shuffle = 6) {
        $charset = str_split(str_shuffle($charset));
        $salt = "";
        $len = $randomLength == TRUE ? rand($min, $max) : $len;
        for (; $shuffle > 0; $shuffle--) {
            shuffle($charset);
        }
        for ($i = 0; $i < $len; $i++) {
            $salt.=$charset[$i];
        }
        return $salt;
    }

Comments

Popular posts from this blog

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Vanilla Javascript each()

JQuery's each() is very useful when iterating through elements. But you don't want to use JQuery in your project you can simply add the following javascript code which works somewhat similar to the JQuery's each function. Here the fnc parameter is the function string which is converted to a valid function call replacing all the $(this) with this /**  * This function binds a particular function to every element with the specified selector. It is somewhat same as JQuery's each() with less functionality  * @param {String|DOMElement} selector  * @param {Function} fnc  */ function each(selector, fnc) {     var elem;     if (typeof selector === "string") {         elem = $_(selector);     } else {         elem = selector;     }     fnc = (fnc.toString().replace("$(this)", "elem") + "();").replace("function () {", "").replac...

Java Program to display Welcome Message

import java.io.*;// I/O package imported. public class Welcome{        //class name is "Welcome"     public Welcome(){      //constructor declaired to print the message.         System.out.println("Welcome to Java Programming Language!");/* System.out.println is used for output. Welcome Message is written within " ".*/     }//display() closes here.     public static void main(String[] args){        //main() is declaired to declair an object in it.         Welcome obj=new Welcome();  //Object "Obj" is bean created.     }//main() closes. }//class "Welcome" ends here. Above program displays the message which is written by you in " ".  In programs "/*" and "*/" are use for multiple line comment(s) and "//" is use for single line comment. Code line "Welc...