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
@@ -13,6 +13,8 @@ import minsecurity.crypto.sm2.SM2PrivateKey;
|
||||
import minsecurity.crypto.sm2.SM2PublicKey;
|
||||
import org.bouncycastle.crypto.CryptoException;
|
||||
import org.bouncycastle.crypto.InvalidCipherTextException;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
@@ -22,6 +24,7 @@ import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
|
||||
/*
|
||||
@@ -32,7 +35,7 @@ import java.util.Base64;
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class Identity {
|
||||
|
||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Identity.class);
|
||||
private String Name;
|
||||
private KeyParam KeyParam;
|
||||
private PrivateKeyInterface Prikey;
|
||||
@@ -108,6 +111,7 @@ public class Identity {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
InnerIdentity innerIdentity = IdentityUtil.parseIdentityToInner(this);
|
||||
byte[] bytesOfInnerIdentity = mapper.writeValueAsBytes(innerIdentity);
|
||||
// logger.debug(ByteUtils.toHexString(bytesOfInnerIdentity));
|
||||
if(passwd != null && passwd.length() != 0) {
|
||||
byte[] sm4key = KeyUtils.get16bytePasswd(passwd.getBytes());
|
||||
byte[] cipher = SM4.encrypt_CBC_Padding(sm4key, new byte[16], bytesOfInnerIdentity);
|
||||
@@ -119,7 +123,7 @@ public class Identity {
|
||||
public static Identity load(byte[] data, String passwd)
|
||||
throws BadPaddingException, NoSuchPaddingException, InvalidAlgorithmParameterException,
|
||||
NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchProviderException,
|
||||
InvalidKeyException, IOException, CertException {
|
||||
InvalidKeyException, CertException {
|
||||
byte[] bytesOfIdentity = Base64.getDecoder().decode(data);
|
||||
byte[] plain;
|
||||
if(passwd != null && passwd.length() != 0){
|
||||
@@ -129,7 +133,13 @@ public class Identity {
|
||||
plain = bytesOfIdentity;
|
||||
}
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
InnerIdentity innerIdentity = mapper.readValue(plain, InnerIdentity.class);
|
||||
InnerIdentity innerIdentity = null;
|
||||
// logger.debug(ByteUtils.toHexString(plain));
|
||||
try {
|
||||
innerIdentity = mapper.readValue(plain, InnerIdentity.class);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return IdentityUtil.parseInnerToIdentity(innerIdentity);
|
||||
}
|
||||
// TODO: passwd ???
|
||||
@@ -230,6 +240,16 @@ public class Identity {
|
||||
Cert = cert;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Identity{" +
|
||||
"Name='" + Name + '\'' +
|
||||
", KeyParam=" + KeyParam +
|
||||
", Prikey=" + Prikey +
|
||||
", PrikeyRawByte=" + (PrikeyRawByte == null ? "null":ByteUtils.toHexString(PrikeyRawByte)) +
|
||||
", Pubkey=" + Pubkey +
|
||||
", Passwd='" + Passwd + '\'' +
|
||||
", Cert=" + Cert +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ public class IdentityUtil {
|
||||
innerIdentity.setPrikey(identity.getPrikey().getBytes());
|
||||
}
|
||||
if(identity.getPubkey() != null){
|
||||
innerIdentity.setPrikey(identity.getPubkey().getBytes());
|
||||
innerIdentity.setPubkey(identity.getPubkey().getBytes());
|
||||
}
|
||||
innerIdentity.setPasswd(identity.getPasswd());
|
||||
innerIdentity.setCert(CertUtils.toPem(identity.getCert(), identity.getPasswd().getBytes(), Common.SM4CBC));
|
||||
|
||||
@@ -85,8 +85,8 @@ public class InnerIdentity {
|
||||
return "InnerIdentity{" +
|
||||
"Name='" + Name + '\'' +
|
||||
", KeyParam=" + KeyParam +
|
||||
", Prikey=" + ByteUtils.toHexString(Prikey) +
|
||||
", Pubkey=" + ByteUtils.toHexString(Pubkey) +
|
||||
", Prikey=" + (Prikey == null ? "null" : ByteUtils.toHexString(Prikey)) +
|
||||
", Pubkey=" + (Pubkey == null ? "null" : ByteUtils.toHexString(Pubkey)) +
|
||||
", Passwd='" + Passwd + '\'' +
|
||||
", Cert='" + Cert + '\'' +
|
||||
'}';
|
||||
|
||||
Reference in new issue
Block a user