mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
使用mapDB持久化identity(50%)
This commit is contained in:
1 parent
9827414ac5
commit
f693a7a120
5 files changed
+194
-4
No files matched your search
@@ -20,6 +20,7 @@ import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
@@ -43,10 +44,12 @@ public class Identity {
|
||||
private PublicKeyInterface Pubkey;
|
||||
private String Passwd;
|
||||
private Certificate Cert;
|
||||
private boolean IsDefault;
|
||||
|
||||
public Identity(String name, KeyParam keyParam,
|
||||
PrivateKeyInterface prikey, byte[] prikeyRawByte,
|
||||
PublicKeyInterface pubkey, String passwd, Certificate cert) {
|
||||
PublicKeyInterface pubkey, String passwd, Certificate cert,
|
||||
boolean isDefault) {
|
||||
Name = name;
|
||||
KeyParam = keyParam;
|
||||
Prikey = prikey;
|
||||
@@ -54,6 +57,7 @@ public class Identity {
|
||||
Pubkey = pubkey;
|
||||
Passwd = passwd;
|
||||
Cert = cert;
|
||||
IsDefault = isDefault;
|
||||
}
|
||||
|
||||
|
||||
@@ -240,16 +244,27 @@ public class Identity {
|
||||
Cert = cert;
|
||||
}
|
||||
|
||||
public boolean isDefault() {
|
||||
return IsDefault;
|
||||
}
|
||||
|
||||
public void setDefault(boolean aDefault) {
|
||||
IsDefault = aDefault;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Identity{" +
|
||||
"Name='" + Name + '\'' +
|
||||
", KeyParam=" + KeyParam +
|
||||
", Prikey=" + Prikey +
|
||||
", KeyParam.signAlgo=" + KeyParam.SignatureAlgorithm +
|
||||
", KeyParam.pubAlgo=" + KeyParam.PublicKeyAlgorithm +
|
||||
", Prikey=" + ByteUtils.toHexString(Prikey.getBytes()) +
|
||||
", pubKey=" + ByteUtils.toHexString(Pubkey.getBytes()) +
|
||||
", PrikeyRawByte=" + (PrikeyRawByte == null ? "null":ByteUtils.toHexString(PrikeyRawByte)) +
|
||||
", Pubkey=" + Pubkey +
|
||||
", Passwd='" + Passwd + '\'' +
|
||||
", Cert=" + Cert +
|
||||
", isDefault=" + IsDefault +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package minsecurity.identity.persist;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import minsecurity.Common;
|
||||
import minsecurity.certificate.cert.CertException;
|
||||
import minsecurity.certificate.cert.CertUtils;
|
||||
import minsecurity.certificate.cert.Certificate;
|
||||
import minsecurity.crypto.sm2.SM2PrivateKey;
|
||||
import minsecurity.crypto.sm2.SM2PublicKey;
|
||||
import minsecurity.identity.Identity;
|
||||
import minsecurity.identity.KeyParam;
|
||||
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.mapdb.DataInput2;
|
||||
import org.mapdb.DataOutput2;
|
||||
import org.mapdb.Serializer;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import java.io.IOException;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.NoSuchProviderException;
|
||||
|
||||
/*
|
||||
* @Author: hongyu guo
|
||||
* @Description: mapDB需要自定义序列化、反序列化方法
|
||||
* @Version: 1.0.0
|
||||
* @Date: 20:50 2021/03/09
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class IdentitySerializer implements Serializer<Identity> {
|
||||
|
||||
|
||||
@Override
|
||||
public void serialize(@NotNull DataOutput2 dataOutput2, @NotNull Identity identity) throws IOException {
|
||||
String name = identity.getName();
|
||||
String pub = ByteUtils.toHexString(identity.getPubkey().getBytes());
|
||||
String priv = ByteUtils.toHexString(identity.getPrikey().getBytes());
|
||||
int pubAlgo = identity.getKeyParam().PublicKeyAlgorithm;
|
||||
int signAlgo = identity.getKeyParam().SignatureAlgorithm;
|
||||
String passwd = identity.getPasswd();
|
||||
String cert = "";
|
||||
try {
|
||||
cert = CertUtils.toPem(identity.getCert(),null, Common.SM4CBC);
|
||||
} catch (CertException | NoSuchPaddingException | InvalidAlgorithmParameterException |
|
||||
NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException |
|
||||
NoSuchProviderException | InvalidKeyException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
boolean isDefault = identity.isDefault();
|
||||
String prikeyRawByte = identity.getPrikeyRawByte()== null ?
|
||||
"" :ByteUtils.toHexString(identity.getPrikeyRawByte());
|
||||
|
||||
|
||||
dataOutput2.writeUTF(name);
|
||||
dataOutput2.writeUTF(pub);
|
||||
dataOutput2.writeUTF(priv);
|
||||
dataOutput2.writeInt(pubAlgo);
|
||||
dataOutput2.writeInt(signAlgo);
|
||||
dataOutput2.writeUTF(passwd);
|
||||
dataOutput2.writeUTF(cert);
|
||||
dataOutput2.writeBoolean(isDefault);
|
||||
dataOutput2.writeUTF(prikeyRawByte);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Identity deserialize(@NotNull DataInput2 dataInput2, int i) throws IOException {
|
||||
String name = dataInput2.readUTF();
|
||||
byte[] pub = ByteUtils.fromHexString(dataInput2.readUTF());
|
||||
byte[] priv = ByteUtils.fromHexString(dataInput2.readUTF());
|
||||
int pubAlgo = dataInput2.readInt();
|
||||
int signAlgo = dataInput2.readInt();
|
||||
String passwd = dataInput2.readUTF();
|
||||
String certString = dataInput2.readUTF();
|
||||
Certificate certificate = null;
|
||||
try {
|
||||
certificate = CertUtils.fromPem(certString, null, Common.SM4CBC);
|
||||
} catch (CertException | BadPaddingException | NoSuchPaddingException |
|
||||
InvalidAlgorithmParameterException | NoSuchAlgorithmException |
|
||||
IllegalBlockSizeException | NoSuchProviderException | InvalidKeyException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
boolean isDefault = dataInput2.readBoolean();
|
||||
byte[] prikeyRawByte = dataInput2.readUTF().equals("") ?
|
||||
null : ByteUtils.fromHexString(dataInput2.readUTF());
|
||||
KeyParam keyParam = new KeyParam(pubAlgo, signAlgo);
|
||||
SM2PrivateKey sm2PrivateKey = new SM2PrivateKey();
|
||||
sm2PrivateKey.setBytes(priv);
|
||||
SM2PublicKey sm2PublicKey = new SM2PublicKey();
|
||||
sm2PublicKey.setBytes(pub);
|
||||
return new Identity(name,keyParam, sm2PrivateKey,prikeyRawByte, sm2PublicKey, passwd, certificate, isDefault);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user