This commit is contained in:
liulu
2024-12-20 10:34:08 +08:00
parent 427df73a40
commit e3bf63ddef
9 changed files with 98 additions and 13 deletions
@@ -0,0 +1,46 @@
package com.sunyard.chsm.controller;
import com.sunyard.chsm.auth.AuthCode;
import com.sunyard.chsm.constant.AuthCodeConst;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.param.HashReq;
import com.sunyard.chsm.param.HashResp;
import com.sunyard.chsm.sdf.SdfApiService;
import com.sunyard.chsm.utils.CodecUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
/**
* 杂凑运算类接口
*
* @author liulu
* @since 2024/12/19
*/
@RestController
public class HashController {
@Resource
private SdfApiService sdfApiService;
/**
* 计算Hash
*
* @param req
* @return
*/
@PostMapping("/hash")
@AuthCode(AuthCodeConst.cal_hash)
public R<HashResp> hash(@Valid @RequestBody HashReq req) {
byte[] bytes = CodecUtils.decodeBase64(req.getPlainData());
byte[] hash = sdfApiService.hash(bytes);
HashResp resp = new HashResp();
resp.setHash(CodecUtils.encodeBase64(hash));
return R.data(resp);
}
}
@@ -183,12 +183,12 @@ public class LoadBalancedSdfApiService implements SdfApiService {
@Override
public byte[] encryptByTMK(byte[] data) {
byte[] pad = PaddingUtil.PKCS7Padding(data);
return apply(s -> s.getSdfApiAdapter().symEncrypt(s.getSessionHandle(), s.getKeyHandle(), AlgId.SGD_SM4_CBC, CryptoConst.iv, pad));
return apply(s -> s.getSdfApiAdapter().symEncrypt(s.getSessionHandle(), s.getKeyHandle(), AlgId.SGD_SM4_CBC, CryptoConst.iv(), pad));
}
@Override
public byte[] decryptByTMK(byte[] data) {
byte[] decrypt = apply(s -> s.getSdfApiAdapter().symDecrypt(s.getSessionHandle(), s.getKeyHandle(), AlgId.SGD_SM4_CBC, CryptoConst.iv, data));
byte[] decrypt = apply(s -> s.getSdfApiAdapter().symDecrypt(s.getSessionHandle(), s.getKeyHandle(), AlgId.SGD_SM4_CBC, CryptoConst.iv(), data));
if (log.isDebugEnabled()) {
log.debug("decryptByTMK res: {}", CodecUtils.encodeHex(decrypt));
}
@@ -23,12 +23,13 @@ public class SymKeyTest extends BaseTest {
@Test
public void testEncrypt() {
Long keyId = 1869562427636801538L;
Long keyId = 1869666111835049985L;
SymEncryptReq symEncryptReq = new SymEncryptReq();
symEncryptReq.setKeyId(keyId);
symEncryptReq.setPlainData(CodecUtils.encodeBase64(plain.getBytes()));
symEncryptReq.setMode(AlgMode.ECB);
symEncryptReq.setIv(CodecUtils.encodeBase64(iv));
symEncryptReq.setMode(AlgMode.CBC);
SymEncryptResp symEncryptResp = execute("/sym/encrypt", symEncryptReq, SymEncryptResp.class);
log.info("SymEncryptResp: {}", symEncryptResp);
@@ -40,12 +41,13 @@ public class SymKeyTest extends BaseTest {
SymDecryptReq decryptReq = new SymDecryptReq();
decryptReq.setKeyId(keyId);
decryptReq.setKeyIndex(symEncryptResp.getKeyIndex());
decryptReq.setMode(AlgMode.ECB);
decryptReq.setIv(CodecUtils.encodeBase64(iv));
decryptReq.setMode(AlgMode.CBC);
decryptReq.setCipherData(symEncryptResp.getCipherData());
SymDecryptResp decryptResp = execute("/sym/decrypt", decryptReq, SymDecryptResp.class);
log.info("SymDecryptResp: {}", decryptResp);
Assertions.assertNotNull(decryptResp);
String calPlain = new String(CodecUtils.decodeBase64(decryptResp.getPlainData()));
log.info("SymDecryptResp: {}, {}", calPlain, decryptResp);
Assertions. assertNotNull(decryptResp);
Assertions.assertEquals(plain, calPlain);
@@ -149,13 +149,13 @@ public class SingleSdfApiService implements SdfApiService {
public byte[] encryptByTMK(byte[] data) {
checkKey();
byte[] pad = PaddingUtil.PKCS7Padding(data);
return sdfApiAdapter.symEncrypt(sessionHandle, tmkHandle, AlgId.SGD_SM4_CBC, CryptoConst.iv, pad);
return sdfApiAdapter.symEncrypt(sessionHandle, tmkHandle, AlgId.SGD_SM4_CBC, CryptoConst.iv(), pad);
}
@Override
public byte[] decryptByTMK(byte[] data) {
checkKey();
byte[] decrypt = sdfApiAdapter.symDecrypt(sessionHandle, tmkHandle, AlgId.SGD_SM4_CBC, CryptoConst.iv, data);
byte[] decrypt = sdfApiAdapter.symDecrypt(sessionHandle, tmkHandle, AlgId.SGD_SM4_CBC, CryptoConst.iv(), data);
return PaddingUtil.PKCS7Unpadding(decrypt);
}