mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
identity base test
This commit is contained in:
1 parent
755ddc2129
commit
9827414ac5
4 files changed
+107
-8
No files matched your search
@@ -0,0 +1,79 @@
|
||||
package minsecurity.identity;
|
||||
|
||||
import minsecurity.Common;
|
||||
import minsecurity.certificate.cert.CertUtils;
|
||||
import minsecurity.certificate.cert.Certificate;
|
||||
import minsecurity.crypto.TestSM2;
|
||||
import minsecurity.crypto.sm2.SM2Base;
|
||||
import minsecurity.crypto.sm2.SM2PrivateKey;
|
||||
import minsecurity.crypto.sm2.SM2PublicKey;
|
||||
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
|
||||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
|
||||
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 15:24 2021/03/09
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class TestIdentity {
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TestIdentity.class);
|
||||
|
||||
|
||||
@Test
|
||||
public void testIdentity1() throws Exception {
|
||||
AsymmetricCipherKeyPair keyPair = SM2Base.generateKeyPairParameter();
|
||||
ECPrivateKeyParameters priKey = (ECPrivateKeyParameters) keyPair.getPrivate();
|
||||
ECPublicKeyParameters pubKey = (ECPublicKeyParameters) keyPair.getPublic();
|
||||
byte[] d = priKey.getD().toByteArray();
|
||||
// d = Arrays.copyOf(d,32);
|
||||
byte[] x = pubKey.getQ().getAffineXCoord().getEncoded();
|
||||
byte[] y = pubKey.getQ().getAffineYCoord().getEncoded();
|
||||
logger.debug("d.len = {}, x.len = {}, y.len = {}",d.length, x.length, y.length);
|
||||
|
||||
// BigInteger bigInteger = priKey.getD();
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey(d);
|
||||
SM2PublicKey sm2PublicKey = new SM2PublicKey(x,y);
|
||||
KeyParam keyParam = new KeyParam(Common.SM2, Common.SM3withSM2);
|
||||
Identity identity = new Identity("root",keyParam,sm2PrivateKey,null,sm2PublicKey, "123456", null);
|
||||
Certificate certificate = new Certificate(1, 1, sm2PublicKey, null,
|
||||
Common.SM3withSM2, Common.SM2, "root", "root",
|
||||
System.currentTimeMillis() - 1000, System.currentTimeMillis() + 5000,
|
||||
Common.CertSign, true, System.currentTimeMillis());
|
||||
CertUtils.signCert(certificate, sm2PrivateKey);
|
||||
identity.setCert(certificate);
|
||||
|
||||
String text = "2020-03-09";
|
||||
// test sign and verify
|
||||
byte[] signature = identity.sign(text.getBytes());
|
||||
boolean flag = identity.verify(text.getBytes(), signature);
|
||||
logger.debug("identity verify: {}", flag);
|
||||
assertTrue(flag);
|
||||
|
||||
|
||||
// test enc and dec
|
||||
byte[] cipher = identity.encrypt(text.getBytes());
|
||||
byte[] bytesOfDec = identity.decrypt(cipher);
|
||||
assertArrayEquals(bytesOfDec, text.getBytes());
|
||||
|
||||
|
||||
// test dump and load
|
||||
byte[] bytesOfDump = identity.dump(identity.getPasswd());
|
||||
Identity idFromBytes = Identity.load(bytesOfDump, identity.getPasswd());
|
||||
assertEquals(ByteUtils.toHexString(identity.getPrikey().getBytes()), ByteUtils.toHexString(idFromBytes.getPrikey().getBytes()));
|
||||
assertEquals(ByteUtils.toHexString(identity.getPubkey().getBytes()), ByteUtils.toHexString(idFromBytes.getPubkey().getBytes()));
|
||||
// test dump and load without passwd
|
||||
bytesOfDump = identity.dump(null);
|
||||
idFromBytes = Identity.load(bytesOfDump, null);
|
||||
assertEquals(ByteUtils.toHexString(identity.getPrikey().getBytes()), ByteUtils.toHexString(idFromBytes.getPrikey().getBytes()));
|
||||
assertEquals(ByteUtils.toHexString(identity.getPubkey().getBytes()), ByteUtils.toHexString(idFromBytes.getPubkey().getBytes()));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user