签名 @param M 签名信息 @param IDA 签名方唯一标识 @param keyPair 签名方密钥对 @return 签名
(String M, String IDA, SM2KeyPair keyPair)
| 489 | * @return 签名 |
| 490 | */ |
| 491 | public Signature sign(String M, String IDA, SM2KeyPair keyPair) { |
| 492 | byte[] ZA = ZA(IDA, keyPair.getPublicKey()); |
| 493 | byte[] M_ = join(ZA, M.getBytes()); |
| 494 | BigInteger e = new BigInteger(1, sm3hash(M_)); |
| 495 | // BigInteger k = new BigInteger( |
| 496 | // "6CB28D99 385C175C 94F94E93 4817663F C176D925 DD72B727 260DBAAE |
| 497 | // 1FB2F96F".replace(" ", ""), 16); |
| 498 | BigInteger k; |
| 499 | BigInteger r; |
| 500 | do { |
| 501 | k = random(n); |
| 502 | ECPoint p1 = G.multiply(k).normalize(); |
| 503 | BigInteger x1 = p1.getXCoord().toBigInteger(); |
| 504 | r = e.add(x1); |
| 505 | r = r.mod(n); |
| 506 | } while (r.equals(BigInteger.ZERO) || r.add(k).equals(n)); |
| 507 | |
| 508 | BigInteger s = ((keyPair.getPrivateKey().add(BigInteger.ONE).modInverse(n)) |
| 509 | .multiply((k.subtract(r.multiply(keyPair.getPrivateKey()))).mod(n))).mod(n); |
| 510 | |
| 511 | return new Signature(r, s); |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * 验签 |
nothing calls this directly
no test coverage detected