List of usage examples for java.math BigInteger toByteArray
public byte[] toByteArray()
From source file:HexUtil.java
/** * Write a (reasonably short) BigInteger to a stream. * @param integer the BigInteger to write * @param out the stream to write it to *//*www .j ava2s. c o m*/ public static void writeBigInteger(BigInteger integer, DataOutputStream out) throws IOException { if (integer.signum() == -1) { //dump("Negative BigInteger", Logger.ERROR, true); throw new IllegalStateException("Negative BigInteger!"); } byte[] buf = integer.toByteArray(); if (buf.length > Short.MAX_VALUE) throw new IllegalStateException("Too long: " + buf.length); out.writeShort((short) buf.length); out.write(buf); }
From source file:org.cryptomath.CryptoMath.java
public static String multiply(final String a, final String b, final String alias) throws CryptoMathException { try {/*from w w w. j a v a 2 s. com*/ RSAFunctionUtil util = new RSAFunctionUtil(); KeyPair kp = (KeyPair) util.getKeyPair(alias); BigInteger m = RSAMath.multiply(new BigInteger(Base64.decodeBase64(a.getBytes())), new BigInteger(Base64.decodeBase64(b.getBytes())), kp.getPublicKey()); return new String(Base64.encodeBase64(m.toByteArray())); } catch (KeyGenerationException ex) { logger.error("Multiplying given values failed", ex); throw new CryptoMathException("Multiplying given values failed", ex); } }
From source file:com.sun.identity.openid.provider.Codec.java
/** * TODO: Description.//from w w w . j a v a 2s. c om * * @param value * TODO. * @return TODO. */ public static String encodeBigInteger(BigInteger value) { if (value == null) { return null; } return encodeBytes(value.toByteArray()); }
From source file:jp.co.ntts.vhut.util.Ipv4ConversionUtil.java
/** * IP(HEX)????./* w w w .j a v a 2 s . c o m*/ * ????int????????????. * @param startIpaddr (HEX/) * @param endIpaddr (HEX/) * @return IP(HEX)?? */ public static Set<String> getIpAddressSetBetween(String startIpaddr, String endIpaddr) { BigInteger startBI = new BigInteger(addrTobyte(startIpaddr)); BigInteger endBI = new BigInteger(addrTobyte(endIpaddr)); int length = (int) Math.min(endBI.subtract(startBI).longValue(), Integer.MAX_VALUE); Set<String> resultSet = new HashSet<String>(); BigInteger currentBI = startBI; for (int i = 0; i <= length; i++) { resultSet.add(byteToAddr(currentBI.toByteArray())); currentBI = currentBI.add(BigInteger.ONE); } return resultSet; }
From source file:org.energy_home.jemma.javagal.layers.data.implementations.Utils.DataManipulation.java
/** * Converts a {@code BigInteger} to a {@code byte[]}. The resulting array * will have all bytes contained in the BigInteger placed in the same * position (from byte 0 to byte n). The pad parameter indicate the minimum * size of the resulting array. In case its size would be less then the one * indicated in the pad parameter, a number of leading zeros will be * inserted./*from w w w . j av a2 s . c o m*/ * * @param toConvert * the BigInteger to convert. * @param pad * the minimum size of the returned array. * @return the resulting array. */ public static byte[] toByteVect(BigInteger toConvert, int pad) { byte[] toReturn = new byte[pad]; byte[] byteArray = toConvert.toByteArray(); int i; for (i = 0; i <= (pad - byteArray.length - 1); i++) toReturn[i] = 0; int x = 0; for (; i <= (toReturn.length - 1); i++) toReturn[i] = byteArray[x++]; return toReturn; }
From source file:org.red5.server.net.rtmp.RTMPHandshake.java
/** * Returns the public key for a given key pair. * /*from w ww. ja v a2s . c o m*/ * @param keyPair * @return public key */ protected static byte[] getPublicKey(KeyPair keyPair) { DHPublicKey incomingPublicKey = (DHPublicKey) keyPair.getPublic(); BigInteger dhY = incomingPublicKey.getY(); log.debug("Public key: {}", dhY); byte[] result = dhY.toByteArray(); //log.debug("Public key as bytes - length [{}]: {}", result.length, Hex.encodeHexString(result)); byte[] temp = new byte[KEY_LENGTH]; if (result.length < KEY_LENGTH) { System.arraycopy(result, 0, temp, KEY_LENGTH - result.length, result.length); result = temp; log.debug("Padded public key length to 128"); } else if (result.length > KEY_LENGTH) { System.arraycopy(result, result.length - KEY_LENGTH, temp, 0, KEY_LENGTH); result = temp; log.debug("Truncated public key length to 128"); } return result; }
From source file:org.springframework.ldap.support.LdapUtils.java
/** * Converts the given number to a binary representation of the specified * length and "endian-ness"./* w ww . j a v a 2s .co m*/ * * @param number String with number to convert * @param length How long the resulting binary array should be * @param bigEndian <code>true</code> if big endian (5=0005), or * <code>false</code> if little endian (5=5000) * @return byte array containing the binary result in the given order */ static byte[] numberToBytes(String number, int length, boolean bigEndian) { BigInteger bi = new BigInteger(number); byte[] bytes = bi.toByteArray(); int remaining = length - bytes.length; if (remaining < 0) { bytes = ArrayUtils.subarray(bytes, -remaining, bytes.length); } else { byte[] fill = new byte[remaining]; bytes = ArrayUtils.addAll(fill, bytes); } if (!bigEndian) { ArrayUtils.reverse(bytes); } return bytes; }
From source file:org.apache.carbondata.core.util.DataTypeUtil.java
/** * This method will convert a big decimal value to bytes * * @param num//from w w w . j a v a 2 s .com * @return */ public static byte[] bigDecimalToByte(BigDecimal num) { BigInteger sig = new BigInteger(num.unscaledValue().toString()); int scale = num.scale(); byte[] bscale = new byte[] { (byte) (scale) }; byte[] buff = sig.toByteArray(); byte[] completeArr = new byte[buff.length + bscale.length]; System.arraycopy(bscale, 0, completeArr, 0, bscale.length); System.arraycopy(buff, 0, completeArr, bscale.length, buff.length); return completeArr; }
From source file:Main.java
public static byte[] base58ToBytes(String value) { BigInteger bigNum58 = new BigInteger("58"); BigInteger tempBigValue = new BigInteger("0"); int leadingZeroes = 0; boolean justStarted = true; for (int i = 0; i < value.length(); i++) { if (justStarted && value.toCharArray()[i] == '1') { leadingZeroes++;/*from www .j a va2 s. c o m*/ } else { justStarted = false; tempBigValue = tempBigValue.add(bigNum58.pow(value.length() - i - 1) .multiply(new BigInteger("" + base58Array.indexOf(value.toCharArray()[i])))); } } byte[] bigValue = tempBigValue.toByteArray(); int bigValueStart = 0; for (int j = 0; j < bigValue.length; j++) { if (bigValue[j] != 0) { bigValueStart = j; break; } } byte[] byteResult = new byte[bigValue.length + leadingZeroes - bigValueStart]; for (int i = 0; i < byteResult.length; i++) { if (i - leadingZeroes + bigValueStart < bigValue.length && i - leadingZeroes + bigValueStart >= 0) byteResult[i] = i < leadingZeroes ? 0 : bigValue[i - leadingZeroes + bigValueStart]; } return byteResult; }
From source file:com.d2lvalence.idkeyauth.codec.binary.Base64.java
/** * Returns a byte-array representation of a <code>BigInteger</code> without sign bit. * * @param bigInt// w w w . j a v a 2s. c o m * <code>BigInteger</code> to be converted * @return a byte array representation of the BigInteger parameter */ static byte[] toIntegerBytes(final BigInteger bigInt) { int bitlen = bigInt.bitLength(); // round bitlen bitlen = ((bitlen + 7) >> 3) << 3; final byte[] bigBytes = bigInt.toByteArray(); if (((bigInt.bitLength() % 8) != 0) && (((bigInt.bitLength() / 8) + 1) == (bitlen / 8))) { return bigBytes; } // set up params for copying everything but sign bit int startSrc = 0; int len = bigBytes.length; // if bigInt is exactly byte-aligned, just skip signbit in copy if ((bigInt.bitLength() % 8) == 0) { startSrc = 1; len--; } final int startDst = bitlen / 8 - len; // to pad w/ nulls as per spec final byte[] resizedBytes = new byte[bitlen / 8]; System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, len); return resizedBytes; }