To save a file i defined the following method
public int encrypt(String fileName, String password) {
return (fileName.concat(password)).hashCode();
}
This returns a hashvalue that is stored in a file. Whenever the user wants to access the file, he enters the password, and if the same hash is generated, he can access the file.
I suppose this isn't really safe, but how safe it is? How high is the chance that String#hashCode generates the same hash with two different inputs?
EDIT:
According to your answers I changed the code:
public String encrypt(String password) {
String hash = "";
try {
MessageDigest md5 = MessageDigest.getInstance("SHA-512");
byte [] digest = md5.digest(password.getBytes("UTF-8"));
hash = Arrays.toString(digest);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return hash;
}
So it should be better now??