|
|
|
@ -25,7 +25,7 @@ public class PasswordPartyServiceImpl implements IPasswordPartyService { |
|
|
|
private final PasswordPartyConfig passwordPartyConfig; |
|
|
|
|
|
|
|
private static final int KEY_INDEX = 5; |
|
|
|
private static final String ALG_TYPE = "SM4"; |
|
|
|
private static final String ALG_TYPE = "SM4-CBC"; |
|
|
|
|
|
|
|
/** |
|
|
|
* 生成随机数 |
|
|
|
@ -37,7 +37,6 @@ public class PasswordPartyServiceImpl implements IPasswordPartyService { |
|
|
|
public String generateRandom(int length) { |
|
|
|
JSONObject params = new JSONObject(); |
|
|
|
params.put("length", length); |
|
|
|
|
|
|
|
JSONObject data = callHsmApi("/hsm/api/GenerateRandom", params, "生成随机数"); |
|
|
|
return data.getString("random"); |
|
|
|
} |
|
|
|
@ -45,14 +44,13 @@ public class PasswordPartyServiceImpl implements IPasswordPartyService { |
|
|
|
/** |
|
|
|
* 对称加密 |
|
|
|
* |
|
|
|
* @param key 外部密钥,keyIndex为0时生效(Hex 16进制类型) |
|
|
|
* @param iv IV值,CBC/CFB/OFB模式时必传(Hex 16进制类型) |
|
|
|
* @param inputData 加密原文(Hex 16进制类型) |
|
|
|
* @return 对称加密结果 |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public SymmetryEncryptVo symmetryEncrypt(String key, String iv, String inputData) { |
|
|
|
JSONObject params = buildSymmetryParams(key, iv, inputData); |
|
|
|
public SymmetryEncryptVo symmetryEncrypt(String inputData) { |
|
|
|
String iv = generateRandom(16); |
|
|
|
JSONObject params = buildSymmetryParams(iv, inputData); |
|
|
|
|
|
|
|
JSONObject data = callHsmApi("/hsm/api/symmetryEncrypt", params, "对称加密"); |
|
|
|
return buildSymmetryEncryptVo(data); |
|
|
|
@ -61,14 +59,13 @@ public class PasswordPartyServiceImpl implements IPasswordPartyService { |
|
|
|
/** |
|
|
|
* 对称解密 |
|
|
|
* |
|
|
|
* @param key 外部密钥,keyIndex为0时生效(Hex 16进制类型) |
|
|
|
* @param iv IV值,CBC/CFB/OFB模式时必传(Hex 16进制类型) |
|
|
|
* @param inputData 解密原文(Hex 16进制类型) |
|
|
|
* @return 对称解密结果 |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public SymmetryEncryptVo symmetryDecrypt(String key, String iv, String inputData) { |
|
|
|
JSONObject params = buildSymmetryParams(key, iv, inputData); |
|
|
|
public SymmetryEncryptVo symmetryDecrypt(String iv, String inputData) { |
|
|
|
JSONObject params = buildSymmetryParams(iv, inputData); |
|
|
|
|
|
|
|
JSONObject data = callHsmApi("/hsm/api/symmetryDecrypt", params, "对称解密"); |
|
|
|
return buildSymmetryEncryptVo(data); |
|
|
|
@ -148,17 +145,13 @@ public class PasswordPartyServiceImpl implements IPasswordPartyService { |
|
|
|
/** |
|
|
|
* HMAC摘要 |
|
|
|
* |
|
|
|
* @param key 密钥,非必传(Hex 16进制类型) |
|
|
|
* @param inputData 明文摘要(Hex 16进制类型,必填) |
|
|
|
* @return HMAC值(Hex 16进制类型) |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public String hmacWithIndex(String key, String inputData) { |
|
|
|
public String hmacWithIndex(String inputData) { |
|
|
|
JSONObject params = new JSONObject(); |
|
|
|
params.put("keyIndex", KEY_INDEX); |
|
|
|
if (key != null) { |
|
|
|
params.put("key", key); |
|
|
|
} |
|
|
|
params.put("algType", "SM3"); |
|
|
|
params.put("inputData", inputData); |
|
|
|
|
|
|
|
@ -169,13 +162,10 @@ public class PasswordPartyServiceImpl implements IPasswordPartyService { |
|
|
|
/** |
|
|
|
* 构建对称加密/解密的公共请求参数 |
|
|
|
*/ |
|
|
|
private JSONObject buildSymmetryParams(String key, String iv, String inputData) { |
|
|
|
private JSONObject buildSymmetryParams(String iv, String inputData) { |
|
|
|
JSONObject params = new JSONObject(); |
|
|
|
params.put("keyIndex", KEY_INDEX); |
|
|
|
params.put("algType", ALG_TYPE); |
|
|
|
if (key != null) { |
|
|
|
params.put("key", key); |
|
|
|
} |
|
|
|
if (iv != null) { |
|
|
|
params.put("iv", iv); |
|
|
|
} |
|
|
|
|