如何比较 Java 中的 sha1 加密密码?
how to compare sha1 encripted password in Java?
假设我已经像这样加密了 SHA1 密码
String pass = "f6ce584e7b4ff5253eed4a2ea2b44247";
我想要这样的条件:
if (pass.equals("userinput")){
System.out.println("success");
}
请有人帮助我制定适当的条件/函数来比较用户输入和加密密码之间的值。非常感谢您的帮助。谢谢
获取哈希码:
public static byte[] sha1(byte[] data)
Calculates the SHA-1 digest and returns the value as a byte[].
Parameters:
data - Data to digest
Returns:
SHA-1 digest
这有助于您的流程。
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jcajce.provider.asymmetric.rsa.DigestSignatureSpi.SHA1;
public class SHA1_test {
public static String sha1(String s, String keyString)
throws UnsupportedEncodingException, NoSuchAlgorithmException,
InvalidKeyException {
SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"),
"HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(key);
byte[] bytes = mac.doFinal(s.getBytes("UTF-8"));
return new String(Base64.encodeBase64(bytes));
}
public static void main(String[] args) throws InvalidKeyException,
UnsupportedEncodingException, NoSuchAlgorithmException {
Boolean validate = false;
String code = sha1("admin", "123456");
String your_user_inputString = "testpassword";
if (code.equals(sha1(your_user_inputString, "123456"))) {
System.out.println("Correct");
} else {
System.out.println("Bad password");
}
}
}
这有效!!!
SHA1是一种哈希算法,也就是说它是单向的。散列后您无法获得原始消息。不同于双向加密(允许加密和解密)。
这意味着如果你想比较一个散列,你不会尝试获取原始消息。相反,您还散列要比较的消息,然后执行匹配:
因此,如果哈希密码存储为:
String pass = "f6ce584e7b4ff5253eed4a2ea2b44247";
为了匹配后续输入的密码,您可以:
//check if hashed userInput is also "f6ce584e7b4ff5253eed4a2ea2b44247"
if(pass.equals(sha1(userInput))){
//do whatever
}
实现一个sha1()
散列函数,参考:Java String to SHA1
假设我已经像这样加密了 SHA1 密码
String pass = "f6ce584e7b4ff5253eed4a2ea2b44247";
我想要这样的条件:
if (pass.equals("userinput")){
System.out.println("success");
}
请有人帮助我制定适当的条件/函数来比较用户输入和加密密码之间的值。非常感谢您的帮助。谢谢
获取哈希码:
public static byte[] sha1(byte[] data)
Calculates the SHA-1 digest and returns the value as a byte[].
Parameters:
data - Data to digest
Returns:
SHA-1 digest
这有助于您的流程。
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jcajce.provider.asymmetric.rsa.DigestSignatureSpi.SHA1;
public class SHA1_test {
public static String sha1(String s, String keyString)
throws UnsupportedEncodingException, NoSuchAlgorithmException,
InvalidKeyException {
SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"),
"HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(key);
byte[] bytes = mac.doFinal(s.getBytes("UTF-8"));
return new String(Base64.encodeBase64(bytes));
}
public static void main(String[] args) throws InvalidKeyException,
UnsupportedEncodingException, NoSuchAlgorithmException {
Boolean validate = false;
String code = sha1("admin", "123456");
String your_user_inputString = "testpassword";
if (code.equals(sha1(your_user_inputString, "123456"))) {
System.out.println("Correct");
} else {
System.out.println("Bad password");
}
}
}
这有效!!!
SHA1是一种哈希算法,也就是说它是单向的。散列后您无法获得原始消息。不同于双向加密(允许加密和解密)。
这意味着如果你想比较一个散列,你不会尝试获取原始消息。相反,您还散列要比较的消息,然后执行匹配:
因此,如果哈希密码存储为:
String pass = "f6ce584e7b4ff5253eed4a2ea2b44247";
为了匹配后续输入的密码,您可以:
//check if hashed userInput is also "f6ce584e7b4ff5253eed4a2ea2b44247"
if(pass.equals(sha1(userInput))){
//do whatever
}
实现一个sha1()
散列函数,参考:Java String to SHA1