Example usage for java.math BigInteger toByteArray

List of usage examples for java.math BigInteger toByteArray

Introduction

In this page you can find the example usage for java.math BigInteger toByteArray.

Prototype

public byte[] toByteArray() 

Source Link

Document

Returns a byte array containing the two's-complement representation of this BigInteger.

Usage

From source file:org.rifidi.edge.core.services.notification.data.EPCGeneration1Event.java

/**
 * Set the epc memory bank./*from  w ww .  j a v a 2s  .c om*/
 * 
 * @param memBank
 * @param length
 *            The size of the memory bank in bits
 */
public void setEPCMemory(BigInteger memBank, Integer length) {
    hex = new String(Hex.encodeHex(memBank.toByteArray()));
    memoryBanks.get(0).setLength(length);
    memoryBanks.get(0).setMemory(memBank);
}

From source file:com.trsst.Common.java

public static byte[] fromBase58(String s) {
    try {//w  ww .j  a  va2s . co m
        boolean leading = true;
        int lz = 0;
        BigInteger b = BigInteger.ZERO;
        for (char c : s.toCharArray()) {
            if (leading && c == '1') {
                ++lz;
            } else {
                leading = false;
                b = b.multiply(BigInteger.valueOf(58));
                b = b.add(BigInteger.valueOf(r58[c]));
            }
        }
        byte[] encoded = b.toByteArray();
        if (encoded[0] == 0) {
            if (lz > 0) {
                --lz;
            } else {
                byte[] e = new byte[encoded.length - 1];
                System.arraycopy(encoded, 1, e, 0, e.length);
                encoded = e;
            }
        }
        byte[] result = new byte[encoded.length + lz];
        System.arraycopy(encoded, 0, result, lz, encoded.length);

        return result;
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new IllegalArgumentException("Invalid character in address");
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:org.openbaton.nfvo.core.api.KeyManagement.java

private String encodePublicKey(RSAPublicKey key, String keyname) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    /* encode the "ssh-rsa" string */
    byte[] sshrsa = new byte[] { 0, 0, 0, 7, 's', 's', 'h', '-', 'r', 's', 'a' };
    out.write(sshrsa);//from  w  w w . j  a  v  a 2 s  .  com
    /* Encode the public exponent */
    BigInteger e = key.getPublicExponent();
    byte[] data = e.toByteArray();
    encodeUInt32(data.length, out);
    out.write(data);
    /* Encode the modulus */
    BigInteger m = key.getModulus();
    data = m.toByteArray();
    encodeUInt32(data.length, out);
    out.write(data);
    return "ssh-rsa " + Base64.encodeBase64String(out.toByteArray()) + " " + keyname;
}

From source file:cherry.windows.charset.ListCreator.java

public void createList() throws IOException {

    Charset ms932 = Charset.forName("MS932");
    Charset cp943 = Charset.forName("CP943");
    Charset win31j = Charset.forName("Windows-31J");

    System.out.println("WIN\tUNI\tMS932(1)\tMS932(2)\tCP943(1)\tCP943(2)\tWin31J(1)\tWin31J(2)\tCOMMENT");

    List<Entry> list = tableParser.parse("CP932.TXT");
    for (Entry entry : list) {

        System.out.print(entry.getWinCode());
        System.out.print("\t");
        System.out.print(entry.getUniCode());
        System.out.print("\t");

        BigInteger winCode = new BigInteger(entry.getWinCode(), 16);

        BigInteger bi932x1 = new BigInteger((new String(winCode.toByteArray(), ms932)).getBytes(ms932));
        BigInteger bi932x2 = new BigInteger((new String(bi932x1.toByteArray(), ms932)).getBytes(ms932));
        System.out.print(winCode.equals(bi932x1));
        System.out.print("\t");
        System.out.print(bi932x1.equals(bi932x2));
        System.out.print("\t");

        BigInteger bi943x1 = new BigInteger((new String(winCode.toByteArray(), cp943)).getBytes(cp943));
        BigInteger bi943x2 = new BigInteger((new String(bi943x1.toByteArray(), cp943)).getBytes(cp943));
        System.out.print(winCode.equals(bi943x1));
        System.out.print("\t");
        System.out.print(bi943x1.equals(bi943x2));
        System.out.print("\t");

        BigInteger bi31jx1 = new BigInteger((new String(winCode.toByteArray(), win31j)).getBytes(win31j));
        BigInteger bi31jx2 = new BigInteger((new String(bi31jx1.toByteArray(), win31j)).getBytes(win31j));
        System.out.print(winCode.equals(bi31jx1));
        System.out.print("\t");
        System.out.print(bi31jx1.equals(bi31jx2));
        System.out.print("\t");

        System.out.print(entry.getComment());
        System.out.println();//from  w  ww  .  jav  a  2  s  .c  o  m
    }
}

From source file:org.wso2.carbon.identity.oauth.endpoint.jwks.JwksEndpoint.java

public String base64EncodeUint(final BigInteger v) {

    return base64urlEncode(v.toByteArray());
}

From source file:com.openvcx.conference.KeyGenerator.java

/**
 * <p>Obtains a sequence of random bytes.</p>
 * <p><i>java.security.SecureRandom</i> is used as the random number generator</p>
 * @param length The length in bytes of the random byte sequence to generate
 * @param algo The algorithm used to hash the random bytes
 * @return The random byte sequence/*  w  ww .  j a v a 2 s .c o  m*/
 */
public byte[] getRandomBytes(int length, String algo) throws NoSuchAlgorithmException {
    int index = 0;
    byte[] randBytes = new byte[length];

    synchronized (m_sync) {

        while (index < length) {
            BigInteger bi = new BigInteger(64, m_rand);
            byte[] b = createHash(bi.toByteArray(), algo);
            //int i; for(i=0; i<b.length; i++) {System.out.printf("0x%02X ", b[i]); }
            int copy = Math.min(length - index, b.length);
            System.arraycopy(b, 0, randBytes, index, copy);
            index += copy;
        }

    }

    return randBytes;
}

From source file:nl.minvenj.pef.pseudo.IPPseudonymizer.java

private byte[] toIPAddressOfSize(final BigInteger bigInt, final int size) {
    final byte[] bytes = new byte[size];
    final byte[] intBytes = bigInt.toByteArray();

    if (size == intBytes.length) {
        System.arraycopy(intBytes, 0, bytes, 0, size);
    } else if (size < intBytes.length) {
        System.arraycopy(intBytes, intBytes.length - size, bytes, 0, size);
    } else {//from w  w w  .j av  a 2  s.  c o m
        System.arraycopy(intBytes, 0, bytes, size - intBytes.length, intBytes.length);
    }
    return bytes;
}

From source file:com.offbynull.peernetic.common.identification.IdGenerator.java

/**
 * Generates a {@link Id}./*from   w  w w . j a  v a  2  s.co  m*/
 * @return a {@link Id} between {@code 0} and {@code limit}
 * @throws NullPointerException if any arguments are {@code null}
 */
public Id generate() {
    try {
        int rawLen = limit.length;
        byte[] raw = new byte[rawLen];
        random.nextBytes(raw);

        BigInteger rawBd = new BigInteger(1, raw);
        BigInteger limitBd = new BigInteger(1, limit);

        rawBd = rawBd.mod(limitBd);

        return new Id(rawBd.toByteArray(), limit);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.coinspark.core.CSUtils.java

public static void littleEndianByteArray(BigInteger n, byte[] arr, int off) {
    if (n == null) {
        return;/*from   ww  w  .  j  av  a  2  s .  c  o  m*/
    }
    byte[] k = n.toByteArray();
    for (int i = 0; i < k.length; i++) {
        arr[off + i] = k[k.length - i - 1];
    }
}

From source file:test.integ.be.fedict.trust.Foreigner201305Test.java

@Test
public void testForeigner201305() throws Exception {
    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    X509Certificate rootCert = (X509Certificate) certificateFactory
            .generateCertificate(Foreigner201305Test.class.getResourceAsStream("/belgiumrca2.crt"));
    X509Certificate foreigner201304Cert = (X509Certificate) certificateFactory
            .generateCertificate(Foreigner201305Test.class.getResourceAsStream("/foreigner201304.crt"));

    foreigner201304Cert.verify(rootCert.getPublicKey());

    X509Certificate foreigner201305Cert = (X509Certificate) certificateFactory
            .generateCertificate(Foreigner201305Test.class.getResourceAsStream("/foreigner201305.crt"));

    foreigner201305Cert.verify(rootCert.getPublicKey());

    byte[] foreigner201304SignatureValue = foreigner201304Cert.getSignature();
    byte[] foreigner201305SignatureValue = foreigner201305Cert.getSignature();
    LOG.debug("201304 signature size: " + foreigner201304SignatureValue.length);
    LOG.debug("201305 signature size: " + foreigner201305SignatureValue.length);

    RSAPublicKey rootPublicKey = (RSAPublicKey) rootCert.getPublicKey();

    BigInteger foreigner201304Signature = new BigInteger(foreigner201304SignatureValue);
    BigInteger foreigner201305Signature = new BigInteger(foreigner201305SignatureValue);

    LOG.debug("201305 signature size: " + foreigner201305Signature.toByteArray().length);

    BigInteger foreigner201304PaddedMessage = foreigner201304Signature.modPow(rootPublicKey.getPublicExponent(),
            rootPublicKey.getModulus());
    BigInteger foreigner201305PaddedMessage = foreigner201305Signature.modPow(rootPublicKey.getPublicExponent(),
            rootPublicKey.getModulus());

    LOG.debug(//from  w w w .ja va 2s . co m
            "201304 padded message: " + new String(Hex.encodeHex(foreigner201304PaddedMessage.toByteArray())));
    LOG.debug(
            "201305 padded message: " + new String(Hex.encodeHex(foreigner201305PaddedMessage.toByteArray())));

    LOG.debug("201304 modulus size: "
            + ((RSAPublicKey) foreigner201304Cert.getPublicKey()).getModulus().toByteArray().length);
    LOG.debug("201305 modulus size: "
            + ((RSAPublicKey) foreigner201305Cert.getPublicKey()).getModulus().toByteArray().length);
    LOG.debug("201304 modulus: " + new String(
            Hex.encodeHex(((RSAPublicKey) foreigner201304Cert.getPublicKey()).getModulus().toByteArray())));
    LOG.debug("201305 modulus: " + new String(
            Hex.encodeHex(((RSAPublicKey) foreigner201305Cert.getPublicKey()).getModulus().toByteArray())));
}