How can I hash a password in Java?

前端 未结 13 1968
暖寄归人
暖寄归人 2020-11-22 05:00

I need to hash passwords for storage in a database. How can I do this in Java?

I was hoping to take the plain text password, add a random salt, then store the salt a

相关标签:
13条回答
  • 2020-11-22 05:23

    BCrypt is a very good library, and there is a Java port of it.

    0 讨论(0)
  • 2020-11-22 05:26

    Among all the standard hash schemes, LDAP ssha is the most secure one to use,

    http://www.openldap.org/faq/data/cache/347.html

    I would just follow the algorithms specified there and use MessageDigest to do the hash.

    You need to store the salt in your database as you suggested.

    0 讨论(0)
  • 2020-11-22 05:27

    Here you have two links for MD5 hashing and other hash methods:

    Javadoc API: https://docs.oracle.com/javase/1.5.0/docs/api/java/security/MessageDigest.html

    Tutorial: http://www.twmacinta.com/myjava/fast_md5.php

    0 讨论(0)
  • 2020-11-22 05:27

    i leaned that from a video on udemy and edited to be stronger random password

    }
    
    private String pass() {
            String passswet="1234567890zxcvbbnmasdfghjklop[iuytrtewq@#$%^&*" ;
    
            char icon1;
            char[] t=new char[20];
    
             int rand1=(int)(Math.random()*6)+38;//to make a random within the range of special characters
    
                icon1=passswet.charAt(rand1);//will produce char with a special character
    
            int i=0;
            while( i <11) {
    
                 int rand=(int)(Math.random()*passswet.length());
                 //notice (int) as the original value of Math>random() is double
    
                 t[i] =passswet.charAt(rand);
    
                 i++;
                    t[10]=icon1;
    //to replace the specified item with icon1
             }
            return new String(t);
    }
    
    
    
    
    
    
    }
    
    0 讨论(0)
  • 2020-11-22 05:29

    You can use the Shiro library's (formerly JSecurity) implementation of what is described by OWASP.

    It also looks like the JASYPT library has a similar utility.

    0 讨论(0)
  • 2020-11-22 05:35

    As of 2020, the most reliable password hashing algorithm in use, most likely to optimise its strength given any hardware, is Argon2id or Argon2i but not its Spring implementation.

    The PBKDF2 standard includes the the CPU-greedy/computationally-expensive feature of the block cipher BCRYPT algo, and add its stream cipher capability. PBKDF2 was overwhelmed by the memory exponentially-greedy SCRYPT then by the side-channel-attack-resistant Argon2

    Argon2 provides the necessary calibration tool to find optimized strength parameters given a target hashing time and the hardware used.

    • Argon2i is specialized in memory greedy hashing
    • Argon2d is specialized in CPU greedy hashing
    • Argon2id use both methods.

    Memory greedy hashing would help against GPU use for cracking.

    Spring security/Bouncy Castle implementation is not optimized and relatively week given what attacker could use. cf: Spring doc Argon2 and Scrypt

    The currently implementation uses Bouncy castle which does not exploit parallelism/optimizations that password crackers will, so there is an unnecessary asymmetry between attacker and defender.

    The most credible implementation in use for java is mkammerer's one,

    a wrapper jar/library of the official native implementation written in C.

    It is well written and simple to use.

    The embedded version provides native builds for Linux, windows and OSX.

    As an example, it is used by jpmorganchase in its tessera security project used to secure Quorum, its Ethereum cryptocurency implementation.

    Here is an example:

        final char[] password = "a4e9y2tr0ngAnd7on6P১M°RD".toCharArray();
        byte[] salt = new byte[128];
        new SecureRandom().nextBytes(salt);
        final Argon2Advanced argon2 = Argon2Factory.createAdvanced(Argon2Factory.Argon2Types.ARGON2id);
        byte[] hash = argon2.rawHash(10, 1048576, 4, password, salt);
    

    (see tessera)

    Declare the lib in your POM:

    <dependency>
        <groupId>de.mkammerer</groupId>
        <artifactId>argon2-jvm</artifactId>
        <version>2.7</version>
    </dependency>
    

    or with gradle:

    compile 'de.mkammerer:argon2-jvm:2.7'
    

    Calibration may be performed using de.mkammerer.argon2.Argon2Helper#findIterations

    SCRYPT and Pbkdf2 algorithm might also be calibrated by writing some simple benchmark, but current minimal safe iterations values, will require higher hashing times.

    0 讨论(0)
提交回复
热议问题