sdf api
This commit is contained in:
@@ -2,10 +2,18 @@ package com.sunyard.chsm.pool;
|
||||
|
||||
import com.sunyard.chsm.sdf.SdfApiService;
|
||||
import com.sunyard.chsm.sdf.context.AlgId;
|
||||
import com.sunyard.chsm.sdf.model.EccCipher;
|
||||
import com.sunyard.chsm.sdf.model.EccKey;
|
||||
import com.sunyard.chsm.sdf.model.EccPriKey;
|
||||
import com.sunyard.chsm.sdf.model.EccPubKey;
|
||||
import com.sunyard.chsm.sdf.model.EccSignature;
|
||||
import com.sunyard.chsm.sdf.util.PaddingUtil;
|
||||
import com.sunyard.chsm.utils.gm.BCECUtils;
|
||||
import com.sunyard.chsm.utils.gm.BCSM3Utils;
|
||||
import com.sunyard.chsm.utils.gm.SM2PreprocessSigner;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bouncycastle.crypto.CipherParameters;
|
||||
import org.bouncycastle.crypto.params.ParametersWithID;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -63,6 +71,59 @@ public class LoadBalancedSdfApiService implements SdfApiService {
|
||||
return apply(s -> s.getSdfApiAdapter().generateKeyPairECC(s.getSessionHandle(), AlgId.SGD_SM2_1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public EccSignature externalSign(byte[] privateKey, byte[] pucData) {
|
||||
return externalSignWithId(privateKey, pucData, new byte[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EccSignature externalSignWithId(byte[] privateKey, byte[] pucData, byte[] userId) {
|
||||
EccPriKey sdfEccPrivateKey = EccPriKey.fromBytes(privateKey);
|
||||
SM2PreprocessSigner signer = new SM2PreprocessSigner();
|
||||
CipherParameters keyParameters = BCECUtils.createECPrivateKeyParameters(sdfEccPrivateKey.getD());
|
||||
if (userId != null && userId.length > 0) {
|
||||
keyParameters = new ParametersWithID(keyParameters, userId);
|
||||
}
|
||||
signer.init(true, keyParameters);
|
||||
byte[] preHash = signer.preprocess(pucData);
|
||||
|
||||
return apply(s -> s.getSdfApiAdapter().externalSignECC(s.getSessionHandle(), sdfEccPrivateKey, preHash));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean externalVerifyWithId(byte[] publicKey, byte[] pubData, byte[] signData, byte[] userId) {
|
||||
EccPubKey eccPublicKey = EccPubKey.fromBytes(publicKey);
|
||||
CipherParameters parameters = BCECUtils.createECPublicKeyParameters(eccPublicKey.getX(), eccPublicKey.getY());
|
||||
if (userId != null && userId.length > 0) {
|
||||
parameters = new ParametersWithID(parameters, userId);
|
||||
}
|
||||
SM2PreprocessSigner signer = new SM2PreprocessSigner();
|
||||
signer.init(false, parameters);
|
||||
byte[] preHash = signer.preprocess(pubData);
|
||||
EccSignature eccSignature = EccSignature.fromBytes(signData);
|
||||
|
||||
return apply(s -> s.getSdfApiAdapter().externalVerifyECC(s.getSessionHandle(),
|
||||
eccPublicKey, preHash, eccSignature));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean externalVerify(byte[] publicKey, byte[] pubData, byte[] signData) {
|
||||
return externalVerifyWithId(publicKey, pubData, signData, new byte[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EccCipher externalEncrypt(byte[] publicKey, byte[] pucData) {
|
||||
EccPubKey eccPublicKey = EccPubKey.fromBytes(publicKey);
|
||||
return apply(s -> s.getSdfApiAdapter().externalEncryptECC(s.getSessionHandle(), eccPublicKey, pucData));
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] externalDecrypt(byte[] privateKey, byte[] encData) {
|
||||
EccPriKey sdfEccPrivateKey = EccPriKey.fromBytes(privateKey);
|
||||
EccCipher eccCipher = EccCipher.fromBytes(encData);
|
||||
return apply(s -> s.getSdfApiAdapter().externalDecryptECC(s.getSessionHandle(), sdfEccPrivateKey, eccCipher));
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] calculateMAC(byte[] symKey, byte[] pucIv, byte[] pucData) {
|
||||
return apply(s -> {
|
||||
|
||||
Reference in New Issue
Block a user