I have devised what I feel to be a simple hash/salting algo for Java, and am looking for some feedback from people more skilled than myself. Please find below my code;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
public class PassSecureSuite
{
/**
* Method: RecoverPass
* Function: This class contains methods for authenticating the user against the database and creating
* a new user with a secure password.
*/
public static String verifyIput(String submittedPass, String salt)
{
MessageDigest soMd = null;
try
{
soMd = MessageDigest.getInstance("SHA-256");
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
soMd.update(salt.getBytes());
byte[] abytesToString = soMd.digest(submittedPass.getBytes());
StringBuilder buildThis = new StringBuilder();
for(int i=0; i<abytesToString.length; i++)
{
buildThis.append(Integer.toString((abytesToString[i] & 0xff) + 0x100, 16).substring(1));
}
String passToVerify = buildThis.toString();
return passToVerify;
}
/**
* Method: getSalt
* Function: Generate a secure, randomly-generated string to add extra security to the user password
*/
public static String getSalt()
{
SecureRandom sosRand = null;
try
{
sosRand = SecureRandom.getInstance("SHA1PRNG", "SUN");
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch (NoSuchProviderException e)
{
e.printStackTrace();
}
byte[] baniceAndSalty = new byte[16];
sosRand.nextBytes(baniceAndSalty);
return baniceAndSalty.toString();
}
/**
* Method: agenerateSecurePass
* Function: Generates a password string based on a byte feed
*/
public static String sgenSecurePass(String password, String salt)
{
String ageneratedPass = null;
MessageDigest digest = null;
try
{
digest = MessageDigest.getInstance("SHA-256");
}
catch (NoSuchAlgorithmException nsae)
{
nsae.printStackTrace();
}
digest.update(salt.getBytes());
byte[] babytes = digest.digest(password.getBytes());
StringBuilder obuildThis = new StringBuilder();
for(int i=0; i < babytes.length; i++)
{
obuildThis.append(Integer.toString((babytes[i] & 0xff) + 0x100, 16).substring(1));
}
ageneratedPass = obuildThis.toString();
return ageneratedPass;
}
}
I am looking to make this as secure as possible without using other people's APIs, as I enjoy having total control over my security within applications.
The above example appears fairly robust to me, as I have been unable to break the hash/salt on any application I have built, but I feel that I can improve this, I am just not sure how.
StringBuilder.append()
line. \$\endgroup\$