예제 #1
0
  public static boolean verify(
      byte[] signature, byte[] message, byte[] publicKey, boolean enforceCanonical) {

    try {

      if (enforceCanonical && !Curve25519.isCanonicalSignature(signature)) {
        Logger.logDebugMessage("Rejecting non-canonical signature");
        return false;
      }

      if (enforceCanonical && !Curve25519.isCanonicalPublicKey(publicKey)) {
        Logger.logDebugMessage("Rejecting non-canonical public key");
        return false;
      }

      byte[] Y = new byte[32];
      byte[] v = new byte[32];
      System.arraycopy(signature, 0, v, 0, 32);
      byte[] h = new byte[32];
      System.arraycopy(signature, 32, h, 0, 32);
      Curve25519.verify(Y, v, h, publicKey);

      MessageDigest digest = Crypto.sha256();
      byte[] m = digest.digest(message);
      digest.update(m);
      byte[] h2 = digest.digest(Y);

      return Arrays.equals(h, h2);

    } catch (RuntimeException e) {
      Logger.logMessage("Error in Crypto verify", e);
      return false;
    }
  }
 private void loadPassPhrases() {
   try {
     List<String> passphrases =
         Files.readAllLines(Paths.get("passphrases.txt"), Charset.forName("US-ASCII"));
     for (String ps : passphrases) {
       if (!ps.isEmpty()) {
         byte[] publicKey = Crypto.getPublicKey(ps);
         byte[] publicKeyHash = Crypto.sha256().digest(publicKey);
         Long id = Convert.fullHashToId(publicKeyHash);
         loadedPassPhrases.put(id, ps);
         LOGGER.info("Added key: {" + ps + "} -> {" + Convert.toUnsignedLong(id) + "}");
       }
     }
   } catch (IOException e) {
     LOGGER.info("Warning: no passphrases.txt found");
   }
 }
예제 #3
0
  @Override
  JSONStreamAware processRequest(HttpServletRequest req) throws NxtException.ValidationException {

    String transactionBytes = Convert.emptyToNull(req.getParameter("unsignedTransactionBytes"));
    String transactionJSON = Convert.emptyToNull(req.getParameter("unsignedTransactionJSON"));
    if (transactionBytes == null && transactionJSON == null) {
      return MISSING_UNSIGNED_BYTES;
    }
    String secretPhrase = Convert.emptyToNull(req.getParameter("secretPhrase"));
    if (secretPhrase == null) {
      return MISSING_SECRET_PHRASE;
    }

    try {
      Transaction transaction;
      if (transactionBytes != null) {
        byte[] bytes = Convert.parseHexString(transactionBytes);
        transaction = Nxt.getTransactionProcessor().parseTransaction(bytes);
      } else {
        JSONObject json = (JSONObject) JSONValue.parse(transactionJSON);
        transaction = Nxt.getTransactionProcessor().parseTransaction(json);
      }
      transaction.validate();
      if (transaction.getSignature() != null) {
        JSONObject response = new JSONObject();
        response.put("errorCode", 4);
        response.put(
            "errorDescription",
            "Incorrect \"unsignedTransactionBytes\" - transaction is already signed");
        return response;
      }
      transaction.sign(secretPhrase);
      JSONObject response = new JSONObject();
      response.put("transaction", transaction.getStringId());
      response.put("fullHash", transaction.getFullHash());
      response.put("transactionBytes", Convert.toHexString(transaction.getBytes()));
      response.put(
          "signatureHash", Convert.toHexString(Crypto.sha256().digest(transaction.getSignature())));
      response.put("verify", transaction.verifySignature());
      return response;
    } catch (NxtException.ValidationException | RuntimeException e) {
      // Logger.logDebugMessage(e.getMessage(), e);
      return INCORRECT_UNSIGNED_BYTES;
    }
  }
예제 #4
0
  public static byte[] getPublicKey(String secretPhrase) {

    try {

      byte[] publicKey = new byte[32];
      Curve25519.keygen(publicKey, null, Crypto.sha256().digest(secretPhrase.getBytes("UTF-8")));

      if (!Curve25519.isCanonicalPublicKey(publicKey)) {
        throw new RuntimeException("Public key not canonical");
      }

      return publicKey;

    } catch (RuntimeException | UnsupportedEncodingException e) {
      Logger.logMessage("Error getting public key", e);
      return null;
    }
  }
예제 #5
0
  public static byte[] sign(byte[] message, String secretPhrase) {

    try {

      byte[] P = new byte[32];
      byte[] s = new byte[32];
      MessageDigest digest = Crypto.sha256();
      Curve25519.keygen(P, s, digest.digest(secretPhrase.getBytes("UTF-8")));

      byte[] m = digest.digest(message);

      digest.update(m);
      byte[] x = digest.digest(s);

      byte[] Y = new byte[32];
      Curve25519.keygen(Y, null, x);

      digest.update(m);
      byte[] h = digest.digest(Y);

      byte[] v = new byte[32];
      Curve25519.sign(v, h, x, s);

      byte[] signature = new byte[64];
      System.arraycopy(v, 0, signature, 0, 32);
      System.arraycopy(h, 0, signature, 32, 32);

      if (!Curve25519.isCanonicalSignature(signature)) {
        throw new RuntimeException("Signature not canonical");
      }

      return signature;

    } catch (RuntimeException | UnsupportedEncodingException e) {
      Logger.logMessage("Error in signing message", e);
      return null;
    }
  }
예제 #6
0
 public static long getId(byte[] publicKey) {
   byte[] publicKeyHash = Crypto.sha256().digest(publicKey);
   return Convert.fullHashToId(publicKeyHash);
 }