How to hash a password with SHA-512 in Java?

后端 未结 7 1894
天涯浪人
天涯浪人 2020-11-30 23:10

I\'ve been investigating a bit about Java String encryption techniques and unfortunately I haven\'t find any good tutorial how to hash String with SHA-512 in Java; I read a

7条回答
  •  再見小時候
    2020-11-30 23:25

    import java.nio.charset.StandardCharsets;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import org.apache.commons.codec.binary.Hex;
    
    public String getHashSHA512(String StringToHash, String salt){
            String generatedPassword = null;
            try {
                MessageDigest md = MessageDigest.getInstance("SHA-512");
                md.update(salt.getBytes(StandardCharsets.UTF_8));
                byte[] bytes = md.digest(StringToHash.getBytes(StandardCharsets.UTF_8));
                generatedPassword = Hex.encodeHexString(bytes);
            }
            catch (NoSuchAlgorithmException e){
                e.printStackTrace();
            }
            return generatedPassword;
        }
    

    It's not recommended to use hash functions for passwords though, newer alogrithms like bcrypt, or scrypt exist

提交回复
热议问题