Java Hashing Password Doesn't Match

Jesus Gonzalez

After searching in Google and watching a few posts in StackOverflow ( Java hashing passwords , Hashing Password ). I try not to duplicate questions and looking for the answers by myself, but as you can appreciate, this was not the case.

I'm creating a simple library in Java to hash passwords using SHA256 algorithm.

Everytime I create a hash the password generated is different. This happens with SHA256 and MD5 algorithms.

Why is this happening? I think that passwords generated should be the same. I may be totally wrong and confused about how hashing works.

The hashing method:

CipherString.java

    public static String cipherPassword(String pwd, String salt) throws NoSuchAlgorithmException, UnsupportedEncodingException
{
    MessageDigest d = MessageDigest.getInstance("SHA-256");
    d.update(salt.getBytes("UTF-8"));
    byte[] hash = d.digest(pwd.getBytes("UTF-8"));

    StringBuilder sb = new StringBuilder();

    for(int i=0; i< hash.length ;i++)
    {
        sb.append(Integer.toString((hash[i] & 0xff) + 0x100, 16).substring(1));
    }

    String pwdCifrada = sb.toString();

    return pwdCifrada;
}

EDIT:

Old Main.java (bugged code)

String username = txtUsername.getText();
char[] password = txtPassword.getPassword();
String hashedPassword = cipherPassword(password.toString(), username);

New Main.java (fixed/solved code)

String username = txtUsername.getText();
char[] password = txtPassword.getPassword();
String hashedPassword = cipherPassword(new String(password), username);

I have deleted all the models, view and controllers which are unneeded.

Thank you all.

tbraun

I strongly recommend using a library to handle this for you.

Consider Apache Commons Codec library:

import org.apache.commons.codec.digest.DigestUtils;

public class HashTest {
    public static String cipher(String pwd, String salt) {
        return DigestUtils.sha256Hex(pwd+salt);
    }
    public static void main(String[] args) {
        String p = "password";
        String s = "randomSalt";
        String c = cipher(p, s);
        System.out.println(c);
    }
}

This will always print

a0494b0d7ef89bba60f9703e2c438465cd1241cc440a8fc20f4330639d2c9c2f

If you are using Maven to manage your dependencies you can check the latest version here: http://mvnrepository.com/artifact/commons-codec/commons-codec

Or use the current latest:

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.10</version>
</dependency>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

PHP: Password Hashing

분류에서Dev

PHP preg_match doesn't match

분류에서Dev

Java script password match and complexity issues

분류에서Dev

Sudo doesn't recognize correct password

분류에서Dev

Screen locker doesn't prompt for password

분류에서Dev

iOS: MPMoviePlayerController text doesn't match language

분류에서Dev

Android UriMatcher doesn't match wildcards

분류에서Dev

Restoring of MySQL password fails and any of the Websites doesn't work

분류에서Dev

sudo inside docker (on fedora) ask for password, on host it doesn't ask password

분류에서Dev

Why kernel version doesn't match Ubuntu version in a Docker container?

분류에서Dev

Calculation of leap years doesn't seem to match JavaScript Date

분류에서Dev

Output inside function scope doesn't match that of it's parent

분류에서Dev

python re.findall doesn't match duplicates

분류에서Dev

Type deducted of method definition doesn't match declaration

분류에서Dev

div table column width doesn't match rows

분류에서Dev

Bundle ID doesn't match any App ID

분류에서Dev

Number of fields in pasted data doesn't match the columns in the table

분류에서Dev

Postgres: Foreign key doesn't match all rows

분류에서Dev

Default SSH username doesn't match $USER value in macOS

분류에서Dev

Java 8 lambda doesn't change data

분류에서Dev

java diamond operator doesn't compile

분류에서Dev

Why doesn't the Java compiler flag this as an error?

분류에서Dev

How do I ensure Git doesn't ask me for my GitHub username and password?

분류에서Dev

I am looking to match patterns for all wikimedia sites I tried using the following regular expression. But it doesn't match

분류에서Dev

Password-Hashing with Taylor Hornby (Defuse)'s Compatible version works with all PHP versions?

분류에서Dev

Regular Expression Match Doesn't Start With, Contain, Or End With Space And Not Empty String

분류에서Dev

Django: Why Doesn't the Current URL Match any Patterns in urls.py

분류에서Dev

Ruby .where to see if array includes a result that doesn't match query.

분류에서Dev

heatmap.2- Error: Colv dendrogram doesn't match size of x

Related 관련 기사

  1. 1

    PHP: Password Hashing

  2. 2

    PHP preg_match doesn't match

  3. 3

    Java script password match and complexity issues

  4. 4

    Sudo doesn't recognize correct password

  5. 5

    Screen locker doesn't prompt for password

  6. 6

    iOS: MPMoviePlayerController text doesn't match language

  7. 7

    Android UriMatcher doesn't match wildcards

  8. 8

    Restoring of MySQL password fails and any of the Websites doesn't work

  9. 9

    sudo inside docker (on fedora) ask for password, on host it doesn't ask password

  10. 10

    Why kernel version doesn't match Ubuntu version in a Docker container?

  11. 11

    Calculation of leap years doesn't seem to match JavaScript Date

  12. 12

    Output inside function scope doesn't match that of it's parent

  13. 13

    python re.findall doesn't match duplicates

  14. 14

    Type deducted of method definition doesn't match declaration

  15. 15

    div table column width doesn't match rows

  16. 16

    Bundle ID doesn't match any App ID

  17. 17

    Number of fields in pasted data doesn't match the columns in the table

  18. 18

    Postgres: Foreign key doesn't match all rows

  19. 19

    Default SSH username doesn't match $USER value in macOS

  20. 20

    Java 8 lambda doesn't change data

  21. 21

    java diamond operator doesn't compile

  22. 22

    Why doesn't the Java compiler flag this as an error?

  23. 23

    How do I ensure Git doesn't ask me for my GitHub username and password?

  24. 24

    I am looking to match patterns for all wikimedia sites I tried using the following regular expression. But it doesn't match

  25. 25

    Password-Hashing with Taylor Hornby (Defuse)'s Compatible version works with all PHP versions?

  26. 26

    Regular Expression Match Doesn't Start With, Contain, Or End With Space And Not Empty String

  27. 27

    Django: Why Doesn't the Current URL Match any Patterns in urls.py

  28. 28

    Ruby .where to see if array includes a result that doesn't match query.

  29. 29

    heatmap.2- Error: Colv dendrogram doesn't match size of x

뜨겁다태그

보관