diff options
author | Magdalena Grodzińska <mag.grodzinska@gmail.com> | 2020-01-13 20:56:01 +0100 |
---|---|---|
committer | Magdalena Grodzińska <mag.grodzinska@gmail.com> | 2020-01-13 20:56:01 +0100 |
commit | 81d1abbbad1f1e9c7774c7162b23fc804f7989a3 (patch) | |
tree | 95c9765b58b0c782a8dfc4684406adfb5263ccee | |
parent | cc901690a8e3e41a1958ae33d95462031fba8e89 (diff) |
Add reading keys from file
-rw-r--r-- | build.gradle | 18 | ||||
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java | 59 | ||||
-rw-r--r-- | src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java | 4 |
3 files changed, 75 insertions, 6 deletions
diff --git a/build.gradle b/build.gradle index c557cad..27279d4 100644 --- a/build.gradle +++ b/build.gradle @@ -65,6 +65,14 @@ ext.zonePath = { return System.getProperty("zonePath") ?: "/uw/violet07" } +ext.publicKeyFilename = { + return System.getProperty("publicKeyFilename") ?: "build/tmp/query_signer.pub" +} + +ext.privateKeyFilename = { + return System.getProperty("privateKeyFilename") ?: "build/tmp/query_signer" +} + repositories { // Use jcenter for resolving dependencies. // You can declare any Maven/Ivy/file repository here. @@ -114,7 +122,7 @@ task runAgent(type: JavaExec) { systemProperty 'UDUPServer.bufsize', UDUPServerBufsize() systemProperty 'Gossip.zone_strategy', zoneSelectionStrategy() systemProperty 'zone_path', zonePath() - systemProperty 'query_signer_hostname', querySignerHostname() + systemProperty 'public_key_file', publicKeyFilename() } task runClient(type: JavaExec) { @@ -142,4 +150,12 @@ task runQuerySigner(type: JavaExec) { classpath = sourceSets.main.runtimeClasspath main = 'pl.edu.mimuw.cloudatlas.querysigner.QuerySigner' systemProperty 'query_signer_hostname', querySignerHostname() + systemProperty 'public_key_file', publicKeyFilename() + systemProperty 'private_key_file', privateKeyFilename() +} + +task runKeyGenerator(type: JavaExec) { + classpath = sourceSets.main.runtimeClasspath + main = 'pl.edu.mimuw.cloudatlas.KeyGenerator' + systemProperty 'query_signer_hostname', querySignerHostname() } diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java b/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java index 8581280..8a474ac 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/KeyGenerator.java @@ -1,16 +1,30 @@ package pl.edu.mimuw.cloudatlas; +import java.io.*; import java.security.*; +import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; public class KeyGenerator { private final static int NUM_KEY_BITS = 1024; private final static String ENCRYPTION_ALGORITHM = "RSA"; // TODO - // TODO read key values from files - // TODO gradlew private static void printKeyToFile(byte[] keyValue, String fileName) { + System.out.println("\n BEGIN KEY"); + for (byte k : keyValue) { + System.out.print(k); + } + System.out.println("\n EOF KEY"); + String keyString = new String(keyValue); + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); + writer.write(keyString); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } } private static void generateKeys() throws NoSuchAlgorithmException { @@ -22,13 +36,48 @@ public class KeyGenerator { keyGenerator.initialize(NUM_KEY_BITS); KeyPair keyPair = keyGenerator.generateKeyPair(); + String publicKeyFile = System.getProperty("public_key_file"); + String privateKeyFile = System.getProperty("private_key_file"); + privateKey = keyPair.getPrivate(); - printKeyToFile(privateKey.getEncoded(), "query_signer"); + printKeyToFile(privateKey.getEncoded(), privateKeyFile); publicKey = keyPair.getPublic(); - printKeyToFile(publicKey.getEncoded(), "query_signer.pub"); + printKeyToFile(publicKey.getEncoded(), publicKeyFile); + } + + public static PublicKey getPublicKey(String key){ + try { + byte[] byteKey = Base64.getDecoder().decode(key.getBytes()); + X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey); + KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM); + return kf.generatePublic(X509publicKey); + } catch(Exception e) { + e.printStackTrace(); + } + return null; + } + + public static PrivateKey getPrivateKey(String key){ + try { + byte[] byteKey = Base64.getDecoder().decode(key.getBytes()); + X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey); + KeyFactory kf = KeyFactory.getInstance(ENCRYPTION_ALGORITHM); + return kf.generatePrivate(X509publicKey); + } catch(Exception e) { + e.printStackTrace(); + } + return null; + } + + public static String readKeyFromFile(String filename) throws IOException { + String key; + BufferedReader reader = new BufferedReader(new FileReader(filename)); + key = reader.readLine(); + reader.close(); + return key; } - public static void main() { + public static void main(String[] args) { try { generateKeys(); } catch (NoSuchAlgorithmException e) { diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java new file mode 100644 index 0000000..a09c823 --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/querysigner/QueryData.java @@ -0,0 +1,4 @@ +package pl.edu.mimuw.cloudatlas.querysigner; + +public class QueryData { +} |