Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.math.BigInteger;
- import com.google.common.base.Charsets;
- import com.google.common.hash.Hashing;
- import java.math.BigInteger;
- import com.google.gson.Gson;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- import org.bouncycastle.math.ec.ECPoint;
- import org.json.simple.JSONObject;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.nio.file.StandardOpenOption;
- import java.util.HashMap;
- import java.util.Random;
- import java.util.stream.Stream;
- /**
- * Created by root on 1/20/17.
- */
- public class Signer {
- private ECPoint G;
- private ECPoint ROwner;
- private BigInteger S;
- ECPoint publicKey;
- BigInteger AOwner;
- private BigInteger secretKey;
- /**
- * constructor in which signer generate secret key and write his public key to file
- * @param id
- */
- Signer(int id) {
- secretKey = generateSecretKey();
- G = Main.ec.createPoint(Main.Gx, Main.Gy).normalize();
- publicKey = G.multiply(secretKey).normalize();
- writeToFile(id, publicKey);
- }
- /**
- * method to sign
- * @param id
- */
- JSONObject RSign(int id)
- {
- //size is always number of signers - 1 cause i must be different than j
- BigInteger[] A = new BigInteger[Main.numberOfSigners-1];
- ECPoint[] R = new ECPoint[Main.numberOfSigners-1];
- String[] hash = new String[Main.numberOfSigners-1];
- ECPoint[] Y = getPublicKeysFromFile(id);
- for(int i = 0; i < Main.numberOfSigners - 1; i++)
- {
- A[i] = generateRandom();
- R[i] = G.multiply(A[i]).normalize();
- hash[i] = Hashing.sha1().hashString(Main.M + R[i].toString(), Charsets.UTF_8 ).toString();
- }
- AOwner = generateRandom();
- ECPoint ecPoint = Y[0].multiply(Main.Q.subtract(new BigInteger(hash[0], 16))); //how to initialize it with default value?
- for(int i = 1; i < Main.numberOfSigners - 1; i++) //from one cause have no idea how to initialize ECPoint with sth diffrent than null
- {
- BigInteger h = Main.Q.subtract(new BigInteger(hash[i], 16));
- ecPoint = ecPoint.add(Y[i].multiply(h));
- }
- ROwner = G.multiply(AOwner).add(ecPoint);
- String hashOwner = Hashing.sha1().hashString(Main.M + ROwner.toString(), Charsets.UTF_8 ).toString();
- BigInteger hOwner = new BigInteger(hashOwner, 16);
- BigInteger S = new BigInteger("0");
- for(int i = 0; i < Main.numberOfSigners - 1; i++)
- {
- S = S.add(A[i]);
- }
- S = S.add(secretKey.multiply(hOwner));
- return generateJsonWithSignature(id, ROwner, R, S); //we have everything- generate signature
- }
- /**
- * method which generate json with signature=(Rs, S)
- * @param id
- * @param ROwner
- * @param R
- * @param S
- * @return
- */
- private JSONObject generateJsonWithSignature(int id, ECPoint ROwner, ECPoint[] R, BigInteger S)
- {
- //does the order of R even matters?
- JSONObject json = new JSONObject();
- json.put("id", id);
- json.put("R0", ROwner);
- for(int i = 0; i < R.length; i++)
- {
- System.out.println("i: " + i + " R: " + R[i]);
- }
- for(int i = 0; i < R.length; i++)
- {
- json.put("R"+(i+1), R[i]); //just figured out its working. If sth is stupid but its working then its not stupid. In this case it is
- }
- json.put("S", S);
- System.out.println("jsonSignature: " + json);
- return json;
- }
- /**
- * method to write id of signer and his public key to file
- * @param id
- * @param publicKey
- */
- private void writeToFile(int id, ECPoint publicKey) {
- try {
- HashMap params = new HashMap();
- params.put("Yx", new BigInteger(publicKey.getAffineXCoord().toString(),16));
- params.put("Yy", new BigInteger(publicKey.getAffineYCoord().toString(),16));
- JSONObject json = new JSONObject();
- json.put("id", id);
- json.put("params", params);
- Files.write(Paths.get(Main.pathToFile), (json.toString()+"\n").getBytes(), StandardOpenOption.APPEND);
- }catch (IOException e) {
- //exception handling left as an exercise for the Staniuch
- }
- }
- /**
- *
- * @param id - we have to omit it cause we already have it
- * @return
- */
- private ECPoint[] getPublicKeysFromFile(int id)
- {
- ECPoint[] Y = new ECPoint[Main.numberOfSigners-1];
- File file = new File(Main.pathToFile);
- try (BufferedReader br = new BufferedReader(new FileReader(file))) {
- String line;
- int indexInArray = 0; //yeah, could just use ArrayList
- while ((line = br.readLine()) != null) {
- //System.out.println("line: " + line);
- com.google.gson.JsonObject json = new JsonParser().parse(line).getAsJsonObject();
- if(Integer.parseInt(String.valueOf(json.get("id"))) != id)
- {
- BigInteger Yx = new BigInteger(json.getAsJsonObject("params").get("Yx").toString());
- BigInteger Yy = new BigInteger(json.getAsJsonObject("params").get("Yy").toString());
- Y[indexInArray] = Main.ec.createPoint(Yx, Yy);
- indexInArray++;
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return Y;
- }
- private BigInteger generateRandom() {
- Random r = new Random();
- BigInteger result = new BigInteger(Main.Q.bitLength(), r);
- return result;
- }
- private BigInteger generateSecretKey() {
- Random r = new Random();
- BigInteger result = new BigInteger(Main.Q.bitLength(), r);
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement