Skip to content

Commit

Permalink
added a signing command. can now sign text from any selected key docu…
Browse files Browse the repository at this point in the history
…ment
  • Loading branch information
odds-get-evened committed Jan 26, 2022
1 parent 0ab9b89 commit c814d62
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 40 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ list : list all key documents.
peek : view details of a key document. `peek <# from list>`
sign : signs a string of text, and produces signature. `sign <some silly text here>`
use : designate currently used key document. `use <# from list>`
````

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.white5moke</groupId>
<artifactId>handeroffer</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/module-info.java

This file was deleted.

4 changes: 3 additions & 1 deletion src/main/java/org/white5moke/handoff/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.commons.codec.digest.DigestUtils;
import org.json.JSONObject;
import org.white5moke.handoff.client.Handoff;
import org.white5moke.handoff.client.HandoffClient;

import javax.crypto.*;
Expand Down Expand Up @@ -38,7 +39,8 @@ public App() throws Exception {
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static void a1() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException, InvalidKeySpecException, InvalidKeyException, SignatureException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
public static void a1() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, IOException,
InvalidKeySpecException, InvalidKeyException, SignatureException, NoSuchPaddingException {
final String SECP = "secp256r1";

KeyPairGenerator gen = KeyPairGenerator.getInstance("EC");
Expand Down
98 changes: 98 additions & 0 deletions src/main/java/org/white5moke/handoff/client/Handoff.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.white5moke.handoff.client;

import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.time.Instant;
import java.util.Base64;

public class Handoff {

private PrivateKey privateKey;

public Handoff() throws NoSuchAlgorithmException, InvalidKeySpecException, SignatureException, InvalidKeyException {
//signingKeys();
loop();
}

private void loop() throws NoSuchAlgorithmException, InvalidKeySpecException, SignatureException, InvalidKeyException {
int i = 0;
while(i < 5) {
gen();
i++;
}
}

private void gen() throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException {
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");

//PrivateKey priv = privateKeyFromString();
Signature sig = Signature.getInstance("SHA256withECDSA");
//sig.initSign(priv, random);


String timestampStr = StringUtils.rightPad(String.valueOf(Instant.now().toEpochMilli()), 16, StringUtils.SPACE);
String randomStr = StringUtils.rightPad(Base64.getEncoder().encodeToString(random.generateSeed(16)), 32, StringUtils.SPACE);

String msg = String.format("%s%s",
timestampStr,
randomStr);

sig.update(msg.getBytes(StandardCharsets.UTF_8));
byte[] signed = sig.sign();

msg += StringUtils.rightPad(Base64.getEncoder().encodeToString(signed), 97, StringUtils.SPACE);



System.out.println(msg);
}

private PrivateKey privateKeyFromString(String privateKey) throws NoSuchAlgorithmException,
InvalidKeySpecException {
byte[] pk = Base64.getDecoder().decode(privateKey);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(pk);

KeyFactory factory = KeyFactory.getInstance("EC");

return factory.generatePrivate(spec);
}

private PublicKey publicKeyFromString(String publicKey) throws NoSuchAlgorithmException,
InvalidKeySpecException {
byte[] pk = Base64.getDecoder().decode(publicKey);

EncodedKeySpec spec = new X509EncodedKeySpec(pk);

KeyFactory factory = KeyFactory.getInstance("EC");
PublicKey pubKey = factory.generatePublic(spec);

return pubKey;
}

private KeyPair signingKeys() throws NoSuchAlgorithmException {
KeyPairGenerator sGen = KeyPairGenerator.getInstance("EC");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");

sGen.initialize(256, random);

KeyPair sPair = sGen.generateKeyPair();

System.out.println("pub: " + Base64.getEncoder().encodeToString(sPair.getPublic().getEncoded()));
System.out.println("priv: " + Base64.getEncoder().encodeToString(sPair.getPrivate().getEncoded()));
return sPair;
}

public PrivateKey getPrivateKey() {
return privateKey;
}

public void setPrivateKey(PrivateKey privateKey) {
this.privateKey = privateKey;
}
}
Loading

0 comments on commit c814d62

Please sign in to comment.