use of cn.hutool.crypto.asymmetric.SM2 in project hutool by looly.
the class PemUtilTest method readECPrivateKeyTest2.
@Test
@Ignore
public void readECPrivateKeyTest2() {
// https://gitee.com/loolly/hutool/issues/I37Z75
byte[] d = PemUtil.readPem(FileUtil.getInputStream("d:/test/keys/priv.key"));
byte[] publicKey = PemUtil.readPem(FileUtil.getInputStream("d:/test/keys/pub.key"));
SM2 sm2 = new SM2(d, publicKey);
sm2.usePlainEncoding();
String content = "我是Hanley.";
byte[] sign = sm2.sign(StrUtil.utf8Bytes(content));
boolean verify = sm2.verify(StrUtil.utf8Bytes(content), sign);
Assert.assertTrue(verify);
}
use of cn.hutool.crypto.asymmetric.SM2 in project hutool by looly.
the class PemUtilTest method readECPrivateKeyTest.
@Test
public void readECPrivateKeyTest() {
PrivateKey privateKey = PemUtil.readSm2PemPrivateKey(ResourceUtil.getStream("test_ec_private_key.pem"));
SM2 sm2 = new SM2(privateKey, null);
sm2.usePlainEncoding();
// 需要签名的明文,得到明文对应的字节数组
byte[] dataBytes = "我是一段测试aaaa".getBytes(StandardCharsets.UTF_8);
byte[] sign = sm2.sign(dataBytes, null);
// 64位签名
Assert.assertEquals(64, sign.length);
}
use of cn.hutool.crypto.asymmetric.SM2 in project hutool by looly.
the class SM2Test method sm2SignAndVerifyUseKeyTest2.
@Test
public void sm2SignAndVerifyUseKeyTest2() {
String content = "我是Hanley.";
KeyPair pair = SecureUtil.generateKeyPair("SM2");
final SM2 sm2 = new //
SM2(//
HexUtil.encodeHexStr(pair.getPrivate().getEncoded()), //
HexUtil.encodeHexStr(pair.getPublic().getEncoded()));
byte[] sign = sm2.sign(content.getBytes(StandardCharsets.UTF_8));
boolean verify = sm2.verify(content.getBytes(StandardCharsets.UTF_8), sign);
Assert.assertTrue(verify);
}
use of cn.hutool.crypto.asymmetric.SM2 in project hutool by looly.
the class SM2Test method sm2SignTest.
@Test
public void sm2SignTest() {
// 需要签名的明文,得到明文对应的字节数组
byte[] dataBytes = "我是一段测试aaaa".getBytes(StandardCharsets.UTF_8);
// 指定的私钥
String privateKeyHex = "1ebf8b341c695ee456fd1a41b82645724bc25d79935437d30e7e4b0a554baa5e";
final SM2 sm2 = new SM2(privateKeyHex, null, null);
sm2.usePlainEncoding();
byte[] sign = sm2.sign(dataBytes, null);
// 64位签名
Assert.assertEquals(64, sign.length);
}
use of cn.hutool.crypto.asymmetric.SM2 in project hutool by looly.
the class SM2Test method sm2Base64Test.
@Test
public void sm2Base64Test() {
String textBase = "我是一段特别长的测试";
StringBuilder text = new StringBuilder();
for (int i = 0; i < 100; i++) {
text.append(textBase);
}
SM2 sm2 = new SM2();
// 公钥加密,私钥解密
String encryptStr = sm2.encryptBase64(text.toString(), KeyType.PublicKey);
String decryptStr = StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));
Assert.assertEquals(text.toString(), decryptStr);
// 测试自定义密钥后是否生效
PrivateKey privateKey = sm2.getPrivateKey();
PublicKey publicKey = sm2.getPublicKey();
sm2 = SmUtil.sm2();
sm2.setPrivateKey(privateKey);
sm2.setPublicKey(publicKey);
String decryptStr2 = StrUtil.utf8Str(sm2.decrypt(encryptStr, KeyType.PrivateKey));
Assert.assertEquals(text.toString(), decryptStr2);
}
Aggregations