示例#1
0
  public void calculate_M1() {
    BigInteger N_Hash;
    BigInteger g_Hash;
    BigInteger I_Hash;

    try {
      MessageDigest md = MessageDigest.getInstance("SHA-1");
      // Calculate H(N)
      N_Hash = new BigInteger(md.digest(N.toString().getBytes()));
      md.reset();

      // Calculate H(g)
      g_Hash = new BigInteger(md.digest(g.toString().getBytes()));
      md.reset();

      // Calculate H(I)
      I_Hash = new BigInteger(md.digest(I.toString().getBytes()));
      md.reset();

      // Calculate M1
      M1 =
          new BigInteger(
              md.digest(
                  (N_Hash.xor(g_Hash).toString()
                          + I_Hash.toString()
                          + s.toString()
                          + A.toString()
                          + B.toString()
                          + K.toString())
                      .getBytes()));
      System.out.println("Client M1: " + M1.toString());
    } catch (Exception e) {
    }
  }
 /** @tests java.math.BigInteger#xor(java.math.BigInteger) */
 @TestTargetNew(
     level = TestLevel.COMPLETE,
     notes = "",
     method = "xor",
     args = {java.math.BigInteger.class})
 public void test_xorLjava_math_BigInteger() {
   for (BigInteger[] element : booleanPairs) {
     BigInteger i1 = element[0], i2 = element[1];
     BigInteger res = i1.xor(i2);
     assertTrue("symmetry of xor", res.equals(i2.xor(i1)));
     int len = Math.max(i1.bitLength(), i2.bitLength()) + 66;
     for (int i = 0; i < len; i++) {
       assertTrue("xor", (i1.testBit(i) ^ i2.testBit(i)) == res.testBit(i));
     }
   }
 }
示例#3
0
 public void addResult(RPCInfo<FindNodeResponse> rpcInfo) {
   FindNodeResponse response = rpcInfo.getRPC();
   BigInteger found = response.getFoundNodeID();
   String foundNodeString = found.toString(16);
   try {
     if (queriedNodes.contains(found)) {
       logger.debug("Node " + foundNodeString + " already queried");
     } else if (queriedNodes.size() < maxQueries) {
       logger.debug("Found node " + foundNodeString);
       closestNode =
           new KademliaNode(found, response.getIpAddressInet(), response.getPortInteger());
       results.add(closestNode);
       BigInteger delta = found.xor(searchedNode);
       if (delta.compareTo(distanceFromClosest) < 0) {
         distanceFromClosest = delta;
         requestNode(rpcID, closestNode);
       }
     } else {
       logger.debug("Max queries reached, terminating find node");
       actualStatus = Status.ENDED;
     }
   } catch (UnknownHostException e) {
     logger.warn(e);
   }
 }
  /**
   * Find the number in between the high and the low value with the most zeroes.
   *
   * @param value the lower bound for the number
   * @param highValue the upper bound for the nubmer
   * @return the value for the number with the most zeroes between the two bounds
   */
  private BigInteger maxZeroes(BigInteger value, BigInteger highValue) {
    // Find the highest bit that would be possible to add one to before
    // going over the high value.
    int i = highValue.xor(value).bitLength();
    int j;
    while (--i >= 0) {
      // If you hit a zero bit...
      if (!value.testBit(i)) {
        // ...change that bit to a 1.
        value = value.setBit(i);

        // Create a bit mask to compare with.
        byte[] comparatorBytes = value.toByteArray();
        Arrays.fill(comparatorBytes, (byte) 0xFF);
        BigInteger comparator = new BigInteger(comparatorBytes);
        comparator = comparator.shiftLeft(i);

        // And the value with the bit mask. Everything after the changed
        // bit will be cleared en masse. Way faster than clearing bits
        // individually.
        value = value.and(comparator);
        break;
      }
    }
    return value;
  }
 /**
  * from https://github.com/hashicorp/consul/blob/master/api/event.go#L90-L104 // IDToIndex is a
  * bit of a hack. This simulates the index generation to // convert an event ID into a WaitIndex.
  * func (e *Event) IDToIndex(uuid string) uint64 { lower := uuid[0:8] + uuid[9:13] + uuid[14:18]
  * upper := uuid[19:23] + uuid[24:36] lowVal, err := strconv.ParseUint(lower, 16, 64) if err !=
  * nil { panic("Failed to convert " + lower) } highVal, err := strconv.ParseUint(upper, 16, 64) if
  * err != nil { panic("Failed to convert " + upper) } return lowVal ^ highVal //^ bitwise XOR
  * integers }
  */
 public BigInteger toIndex(String eventId) {
   String lower = eventId.substring(0, 8) + eventId.substring(9, 13) + eventId.substring(14, 18);
   String upper = eventId.substring(19, 23) + eventId.substring(24, 36);
   BigInteger lowVal = new BigInteger(lower, 16);
   BigInteger highVal = new BigInteger(upper, 16);
   BigInteger index = lowVal.xor(highVal);
   return index;
 }
示例#6
0
  public BigInteger cbcBlock(BigInteger message, BigInteger iv, boolean isEncryption) {
    BigInteger output = null;
    if (isEncryption) {
      // message xor iv
      BigInteger input = message.xor(iv);

      // encryption with idea
      output = idea(input, isEncryption);
    } else {
      // decryption with idea
      BigInteger idea = idea(message, isEncryption);

      // idea xor iv
      output = idea.xor(iv);
    }

    return output;
  }
示例#7
0
 private BigInteger hash(String source) {
   if (source == null || source.length() == 0) {
     return new BigInteger("0");
   } else {
     char[] sourceArray = source.toCharArray();
     BigInteger x = BigInteger.valueOf(((long) sourceArray[0]) << 7);
     BigInteger m = new BigInteger("1000003");
     BigInteger mask = new BigInteger("2").pow(this.hashbits).subtract(new BigInteger("1"));
     for (char item : sourceArray) {
       BigInteger temp = BigInteger.valueOf((long) item);
       x = x.multiply(m).xor(temp).and(mask);
     }
     x = x.xor(new BigInteger(String.valueOf(source.length())));
     if (x.equals(new BigInteger("-1"))) {
       x = new BigInteger("-2");
     }
     return x;
   }
 }
示例#8
0
  public static void bitwise(int order) {

    /* Test identity x^y == x|y &~ x&y */
    int failCount = 0;
    for (int i = 0; i < size; i++) {
      BigInteger x = fetchNumber(order);
      BigInteger y = fetchNumber(order);
      BigInteger z = x.xor(y);
      BigInteger w = x.or(y).andNot(x.and(y));
      if (!z.equals(w)) failCount++;
    }
    report("Logic (^ | & ~)", failCount);

    /* Test identity x &~ y == ~(~x | y) */
    failCount = 0;
    for (int i = 0; i < size; i++) {
      BigInteger x = fetchNumber(order);
      BigInteger y = fetchNumber(order);
      BigInteger z = x.andNot(y);
      BigInteger w = x.not().or(y).not();
      if (!z.equals(w)) failCount++;
    }
    report("Logic (&~ | ~)", failCount);
  }
示例#9
0
 private BigInteger xor(BigInteger a, BigInteger b) {
   return a.xor(b);
 }
  /**
   * Calculate an XOR on two byte arrays.
   *
   * @param a first byte array
   * @param b second byte array
   * @return results of XOR
   */
  public static byte[] xor(byte[] a, byte[] b) {
    BigInteger x = new BigInteger(a);
    BigInteger y = new BigInteger(b);

    return x.xor(y).toByteArray();
  }
示例#11
0
 public static BigInteger LXOr(BigInteger x, BigInteger y) {
   return x.xor(y);
 }