Commit d7d1bfb6 authored by Schudel, MJ (Michel)'s avatar Schudel, MJ (Michel)

added key service based on elliptic curve cryptography

parent 716f219c
package nl.craftsmen.blockchain.craftscoinnode.transaction;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import nl.craftsmen.blockchain.craftscoinnode.util.InstanceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
@Component
public class KeyRepository {
private InstanceInfo instanceInfo;
@Autowired
public KeyRepository(InstanceInfo instanceInfo) {
this.instanceInfo = instanceInfo;
}
public void saveKeys(KeyPair keyPair) {
try {
Keys keys = new Keys(new String(Base64.getEncoder().encode(keyPair.getPrivate().getEncoded())), new String(Base64.getEncoder().encode(keyPair.getPublic().getEncoded())));
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
File file = createKeyPairFileForThisNode();
objectMapper.writeValue(file, keys);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public KeyPair loadKeys() {
File file = createKeyPairFileForThisNode();
if (file.exists()) {
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
Keys keys = objectMapper.readValue(file, Keys.class);
KeyFactory kf = KeyFactory.getInstance("EC");
byte[] bytePrivateKey = Base64.getDecoder().decode(keys.getPrivateKey().getBytes());
PKCS8EncodedKeySpec pkcs8privateKey = new PKCS8EncodedKeySpec(bytePrivateKey);
PrivateKey privateKey = kf.generatePrivate(pkcs8privateKey);
byte[] bytePublicKey = Base64.getDecoder().decode(keys.getPublicKey().getBytes());
X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(bytePublicKey);
PublicKey publicKey = kf.generatePublic(X509publicKey);
return new KeyPair(publicKey, privateKey);
} catch (GeneralSecurityException | IOException e) {
throw new RuntimeException(e);
}
} else {
return null;
}
}
private File createKeyPairFileForThisNode() {
return new File(System.getProperty("user.dir"), createKeyPairFileName(instanceInfo.getNode()));
}
private String createKeyPairFileName(String node) {
return node.replace(".", "").replace(":", "") + "-keypair.json";
}
@JsonPropertyOrder(alphabetic = true)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
public static class Keys {
private String privateKey;
private String publicKey;
//default public constructor, needed for json deserialization
@SuppressWarnings({"unused", "WeakerAccess"})
public Keys() {
}
Keys(String privateKey, String publicKey) {
this.privateKey = privateKey;
this.publicKey = publicKey;
}
String getPrivateKey() {
return privateKey;
}
String getPublicKey() {
return publicKey;
}
}
}
package nl.craftsmen.blockchain.craftscoinnode.transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
@Component
public class KeyService {
private KeyRepository keyRepository;
private KeyPair keyPair;
@Autowired
public KeyService(KeyRepository keyRepository) {
this.keyRepository = keyRepository;
}
@PostConstruct
public void init() throws NoSuchAlgorithmException {
keyPair = keyRepository.loadKeys();
if (keyPair == null) {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
keyPair = keyGen.generateKeyPair();
keyRepository.saveKeys(keyPair);
}
}
public KeyPair getKeyPair() {
return keyPair;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment