我想使用SHA1哈希将密码插入我的数据库 我通过选择函数sha1在phpmyadmin中手动完成,但是如何使用Java ??
执行此操作任何想法?谢谢!
答案 0 :(得分:1)
如果您必须use java:
import java.io.ByteArrayInputStream;
import java.security.MessageDigest;
public class SHACheckSumExample
{
public static void main(String[] args)throws Exception
{
MessageDigest md = MessageDigest.getInstance("SHA-1");
ByteArrayInputStream fis = new ByteArrayInputStream(args[1].getBytes());
byte[] dataBytes = new byte[1024];
int nread = 0;
while ((nread = fis.read(dataBytes)) != -1) {
md.update(dataBytes, 0, nread);
};
byte[] mdbytes = md.digest();
//convert the byte to hex format method 1
StringBuffer sb = new StringBuffer();
for (int i = 0; i < mdbytes.length; i++) {
sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println("Hex format : " + sb.toString());
//convert the byte to hex format method 2
StringBuffer hexString = new StringBuffer();
for (int i=0;i<mdbytes.length;i++) {
hexString.append(Integer.toHexString(0xFF & mdbytes[i]));
}
System.out.println("Hex format : " + hexString.toString());
}
}
出于性能原因,我会建议查看您的数据库是否支持SHA。我知道Postgres does,不确定其他系统。