1. 登录模块新增软key登录模块代码

develop
admin 2024-11-01 22:14:30 +08:00
parent 877b0aa34a
commit d4cebec79d
2 changed files with 151 additions and 2 deletions

View File

@ -15,6 +15,7 @@ import com.bocloud.sms.interfaces.TrxService;
import com.bocloud.sms.model.*;
import com.bocloud.sms.repository.RoleRepository;
import com.bocloud.sms.repository.UserRepository;
import com.bocloud.sms.service.utils.MessageDigestUtils;
import com.bocloud.sms.service.utils.QxUtils;
import com.bocloud.sms.service.utils.Sha256Util;
import com.bocloud.sms.service.utils.YkUtils;
@ -173,10 +174,11 @@ public class TrxServiceImpl implements TrxService {
@Override
public GeneralResult getpwdAuthen(TrxAuthModel trxAuthModel) {
String url = trxUrl + "/pwdAuthen";
String s1 = Sha256Util.sha256(trxAuthModel.getPassword());
String s1 = MessageDigestUtils.encrypt(trxAuthModel.getPassword(), MessageDigestUtils.SHA_256);
log.info("密码s1 sha256值:" + s1);
log.info("getRandoms 值:" + trxAuthModel.getRandoms());
String rs1 = Sha256Util.sha256(trxAuthModel.getRandoms() + s1);
log.info("待加密 值:" + trxAuthModel.getRandoms()+s1);
String rs1 = MessageDigestUtils.encrypt(trxAuthModel.getRandoms()+s1, MessageDigestUtils.SHA_256);
log.info("密码rs1 sha256值:" + rs1);
Map<String, Object> paramsMap = new HashMap<>();

View File

@ -0,0 +1,147 @@
package com.bocloud.sms.service.utils;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
*
*
* <pre>
*
* MD2 The MD2 message digest algorithm as defined in RFC 1319.
* MD5 The MD5 message digest algorithm as defined in RFC 1321.
* SHA-1 Hash algorithms defined in the FIPS PUB 180-2.
* SHA-256 SHA-256 is a 256-bit hash function intended to provide 128 bits of security against collision attacks,
* SHA-384 while SHA-512 is a 512-bit hash function intended to provide 256 bits of security. A 384-bit hash may
* SHA-512 be obtained by truncating the SHA-512 output.
* </pre>
*
* @author Shawpin Shi
* @since 2016-9-1
*/
@Slf4j
public class MessageDigestUtils {
private final static char[] HEX_ARRAY = "0123456789abcdef".toCharArray();
/**
* MD5(128bit,16byte,32hex)<br>
* Message Digest Algorithm MD5
*/
public static final String MD5 = "MD5";
/**
* SHA-1(160bit,20byte,40hex)<br>
* Secure Hash Algorithm<br>
* SHASHA-1SHA-224SHA-256SHA-384SHA-512 NSA<br>
* NIST SHA-2<br>
* JavaSHA-224
*/
public static final String SHA_1 = "SHA-1";
/**
* SHA-256(256bit,64hex)
*/
public static final String SHA_256 = "SHA-256";
/**
* SHA-384(384bit,96hex)
*/
public static final String SHA_384 = "SHA-384";
/**
* SHA-512(512bit,64byte,128hex)
*/
public static final String SHA_512 = "SHA-512";
/**
*
*
* @author Shawpin Shi
* @since 2016-9-1
*/
public static String encrypt(String source, String algorithmName) {
return encrypt(source, algorithmName, StandardCharsets.UTF_8);
}
/**
* ()
*
* @author Shawpin Shi
* @since 2020-12-03
*/
public static String encrypt(String source, String algorithmName, Charset charset) {
String ciphertext = null;
MessageDigest md;
try {
md = MessageDigest.getInstance(algorithmName);
byte[] digest = md.digest(source.getBytes(charset));
ciphertext = byteToHexString(digest);
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage(), e);
}
return ciphertext;
}
/**
*
*
* @author Shawpin Shi
* @since 2016-9-1
*/
public static String encryptWithSalt(String source, String salt, String algorithmName) {
String newSource = source + "@" + salt;
return encrypt(newSource, algorithmName);
}
/**
* 16
*
* @author Shawpin Shi
* @since 2016-9-1
* @since 2017-9-29
*/
private static String byteToHexString(byte[] bytes) {
int len = bytes.length;
if (len > Integer.MAX_VALUE >> 1) {
throw new RuntimeException("字节数组的长度不能超过" + (Integer.MAX_VALUE >> 1));
}
int newLen = len << 1;// len * 2
char[] hexChars = new char[newLen];
int v, index;
for (int i = 0; i < len; i++) {
v = bytes[i] & 0xFF; // 保留低8位高24位置0
index = i << 1;// i * 2
hexChars[index] = HEX_ARRAY[v >>> 4];
hexChars[index + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
/**
* Test
*/
public static void main(String[] args) {
String source = "0aece0ff48aafb9c481826d13ef5e3c8b74e44b4584b28a0fe499aee108227e8db8470f4b9f9ac5d2ab508a9ff84312da9f1646a8b5ed6570fd2f27790460e47";
System.out.println("Source String:" + source);
System.out.println("Encrypted String:");
System.out.println("Use MD5: " + MessageDigestUtils.encrypt(source, MessageDigestUtils.MD5));
System.out.println("Use SHA-1: " + MessageDigestUtils.encrypt(source, MessageDigestUtils.SHA_1));
System.out.println("Use SHA-256: " + MessageDigestUtils.encrypt(source, MessageDigestUtils.SHA_256));
System.out.println("Use SHA-384: " + MessageDigestUtils.encrypt(source, MessageDigestUtils.SHA_384));
System.out.println("Use SHA-512: " + MessageDigestUtils.encrypt(source, MessageDigestUtils.SHA_512));
System.out.println("MD5 Salt: " + MessageDigestUtils.encryptWithSalt(source, "sxp", MessageDigestUtils.MD5));
}
}