SM2EnhancedUtil.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package com.ruoyi.common.utils;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Component;
  4. import javax.annotation.PostConstruct;
  5. import java.security.KeyPair;
  6. import java.security.PrivateKey;
  7. import java.security.PublicKey;
  8. /**
  9. * 增强版SM2工具类
  10. */
  11. @Component
  12. public class SM2EnhancedUtil {
  13. @Value("${sm2.private-key}")
  14. private String configPrivateKey;
  15. @Value("${sm2.public-key}")
  16. private String configPublicKey;
  17. private PrivateKey privateKey;
  18. private PublicKey publicKey;
  19. private KeyPair keyPair;
  20. @PostConstruct
  21. public void init() {
  22. // 如果配置了密钥,则使用配置的密钥
  23. if (configPrivateKey != null && !configPrivateKey.trim().isEmpty() &&
  24. configPublicKey != null && !configPublicKey.trim().isEmpty()) {
  25. this.privateKey = SM2Util.loadPrivateKey(configPrivateKey);
  26. this.publicKey = SM2Util.loadPublicKey(configPublicKey);
  27. this.keyPair = new KeyPair(publicKey, privateKey);
  28. } else {
  29. // 否则生成新的密钥对
  30. this.keyPair = SM2Util.generateKeyPair();
  31. this.privateKey = keyPair.getPrivate();
  32. this.publicKey = keyPair.getPublic();
  33. // 输出生成的密钥(仅用于开发环境)
  34. System.out.println("生成的SM2公钥: " + getPublicKeyBase64());
  35. System.out.println("生成的SM2私钥: " + getPrivateKeyBase64());
  36. }
  37. }
  38. /**
  39. * 使用配置的私钥签名
  40. */
  41. public String sign(String data) {
  42. return SM2Util.sign(data.getBytes(), privateKey);
  43. }
  44. /**
  45. * 使用配置的公钥验签
  46. */
  47. public boolean verify(String data, String signature) {
  48. return SM2Util.verify(data.getBytes(), signature, publicKey);
  49. }
  50. /**
  51. * 使用配置的私钥签名(字节数组)
  52. */
  53. public String sign(byte[] data) {
  54. return SM2Util.sign(data, privateKey);
  55. }
  56. /**
  57. * 使用配置的公钥验签(字节数组)
  58. */
  59. public boolean verify(byte[] data, String signature) {
  60. return SM2Util.verify(data, signature, publicKey);
  61. }
  62. /**
  63. * 获取当前使用的私钥(Base64)
  64. */
  65. public String getPrivateKeyBase64() {
  66. return SM2Util.privateKeyToString(privateKey);
  67. }
  68. /**
  69. * 获取当前使用的公钥(Base64)
  70. */
  71. public String getPublicKeyBase64() {
  72. return SM2Util.publicKeyToString(publicKey);
  73. }
  74. /**
  75. * 获取密钥对
  76. */
  77. public KeyPair getKeyPair() {
  78. return keyPair;
  79. }
  80. /**
  81. * 生成新的密钥对并更新当前实例
  82. */
  83. public void generateNewKeyPair() {
  84. this.keyPair = SM2Util.generateKeyPair();
  85. this.privateKey = keyPair.getPrivate();
  86. this.publicKey = keyPair.getPublic();
  87. }
  88. /**
  89. * 验证工具类是否正常工作
  90. */
  91. public boolean selfCheck() {
  92. try {
  93. String testData = "SM2签名测试数据";
  94. String signature = sign(testData);
  95. return verify(testData, signature);
  96. } catch (Exception e) {
  97. return false;
  98. }
  99. }
  100. }