Note that there are some explanatory texts on larger screens.

plurals
  1. POHashing password and comparing with MD5
    primarykey
    data
    text
    <p>I have the following requirement.</p> <pre><code>1. save a user password converted to hash(digested) 2. when comparing with data base, add random bytes with the password given from user 3. now send the random bytes added password to DAO class 4. separate the random byte from password 5. compare with the stored hashed(digested) password </code></pre> <p>I tried some thing similar but it gives array out of bound exception.</p> <pre><code>package poc; import com.sun.xml.internal.ws.message.ByteArrayAttachment; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.security.MessageDigest; import java.security.SecureRandom; import java.util.Arrays; public class HashedPassword { public static final String CRYPTOGRAPHY_ALGORITHM = "MD5"; public static final String CHAR_SET = "UTF8"; public static void main(String[] arg){ System.out.println(createPassword("r14@17*$")); } public static byte[] createPassword(String password){ byte[] salt = new byte[12]; byte[] digestedPassword =null; byte[] digestedPasswordPwd =null; try { SecureRandom random = new SecureRandom(); random.nextBytes(salt); MessageDigest mdPassword = MessageDigest.getInstance(CRYPTOGRAPHY_ALGORITHM); MessageDigest mdPasswordPawd = MessageDigest.getInstance(CRYPTOGRAPHY_ALGORITHM); mdPassword.update(salt); mdPassword.update(password.getBytes(CHAR_SET)); mdPasswordPawd.update(password.getBytes(CHAR_SET)); digestedPassword = mdPassword.digest(); digestedPasswordPwd = mdPasswordPawd.digest(); byte[] resultBytes= new byte[1000]; System.arraycopy(digestedPassword, 11, resultBytes,0,digestedPassword.length); if(Arrays.equals(resultBytes, digestedPasswordPwd)){ System.out.println("match"); }else{ System.out.println("no-match"); } } catch (Exception ex) { ex.printStackTrace(); } System.out.println("digestedPassword : "+digestedPassword); System.out.println("digestedPasswordPwd : "+digestedPasswordPwd); return digestedPassword; } } </code></pre> <p>Stacktrace : </p> <pre><code>java.lang.ArrayIndexOutOfBoundsException digestedPassword : [B@9980d5 digestedPasswordPwd : [B@1d95492 [B@9980d5 at java.lang.System.arraycopy(Native Method) at poc.HashedPassword.createPassword(HashedPassword.java:43) at poc.HashedPassword.main(HashedPassword.java:23) </code></pre> <p>so please help me how to go about it</p> <p>Kind Regards</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload