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);   ...

Java Program to calculate the Strike Rate of a Cricket Batsman

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

WP similar or related posts widget without using any plugin

Hi guys, Recently I was working on some WP website and my client told me that he required a widget for displaying related/similar posts on the single post page. But as his website was already using many plugins, even for some pretty small tasks like this one, I decided not to use another WP plugin (plugins are not good for your WP websites, we will discuss about that on some other post.) I am not explaining the code as it is pretty simple if you are familiar with WP classes. But please let me know if you have any questions related to the PHP code posted below in the comments section or even much better, on Gist. You can add the following code directly in your child theme's functions.php file or you can create a separate file and include this at the bottom of functions.php file. <?php class similar_posts_widget extends WP_Widget {     function __construct()     {         parent::__construct('similar_posts_...