mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix getD
This commit is contained in:
@@ -19,7 +19,9 @@ import org.bouncycastle.crypto.signers.DSAEncoding;
|
||||
import org.bouncycastle.crypto.signers.PlainDSAEncoding;
|
||||
import org.bouncycastle.crypto.signers.SM2Signer;
|
||||
import org.bouncycastle.crypto.signers.StandardDSAEncoding;
|
||||
import org.bouncycastle.util.BigIntegers;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
|
||||
@@ -519,7 +521,27 @@ public class SM2 extends AbstractAsymmetricCrypto<SM2> {
|
||||
* @since 5.5.9
|
||||
*/
|
||||
public byte[] getD() {
|
||||
return this.privateKeyParams.getD().toByteArray();
|
||||
return BigIntegers.asUnsignedByteArray(getDBigInteger());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得私钥D值(编码后的私钥)
|
||||
*
|
||||
* @return D值
|
||||
* @since 5.7.17
|
||||
*/
|
||||
public String getDHex() {
|
||||
return getDBigInteger().toString(16);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得私钥D值
|
||||
*
|
||||
* @return D值
|
||||
* @since 5.7.17
|
||||
*/
|
||||
public BigInteger getDBigInteger() {
|
||||
return this.privateKeyParams.getD();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -311,4 +311,14 @@ public class SM2Test {
|
||||
byte[] dec = sm2.decrypt(data, KeyType.PrivateKey);
|
||||
Assert.assertArrayEquals(dec, src.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dLengthTest(){
|
||||
final SM2 sm2 = SmUtil.sm2();
|
||||
Assert.assertEquals(64, sm2.getDHex().length());
|
||||
Assert.assertEquals(32, sm2.getD().length);
|
||||
|
||||
// 04占位一个字节
|
||||
Assert.assertEquals(65, sm2.getQ(false).length);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user