使⽤JAVA实现SSHA512的加密以及校验参考Python的⼀个类库使⽤JAVA进⾏了实现,直接上代码
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import java.util.Random;
public class SSHA512Util {
private static byte[] addBytes(byte[] data1, byte[] data2) {
byte[] data3 = new byte[data1.length + data2.length];
System.arraycopy(data1, 0, data3, 0, data1.length);
System.arraycopy(data2, 0, data3, data1.length, data2.length);
return data3;
}
public static String getSSHA1512Local(String inputStr){
StringBuilder str=new StringBuilder();//定义变长字符串
Random random=new Random();
//随机⽣成数字,并添加到字符串
for(int i=0;i<8;i++){
str.Int(10));
}
//将字符串转换为数字并输出
int num=Integer.String());
System.out.println(num);
byte[] password = Bytes();
byte[] salt = String().getBytes();
try {
MessageDigest digest = Instance("SHA-512");
digest.update(password);
digest.update(salt);
byte[] hashed = digest.digest();
byte[] data = addBytes(hashed, salt);
String encodedHash = Encoder().encodeToString(data);
return "{SSHA512}" + encodedHash;
}
catch (NoSuchAlgorithmException e)
{
return null;
}
}
public static boolean verifySSHA512local(String plainText, String sshaText)
{
if(sshaText.startsWith("{SSHA512}") || sshaText.startsWith("{ssh512}"))
{
sshaText = sshaText.substring(9);
}
else
{
return false;
}
byte[] challengePasswordByte = Decoder().decode(sshaText);
if(challengePasswordByte.length <= 64)
{
return false;
}
python转java代码
}
byte[] encrptByte = new byte[64];
byte[] saltByte = new byte[8];
System.arraycopy(challengePasswordByte, 0, encrptByte, 0, 64);
System.arraycopy(challengePasswordByte, 64, saltByte, 0, 8);
try {
MessageDigest digest = Instance("SHA-512");            set();
digest.Bytes());
digest.update(saltByte);
byte[] hashed = digest.digest();
if(Arrays.equals(hashed, encrptByte))
{
return true;
}
else
{
return false;
}
}
catch (NoSuchAlgorithmException e)
{
return false;
}
}
public static void main(String[] argc)
{
String password = "123456";
String encodePassword = getSSHA1512Local(password);
boolean boolVerify = verifySSHA512local("123456", encodePassword);        System.out.println(boolVerify);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。