在Java中使用SHA-512存储密码 [英] Hash a password with SHA-512 in Java

查看:366
本文介绍了在Java中使用SHA-512存储密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Java String加密技术,不幸的是我没有找到适合SHA-512的优秀教程;我读了一些关于MD5和Base64的博客,但他们没有我想要的那么安全(实际上,Base64不是一种加密技术),所以我更喜欢SHA-512。

I've been investigating a bit about Java String encryption techniques and unfortunately I haven't find any good tutorial for SHA-512; I read a few blogs about MD5 and Base64, but they are not as secure as I'd like to (actually, Base64 is not an encryption technique), so I prefer SHA-512.

推荐答案

您可以将此用于SHA-512

you can use this for SHA-512

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public String get_SHA_512_SecurePassword(String passwordToHash, String   salt){
String generatedPassword = null;
    try {
         MessageDigest md = MessageDigest.getInstance("SHA-512");
         md.update(salt.getBytes(StandardCharsets.UTF_8));
         byte[] bytes = md.digest(passwordToHash.getBytes(StandardCharsets.UTF_8));
         StringBuilder sb = new StringBuilder();
         for(int i=0; i< bytes.length ;i++){
            sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
         }
         generatedPassword = sb.toString();
        } 
       catch (NoSuchAlgorithmException e){
        e.printStackTrace();
       }
    return generatedPassword;
}

这篇关于在Java中使用SHA-512存储密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆