```
/**
* secp256k1 加解密
* <see>https://www.xfqiao.com/api/android-zh/java/security/KeyPairGenerator.html</see>
* <see>https://docs.oracle.com/javase/8/docs/api/java/security/KeyPairGenerator.html</see>
* @author
* @date 2022年11月23日21:37:46
*/
public class KeyPairUtil {
/**
* 生成 公钥 私钥 对
* @param stdName eg: secp256k1
* @return
* @throws NoSuchAlgorithmException
* @throws InvalidAlgorithmParameterException
*/
public static KeyPair createKeyPairGenerator(String stdName) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
// 生成秘钥,在实际业务中,应该加载秘钥
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec m = new ECGenParameterSpec(stdName);
kpg.initialize(m);
KeyPair keyPair = kpg.generateKeyPair();
return keyPair;
}
/**
* 签名
* @param algorithm
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] sign(String algorithm, byte[] data, PrivateKey key) throws Exception {
Signature signer = Signature.getInstance(algorithm);
signer.initSign(key);
signer.update(data);
return (signer.sign());
}
/**
* 验签
* @param algorithm
* @param data
* @param key
* @param sig
* @return
* @throws Exception
*/
public static boolean verifySign(String algorithm, byte[] data, PublicKey key, byte[] sig) throws Exception {
Signature signer = Signature.getInstance(algorithm);
signer.initVerify(key);
signer.update(data);
return (signer.verify(sig));
}
}
```