Java Code Examples for java.math.BigInteger#xor()

The following examples show how to use java.math.BigInteger#xor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HopTwoWayPasswordEncoder.java    From hop with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
protected String decryptPasswordInternal( String encrypted ) {
  if ( encrypted == null ) {
    return "";
  }
  if ( encrypted.length() == 0 ) {
    return "";
  }

  BigInteger bi_confuse = new BigInteger( getSeed() );

  try {
    BigInteger bi_r1 = new BigInteger( encrypted, RADIX );
    BigInteger bi_r0 = bi_r1.xor( bi_confuse );

    return new String( bi_r0.toByteArray() );
  } catch ( Exception e ) {
    return "";
  }
}
 
Example 2
Source File: HashUtil.java    From mango with Apache License 2.0 5 votes vote down vote up
private static BigInteger fnv1_32(byte[] data) {
  BigInteger hash = INIT32;

  for (byte b : data) {
    hash = hash.multiply(PRIME32).mod(MOD32);
    hash = hash.xor(BigInteger.valueOf((int) b & 0xff));
  }

  return hash;
}
 
Example 3
Source File: BigIntegerXorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Xor for a negative number and zero  
 */
public void testNegPos() {
    String numA = "-27384627835298756289327365";
    String numB = "0";
    String res = "-27384627835298756289327365";
    BigInteger aNumber = new BigInteger(numA);
    BigInteger bNumber = new BigInteger(numB);
    BigInteger result = aNumber.xor(bNumber);
    assertTrue(res.equals(result.toString()));
}
 
Example 4
Source File: BigIntegerXorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Xor for a positive and a negative numbers; the first is longer
 */
public void testPosNegFirstLonger() {
    String numA = "2837462783428374767845648748973847593874837948575684767";
    String numB = "-293478573489347658763745839457637";
    String res = "-2837462783428374767845615168483972194300564226167553532";
    BigInteger aNumber = new BigInteger(numA);
    BigInteger bNumber = new BigInteger(numB);
    BigInteger result = aNumber.xor(bNumber);
    assertTrue(res.equals(result.toString()));
}
 
Example 5
Source File: BigIntegerXorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Xor for zero and zero
 */
public void testZeroZero() {
    String numA = "0";
    String numB = "0";
    String res = "0";
    BigInteger aNumber = new BigInteger(numA);
    BigInteger bNumber = new BigInteger(numB);
    BigInteger result = aNumber.xor(bNumber);
    assertTrue(res.equals(result.toString()));
}
 
Example 6
Source File: VarInt.java    From BukkitPE with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param v Unsigned encoded long
 * @return Unsigned decoded long
 */
public static BigInteger decodeZigZag64(BigInteger v) {
    _assert(v);
    BigInteger left = v.shiftRight(1);
    BigInteger right = v.and(BigInteger.ONE).negate();
    return left.xor(right);
}
 
Example 7
Source File: ItemFuncBitXor.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@Override
public BigInteger valInt() {
    BigInteger arg1 = args.get(0).valInt();
    BigInteger arg2 = args.get(1).valInt();
    if (nullValue = (args.get(0).isNullValue() || args.get(1).isNullValue()))
        return BigInteger.ZERO;
    return arg1.xor(arg2);
}
 
Example 8
Source File: BitOps.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
public static BigInteger bitXor(BigInteger p, BigInteger p2, NullPointer nullPointer) {
    if (p == null || p2 == null) {
        nullPointer.setNullValue(true);
        return BigInteger.ZERO;
    }
    nullPointer.setNullValue(false);
    return p.xor(p2);
}
 
Example 9
Source File: BigIntegerXorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Xor for two positive numbers; the first is longer
 */
public void testPosPosFirstLonger() {
    String numA = "2837462783428374767845648748973847593874837948575684767";
    String numB = "293478573489347658763745839457637";
    String res = "2837462783428374767845615168483972194300564226167553530";
    BigInteger aNumber = new BigInteger(numA);
    BigInteger bNumber = new BigInteger(numB);
    BigInteger result = aNumber.xor(bNumber);
    assertTrue(res.equals(result.toString()));
}
 
Example 10
Source File: BigIntegerXorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Xor for two numbers of different signs and the same length
 */
public void testNegPosSameLength() {
    String numA = "-283746278342837476784564875684767";
    String numB = "293478573489347658763745839457637";
    String res = "-71412358434940908477702819237628";
    BigInteger aNumber = new BigInteger(numA);
    BigInteger bNumber = new BigInteger(numB);
    BigInteger result = aNumber.xor(bNumber);
    assertTrue(res.equals(result.toString()));
}
 
Example 11
Source File: BigIntegerXorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Xor for two negative numbers of the same length
 */
public void testNegNegSameLength() {
    String numA = "-283746278342837476784564875684767";
    String numB = "-293478573489347658763745839457637";
    String res = "71412358434940908477702819237626";
    BigInteger aNumber = new BigInteger(numA);
    BigInteger bNumber = new BigInteger(numB);
    BigInteger result = aNumber.xor(bNumber);
    assertTrue(res.equals(result.toString()));
}
 
Example 12
Source File: KeyComparator.java    From Kademlia with MIT License 5 votes vote down vote up
/**
 * Compare two objects which must both be of type <code>Node</code>
 * and determine which is closest to the identifier specified in the
 * constructor.
 *
 * @param n1 Node 1 to compare distance from the key
 * @param n2 Node 2 to compare distance from the key
 */
@Override
public int compare(Node n1, Node n2)
{
    BigInteger b1 = n1.getNodeId().getInt();
    BigInteger b2 = n2.getNodeId().getInt();

    b1 = b1.xor(key);
    b2 = b2.xor(key);

    return b1.abs().compareTo(b2.abs());
}
 
Example 13
Source File: WNafUtil.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public static int getNafWeight(BigInteger k)
{
    if (k.signum() == 0)
    {
        return 0;
    }

    BigInteger _3k = k.shiftLeft(1).add(k);
    BigInteger diff = _3k.xor(k);

    return diff.bitCount();
}
 
Example 14
Source File: BigIntegerXorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Xor for a positive and a negative number; the first is shorter
 */
public void testPosNegFirstShorter() {
    String numA = "293478573489347658763745839457637";
    String numB = "-2837462783428374767845648748973847593874837948575684767";
    String res = "-2837462783428374767845615168483972194300564226167553532";
    BigInteger aNumber = new BigInteger(numA);
    BigInteger bNumber = new BigInteger(numB);
    BigInteger result = aNumber.xor(bNumber);
    assertTrue(res.equals(result.toString()));
}
 
Example 15
Source File: HammingDistanceUDF.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
public static int hammingDistance(final BigInteger a, final BigInteger b) {
    BigInteger xor = a.xor(b);
    return xor.bitCount();
}
 
Example 16
Source File: AES.java    From Java with MIT License 4 votes vote down vote up
/**
 *
 * Returns an array of 10 + 1 round keys that are calculated by using Rijndael
 * key schedule
 *
 * @param initialKey
 * @return array of 10 + 1 round keys
 */
public static BigInteger[] keyExpansion(BigInteger initialKey) {
	BigInteger[] roundKeys = { initialKey, new BigInteger("0"), new BigInteger("0"), new BigInteger("0"),
			new BigInteger("0"), new BigInteger("0"), new BigInteger("0"), new BigInteger("0"), new BigInteger("0"),
			new BigInteger("0"), new BigInteger("0"), };

	// initialize rcon iteration
	int rconCounter = 1;

	for (int i = 1; i < 11; i++) {

		// get the previous 32 bits the key
		BigInteger t = roundKeys[i - 1].remainder(new BigInteger("100000000", 16));

		// split previous key into 8-bit segments
		BigInteger[] prevKey = { roundKeys[i - 1].remainder(new BigInteger("100000000", 16)),
				roundKeys[i - 1].remainder(new BigInteger("10000000000000000", 16))
						.divide(new BigInteger("100000000", 16)),
				roundKeys[i - 1].remainder(new BigInteger("1000000000000000000000000", 16))
						.divide(new BigInteger("10000000000000000", 16)),
				roundKeys[i - 1].divide(new BigInteger("1000000000000000000000000", 16)), };

		// run schedule core
		t = scheduleCore(t, rconCounter);
		rconCounter += 1;

		// Calculate partial round key
		BigInteger t0 = t.xor(prevKey[3]);
		BigInteger t1 = t0.xor(prevKey[2]);
		BigInteger t2 = t1.xor(prevKey[1]);
		BigInteger t3 = t2.xor(prevKey[0]);

		// Join round key segments
		t2 = t2.multiply(new BigInteger("100000000", 16));
		t1 = t1.multiply(new BigInteger("10000000000000000", 16));
		t0 = t0.multiply(new BigInteger("1000000000000000000000000", 16));
		roundKeys[i] = t0.add(t1).add(t2).add(t3);

	}
	return roundKeys;
}
 
Example 17
Source File: WNafUtil.java    From ripple-lib-java with ISC License 4 votes vote down vote up
public static int[] generateCompactNaf(BigInteger k)
{
    if ((k.bitLength() >>> 16) != 0)
    {
        throw new IllegalArgumentException("'k' must have bitlength < 2^16");
    }
    if (k.signum() == 0)
    {
        return EMPTY_INTS;
    }

    BigInteger _3k = k.shiftLeft(1).add(k);

    int bits = _3k.bitLength();
    int[] naf = new int[bits >> 1];

    BigInteger diff = _3k.xor(k);

    int highBit = bits - 1, length = 0, zeroes = 0;
    for (int i = 1; i < highBit; ++i)
    {
        if (!diff.testBit(i))
        {
            ++zeroes;
            continue;
        }

        int digit  = k.testBit(i) ? -1 : 1;
        naf[length++] = (digit << 16) | zeroes;
        zeroes = 1;
        ++i;
    }

    naf[length++] = (1 << 16) | zeroes;

    if (naf.length > length)
    {
        naf = trim(naf, length);
    }

    return naf;
}
 
Example 18
Source File: Operators.java    From beanshell with Apache License 2.0 4 votes vote down vote up
static Object bigIntegerBinaryOperation(BigInteger lhs, BigInteger rhs, int kind)
{

    switch(kind)
    {
        // arithmetic
        case PLUS:
            return lhs.add(rhs);

        case MINUS:
            return lhs.subtract(rhs);

        case STAR:
            return lhs.multiply(rhs);

        case SLASH:
            return lhs.divide(rhs);

        case MOD:
        case MODX:
            return lhs.mod(rhs);

        case POWER:
        case POWERX:
            return lhs.pow(rhs.intValue());

        // bitwise
        case LSHIFT:
        case LSHIFTX:
            return lhs.shiftLeft(rhs.intValue());

        case RSIGNEDSHIFT:
        case RSIGNEDSHIFTX:
            return lhs.shiftRight(rhs.intValue());

        case RUNSIGNEDSHIFT:
        case RUNSIGNEDSHIFTX:
            if ( lhs.signum() >= 0 )
                return lhs.shiftRight(rhs.intValue());
            BigInteger opener = BigInteger.ONE.shiftLeft(lhs.toString(2).length() + 1);
            BigInteger opened = lhs.subtract(opener);
            BigInteger mask = opener.subtract(BigInteger.ONE).shiftRight(rhs.intValue() + 1);
            return opened.shiftRight(rhs.intValue()).and(mask);


        case BIT_AND:
        case BIT_ANDX:
            return lhs.and(rhs);

        case BIT_OR:
        case BIT_ORX:
            return lhs.or(rhs);

        case XOR:
        case XORX:
            return lhs.xor(rhs);

    }
    throw new InterpreterError(
            "Unimplemented binary integer operator");
}
 
Example 19
Source File: BigIntegerMutatorTest.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Override
BigInteger apply(BigInteger left, BigInteger right) {
  return left.xor(right);
}
 
Example 20
Source File: BigIntType.java    From es6draft with MIT License 2 votes vote down vote up
/**
 * BigInt::bitwiseXOR (x, y)
 * 
 * @param x
 *            the x value
 * @param y
 *            the y value
 * @return the result of bitwise xor-ing {@code x} and {@code y}
 */
public static BigInteger bitwiseXOR(BigInteger x, BigInteger y) {
    return x.xor(y);
}