The salt is a sequence of characters / bytes which is added to the password before getting digested by message digest algorithm, such as MD5 or SHA . It is better to send salted hash rather than sending encrypted the password alone . This protects against Brute Force attacks . Salt may be fixed or variable . In fixed salt , everytime the same charaters are used for digesting every password which is not more safe. In variable salt, different characters (Random) are used for each password being digested which is more secured. Now let us see how random salt is generated. For that we have to define a set of characters as a Salt. From these characters , we have to pick some number of chars (in our example it is 1 to 9 random number is generated between 1 to 9 ) randomly which will be a variable salt. The following code returns random salt characters between 1 to 9 .
import java.util.*;
class RandomSaltChars
{
public static void main(String args[])
{
String chars_salt = "1234567890abcdefghijklABCDEFGHIJKLMNOPQRSTUVWXYZmnopqrstuvwxyz";
StringBuffer random_salt = new StringBuffer();
Random rnd = new Random();
int noc = rnd.nextInt(9) + 1;
//returns a random 9 chars salt
while (random_salt.length() < noc) {
int index = (int) (rnd.nextFloat() *chars_salt.length());
random_salt.append(chars_salt.charAt( index));
}
System.out.println("salt characters : " + random_salt);
}
}
Output of the program:
0 comments:
Post a Comment